在 xslt 中创建数组 [英] creating arrays in xslt
问题描述
可以在 xslt 中创建和使用数组吗?如果是这样,是否有合适的在线示例可供学习?如果没有,有没有办法以模仿数组的方式存储值?
can array's be created and used in xslt? If so are there suitable examples online to study? If not is there a way to store values in a way that mimics an array?
推荐答案
使用 XSLT 2.0,您可以建模任何您想要的数据类型.
With XSLT 2.0 you can model any data type you want to.
例如:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" omit-xml-declaration="yes"/>
<xsl:variable name="array" as="element()*">
<Item>A</Item>
<Item>B</Item>
<Item>C</Item>
</xsl:variable>
<xsl:template match="/">
<xsl:value-of select="$array[2]"/>
</xsl:template>
</xsl:stylesheet>
任意输入,输出:
B
在 XSLT 1.0 中没有临时结果树数据类型.有一种不允许节点集运算符的结果树片段数据类型.因此,唯一的方法是使用扩展函数:在这种情况下,来自 EXSLT 的 node-set()
(MSXSL 具有内置的 node-set()
扩展,还有).
In XSLT 1.0 there is not Temporaly Result Tree data type. There is a Result Tree Fragment data type that does not allow node-set operator. So, the only way to go is with extensions functions: in this case node-set()
from EXSLT (MSXSL has a built-in node-set()
extension, also).
因此,在没有扩展的 XSLT 1.0 中,您只能拥有内联数据模型、参数或外部文档.例如:
So, in XSLT 1.0 without extensions you can have only inline data model, or by params or by external document. As example:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" omit-xml-declaration="yes"/>
<xsl:variable name="inline-array">
<Item>A</Item>
<Item>B</Item>
<Item>C</Item>
</xsl:variable>
<xsl:param name="array" select="document('')/*/xsl:variable[@name='inline-array']/*"/>
<xsl:template match="/">
<xsl:value-of select="$array[2]"/>
</xsl:template>
</xsl:stylesheet>
结果,任何输入:
B
如果您愿意,我可以为您提供 XSLT 1.0 plus 扩展示例(它不是标准的...)
Only if you want to, I can provide you a XSLT 1.0 plus extensions example (It's not standar...)
这篇关于在 xslt 中创建数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!