xml 在 xslt 中创建数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3299938/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
creating arrays in xslt
提问by gary A.K.A. G4
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 中创建和使用数组吗?如果是这样,是否有合适的在线示例可供学习?如果没有,有没有办法以模仿数组的方式存储值?
采纳答案by gary A.K.A. G4
With XSLT 2.0 you can model any data type you want to.
使用 XSLT 2.0,您可以为您想要的任何数据类型建模。
As example:
例如:
<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>
With any input, output:
对于任何输入,输出:
B
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 中没有临时结果树数据类型。有一种不允许节点集运算符的结果树片段数据类型。因此,唯一的方法是使用扩展功能:在本例中node-set()来自 EXSLT(MSXSL 也有一个内置node-set()扩展)。
So, in XSLT 1.0 without extensions you can have only inline data model, or by params or by external document. As example:
因此,在没有扩展的 XSLT 1.0 中,您只能拥有内联数据模型、参数或外部文档。例如:
<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>
Result, with any input:
结果,任何输入:
B
Only if you want to, I can provide you a XSLT 1.0 plus extensions example (It's not standar...)
如果您愿意,我可以为您提供 XSLT 1.0 plus 扩展示例(这不是标准的...)
回答by Dimitre Novatchev
The XPath 2.0 sequence (available in XSLT 2+) is the closest thing to an array:
XPath 2.0 序列(在 XSLT 2+ 中可用)是最接近数组的东西:
(1 to 10)[3]
evaluates to 3
评估为 3
('a', 'b', 'a', 'c')[3]
evaluates to 'a'
评估为 'a'
The items of a sequence can be of any conceivable type allowed in XPath, with the exception of sequence itself -- nested sequences are not allowed.
序列的项可以是 XPath 中允许的任何可能的类型,但序列本身除外——不允许嵌套序列。
Do note: Sequences are not the same as arrays:
请注意:序列与数组不同:
Sequences are immutable. Any updating operation on a sequence (appending or prepending an item, inserting an item or removing an item) produces a new sequence.
The access time to the n-th item is not guaranteed to be O(1)as this is for arrays, and may be O(n).
序列是不可变的。对序列的任何更新操作(附加或预先添加一个项目、插入一个项目或删除一个项目)都会产生一个新的序列。
对第 n 项的访问时间不保证为 O(1),因为这是针对数组的,并且可能为 O(n)。
回答by Oded
No, not as such. The closest concept is node-sets, which are collections of nodes. Whenever the result of a select is a number of nodes, you get a node-set. These can be accessed with a index notation (starting with 1), so the first element of the node-set can be accessed with notation such as selectedNodes[1].
不,不是这样。最接近的概念是节点集,它是节点的集合。Whenever the result of a select is a number of nodes, you get a node-set. 这些可以用索引符号(从 1 开始)访问,所以节点集的第一个元素可以用诸如selectedNodes[1].
回答by ravthiru
With XSLT 2.0 you can simply use
使用 XSLT 2.0,您可以简单地使用
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:output omit-xml-declaration="yes"/>
<xsl:template match="/">
<xsl:variable name="array" select="('A','B','C')"/>
<xsl:value-of select="$array[2]"/>
</xsl:template>
</xsl:stylesheet>
回答by Adrug
If need filter and foreach. (csv example)
如果需要过滤器和foreach。(csv示例)
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" version="2.0">
<xsl:output method="text" omit-xml-declaration="yes" />
<xsl:variable name="array" as="element()*">
<column name="Company" enable="true">Company</column>
<column name="User" enable="true">User</column>
</xsl:variable>
<xsl:variable name="separator">
<xsl:text>;</xsl:text>
</xsl:variable>
<xsl:variable name="newline">
<xsl:text>
</xsl:text>
</xsl:variable>
<!-- Output the CSV header -->
<xsl:for-each select="msxsl:node-set($array)/column[@enable = 'true']">
<xsl:value-of select="." />
<xsl:if test="position() != last()">
<xsl:value-of select="$separator" />
</xsl:if>
</xsl:for-each>
<xsl:value-of select="$newline" />
<!-- your code inserted row -->
</xsl:stylesheet>

