XSLT:在 select 路径中使用 position()
XSLT: Using position() in select path
我正在尝试使用位置来控制 select,但它似乎只在使用文字整数时才有效。我创建了一个非常精简的示例,它跳过了很多其他上下文,所以虽然看起来我这样做效率低下,但这是有充分理由的。
输入:
<container xmlns:xlink="http://www.w3.org/1999/xlink">
<item type="a" xlink:href="a1.xxx"/>
<item type="b" xlink:href="b1.jpg"/>
<item type="c" xlink:href="c1.jpg"/>
<item type="a" xlink:href="a2.xxx"/>
<item type="b" xlink:href="b2.jpg"/>
<item type="c" xlink:href="c2.jpg"/>
<assocData type="typeOne" xlink:href="objectOne"/>
<assocData type="typeTwo" xlink:href="objectThree"/>
<assocData type="typeOne" xlink:href="objectTwo"/>
<assocData type="typeTwo" xlink:href="objectFour"/>
</container>
XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="container">
<xsl:for-each select="item[@type='b']">
<container>
<xsl:variable name="position">
<xsl:value-of select="position()"/>
</xsl:variable>
<xsl:copy-of select="../assocData[@type='typeOne'][$position]"/>
</container>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
期望的输出:
<container>
<assocData type="typeOne" xlink:href="objectOne"/>
</container>
<container>
<assocData type="typeOne" xlink:href="objectTwo"/>
</container>
实际输出:
<container>
<assocData type="typeOne" xlink:href="objectOne"/>
<assocData type="typeOne" xlink:href="objectTwo"/>
</container>
<container>
<assocData type="typeOne" xlink:href="objectOne"/>
<assocData type="typeOne" xlink:href="objectTwo"/>
</container>
如果我用文字 1 或 2 替换“$position”,则输出中的每个容器仅包含一个 assocData 标记。我不确定为什么包含位置的变量的行为不同。
改变
<xsl:variable name="position">
<xsl:value-of select="position()"/>
</xsl:variable>
至
<xsl:variable name="position" select="position()"/>
在那种情况下,变量是整数类型,而在您的情况下,它是一个结果树片段,其中包含一个恰好是数值的文本节点。
我正在尝试使用位置来控制 select,但它似乎只在使用文字整数时才有效。我创建了一个非常精简的示例,它跳过了很多其他上下文,所以虽然看起来我这样做效率低下,但这是有充分理由的。
输入:
<container xmlns:xlink="http://www.w3.org/1999/xlink">
<item type="a" xlink:href="a1.xxx"/>
<item type="b" xlink:href="b1.jpg"/>
<item type="c" xlink:href="c1.jpg"/>
<item type="a" xlink:href="a2.xxx"/>
<item type="b" xlink:href="b2.jpg"/>
<item type="c" xlink:href="c2.jpg"/>
<assocData type="typeOne" xlink:href="objectOne"/>
<assocData type="typeTwo" xlink:href="objectThree"/>
<assocData type="typeOne" xlink:href="objectTwo"/>
<assocData type="typeTwo" xlink:href="objectFour"/>
</container>
XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="container">
<xsl:for-each select="item[@type='b']">
<container>
<xsl:variable name="position">
<xsl:value-of select="position()"/>
</xsl:variable>
<xsl:copy-of select="../assocData[@type='typeOne'][$position]"/>
</container>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
期望的输出:
<container>
<assocData type="typeOne" xlink:href="objectOne"/>
</container>
<container>
<assocData type="typeOne" xlink:href="objectTwo"/>
</container>
实际输出:
<container>
<assocData type="typeOne" xlink:href="objectOne"/>
<assocData type="typeOne" xlink:href="objectTwo"/>
</container>
<container>
<assocData type="typeOne" xlink:href="objectOne"/>
<assocData type="typeOne" xlink:href="objectTwo"/>
</container>
如果我用文字 1 或 2 替换“$position”,则输出中的每个容器仅包含一个 assocData 标记。我不确定为什么包含位置的变量的行为不同。
改变
<xsl:variable name="position">
<xsl:value-of select="position()"/>
</xsl:variable>
至
<xsl:variable name="position" select="position()"/>
在那种情况下,变量是整数类型,而在您的情况下,它是一个结果树片段,其中包含一个恰好是数值的文本节点。