xslt检查标记中的第一个和最后一个值是否相等

时间:2017-06-30 08:35:17

标签: xslt substring

我有一个XSL文件包含类似于此的坐标:

<coordinates>
    12345,567,5
    56777,444,1
    67884,443,7
    12345,567,5
<coordinates>

现在我想比较坐标的第一个值和最后一个值(12345,567,5必须等于最后一个)。如何使用XSLT实现这一目标?

我已经尝试用substring()拆分值,但坐标的长度是可变的。

1 个答案:

答案 0 :(得分:0)

好吧,使用tokenize查找行,然后比较:

<xsl:template match="coordinates">
    <xsl:variable name="lines" select="tokenize(., '\r?\n')[normalize-space()]"/>
    <xsl:copy>
        <xsl:sequence select="$lines[1] = $lines[last()]"/>
    </xsl:copy>
</xsl:template>

所以例如

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

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="coordinates">
        <xsl:variable name="lines" select="tokenize(., '\r?\n')[normalize-space()]"/>
        <xsl:copy>
            <xsl:sequence select="$lines[1] = $lines[last()]"/>
        </xsl:copy>
    </xsl:template>

</xsl:transform>

例如

<body>
<coordinates>
    12345,567,5
    56777,444,1
    67884,443,7
    12345,567,5
</coordinates>
<coordinates>
    12345,567,5
    56777,444,1
    67884,443,7
    12345,567,6
</coordinates>
</body>

输出

<body>
<coordinates>true</coordinates>
<coordinates>false</coordinates>
</body>

http://xsltransform.net/ehVYZN3/1