XSL - 列出等效实现
我想知道 XSL 2.0 中是否有类似于 Java 中的 List 的东西。我想递归调用模板 10 次并传递名为“mylist”的输入变量。在模板中,我想要执行诸如将项目添加到列表、从列表中删除项目、迭代列表中的项目等操作。我可以看到类似“序列”的内容,但我不确定它是否可以用于添加、删除、迭代等。请分享您的想法来实现这一点。
我尝试在以下响应的帮助下使用序列,我面临一些语法问题,例如声明空序列。我想使用 insert-before 或 concat 序列函数打印序列 1 2 3 4 5 6 7 8 9 10。请帮我修复语法。
<xsl:stylesheet version="2.0"
xmlns:locator="http://ntr.lxnx.org"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:variable name="output">
<xsl:call-template name="calculate-data">
<xsl:with-param
name="sequence"
select=""/>
<xsl:with-param
name="count"
select="1"/>
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="output"></xsl:value-of>
</xsl:template>
<xsl:variable name="main-root" as="document-node()" select="/"/>
<xsl:template name="calculate-data">
<xsl:param name="sequence"/>
<xsl:param name="count" select="0"/>
<xsl:if test="$count != 10">
fn:insert-before($count as item()*,0 as xs:integer,$sequence as item()*)
<xsl:call-template name="calculate-data">
<xsl:with-param
name="sequence"
select="$sequence"/>
<xsl:with-param
name="count"
select="$count + 1"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
I would like to know if we have something in XSL 2.0, equivalent to a List in Java. I would like to recursively call a template 10 times and pass a input variable with name 'mylist'. Within the template, I want to do operations like adding item to list, removing item from list, iterating over items within the list etc. I could see something like 'sequence' but i am not sure if it can be used to add, remove, iterate etc. Please share your ideas to implement this.
I tried using sequence with the help of the below reponse, I face some issues with syntax, like declaring an empty sequence. I want to print the sequence 1 2 3 4 5 6 7 8 9 10, using the insert-before or concat sequnce functions. Please help me fix the syntax.
<xsl:stylesheet version="2.0"
xmlns:locator="http://ntr.lxnx.org"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:variable name="output">
<xsl:call-template name="calculate-data">
<xsl:with-param
name="sequence"
select=""/>
<xsl:with-param
name="count"
select="1"/>
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="output"></xsl:value-of>
</xsl:template>
<xsl:variable name="main-root" as="document-node()" select="/"/>
<xsl:template name="calculate-data">
<xsl:param name="sequence"/>
<xsl:param name="count" select="0"/>
<xsl:if test="$count != 10">
fn:insert-before($count as item()*,0 as xs:integer,$sequence as item()*)
<xsl:call-template name="calculate-data">
<xsl:with-param
name="sequence"
select="$sequence"/>
<xsl:with-param
name="count"
select="$count + 1"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
澄清序列的实例与 XPath/XSLT 中的其他任何内容一样都是不可变的,答案是肯定的:
迭代序列:
将项目添加到序列(生成一个作为此操作结果的新序列):
.
3. 两个序列的串联(产生一个新序列即此操作的结果):.
.4。 从序列中删除项目:.
.5。 从序列中提取子序列:还有许多更有用的序列上的标准 XPath 2.0 函数。
注意:XPath 2.0 序列唯一不具备的功能是“嵌套性”。序列始终是“平坦的”,并且序列中的一项不能是序列本身。有多种方法可以模拟多级序列——例如,一个项目可以是一个节点,它的子节点可以被视为一个嵌套序列。
更新:以下是如何方便地使用这些函数来解决OP的更新问题:
当此XSLT 2.0转换应用于任何XML文档(未使用)时,就会产生想要的结果< /强>:
With the clarification that an instance of a sequence, as anything else in XPath/XSLT are immutable, the answer is positive:
Iterating over a sequence:
Add an item to a sequence (produces a new sequence that is the result of this operation):
.
3. Concatenation of two sequences (produces a new sequence that is the result of this operation):.
.4. Remove an item from a sequence:.
.5. Extract a subsequence from a sequence:And there are many more useful standard XPath 2.0 functions over sequences.
Note: The only feature that the XPath 2.0 sequence doesn't have is "nestedness". A sequence is always "flat" and an item of a sequence cannot be a sequence itself. There are ways to simulate multi-level sequences -- for example, an item can be a node and its children nodes can be regarded as a nested sequence.
Update: Here is how these functions can be used conveniently to solve the OP's updated question:
When this XSLT 2.0 transformation is applied on any XML document (not used), the wanted result is produced: