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>
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: