XSLT 检查字符串是否在一个集合中

XSLT check if String is inside a set

我正在尝试检查一个字符串是否包含在一个集合中。我有一个 Excel sheet,我将其转换为 xml 文件;示例:

左侧

Excel sheet 并转换为右侧 sheet (RowData.xml):

所以我有一个 xml 文件,其中可能有也可能没有这些数字集。例如,来源 xml 可能如下所示:

Source.xml:

<Data>
    <Number>5556781234</Number>
    <Number>5556781235</Number>
    <Number>5556781236</Number>
</Data>

如你所见,它可以停在任何地方。源 xml 文件可能包含 RowData.xml 中列出的所有数字,也可能只有 1 个或多个。所以我的问题是,如何在我的 xslt 文件中检查它?

我想这样做:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<!-- This is the Excel sheet converted to an XML file -->
<xsl:param name="sheet-uri" select="'RowData.xml'"/>
<xsl:param name="sheet-doc" select="document($sheet-uri)"/>

<xsl:template match="Data">
    <xsl:for-each select="Data/Number">
        <xsl:variable name="continue" select="$sheet-doc//Sheet/Row[Number = current()]/Continue"/>

        <xsl:if test="">
            <!-- Check the Source.xml against the RowData.xml and 
                 see if the set contains any "No"'s in it. -->

            <!-- If it does then don't do the following -->

            <Data2>
                <Number><xsl:value-of select="Number"/></Number>
                <Timestamp>125222</Timestamp>
            </Data2>
        </xsl:if>
    </xsl:for-each>
</xsl:template>

所以基本上,在制作 <Data2> 元素之前,检查 Source.xml 中的数字,看看这些数字中是否有任何数字的值是 NoContinueRowData.xml。我不知道如何制作上面的 if 语句。我知道 xslt 中有一个 contains() 函数;但是,我不知道如何在这里使用它。

这可能吗?如果有任何混淆,请告诉我。提前致谢!

check the numbers in Source.xml and see if any of those numbers have a value of No for the column Continue in RowData.xml.

您可以在此处利用 XSLT 的 "existential equal" 运算符:

test="doc('source.xml')/Data/Number = 
    $sheet-doc//Sheet/Row[Continue='No']/Number"

本质上,如果 A 和 B 是值集,则 A = B returns 如果 A 中的某个值等于 B 中的某个值,则为真。

我建议您使用 key 机制 - 特别是。如果您使用的是 XSLT 2.0.

定义一个键为:

<xsl:key name="row" match="Row" use="Number" />

然后做:

<xsl:template match="/Data">
    <xsl:for-each select="Number[not(key('row', ., $sheet-doc))]">
        <Data2>
            <xsl:copy-of select="."/>
            <Timestamp>125222</Timestamp>
        </Data2>
    </xsl:for-each>
</xsl:template>

这仅选择 NumberRowData.xml 文档中没有相应 Row 的元素。