我有一个 XSL 文件包含与此类似的坐标:
<coordinates>
12345,567,5
56777,444,1
67884,443,7
12345,567,5
<coordinates>
现在我想比较坐标的第一个值和最后一个值(12345,567,5 必须等于最后一个)。如何使用 XSLT 完成此操作?
我已经尝试用 substring() 分割值,但坐标的长度是可变的。
好吧,使用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>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句