Arrays 使用XSLT1.0的数组
下面是一个简单的数组Arrays 使用XSLT1.0的数组,arrays,xml,xslt,xslt-1.0,Arrays,Xml,Xslt,Xslt 1.0,下面是一个简单的数组 <xsl:template match="/"> <xsl:variable name="array"> <Item>One</Item> <Item>Two</Item> <Item>Three</Item> </xsl:variable> <htm
<xsl:template match="/">
<xsl:variable name="array">
<Item>One</Item>
<Item>Two</Item>
<Item>Three</Item>
</xsl:variable>
<html>
<body>
<xsl:value-of select="$array"/>
</body>
</html>
</xsl:template>
然而,当我尝试
<xsl:value-of select="$array[0]"/>
整页都被打断了
知道如何访问数组中的“一”吗?首先,XSLT1.0中没有数组。您的变量是一个包含3个子元素的变量 无法直接解析结果树片段;您需要首先使用处理器支持的扩展函数将它们转换为节点集,例如:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<xsl:variable name="array">
<Item>One</Item>
<Item>Two</Item>
<Item>Three</Item>
</xsl:variable>
<html>
<body>
<xsl:value-of select="exsl:node-set($array)/Item[1]"/>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
一个
两个
三
或者,您可以通过直接解析样式表绕过限制:
<xsl:template match="/">
<xsl:variable name="array">
<Item>One</Item>
<Item>Two</Item>
<Item>Three</Item>
</xsl:variable>
<html>
<body>
<xsl:value-of select="document('')//xsl:variable[@name='array']/Item[1]"/>
</body>
</html>
</xsl:template>
一个
两个
三
请注意,节点编号从1开始,而不是从0开始
<xsl:template match="/">
<xsl:variable name="array">
<Item>One</Item>
<Item>Two</Item>
<Item>Three</Item>
</xsl:variable>
<html>
<body>
<xsl:value-of select="document('')//xsl:variable[@name='array']/Item[1]"/>
</body>
</html>
</xsl:template>