subsequence() function : subsequence « XSLT stylesheet « XML






subsequence() function


File: Data.xml

<?xml version="1.0" encoding="utf-8"?>
<favorite-books>
  <booklist>
    <book isbn="1111111111" 
      favorite="f1">XSLT</book>
  </booklist>
</favorite-books>

File: Transform.xslt


<?xml version="1.0"?>
<xsl:stylesheet version="2.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  
  <xsl:output method="text"/>
 
  <xsl:variable name="Dougs-favorites" as="node()*">
    <xsl:sequence 
      select="/favorite-books/booklist
              /book[contains(@favorite, 'Doug')]"/>
  </xsl:variable>

  <xsl:template match="/">
    <xsl:text>A test of the node-after (>>) operator:</xsl:text>
    <xsl:text>&#xA;&#xA;  Comparing nodes from </xsl:text>
    <xsl:text>the sequence:&#xA;</xsl:text>

    <xsl:value-of 
      select="if (subsequence($Dougs-favorites, 1, 1) >>
                  subsequence($Dougs-favorites, 2, 1))
              then '    node1 >> node2 = true&#xA;'
              else '    node1 >> node2 = false&#xA;'"/>
    <xsl:value-of 
      select="if (subsequence($Dougs-favorites, 2, 1) >>
                  subsequence($Dougs-favorites, 1, 1))
              then '    node2 >> node1 = true&#xA;'
              else '    node2 >> node1 = false&#xA;'"/>
    <xsl:value-of 
      select="if (subsequence($Dougs-favorites, 1, 1) >>
                  subsequence($Dougs-favorites, 1, 1))
              then '    node1 >> node1 = true&#xA;'
              else '    node1 >> node1 = false&#xA;'"/>
  </xsl:template>

</xsl:stylesheet>

Output:

A test of the node-after (>>) operator:

  Comparing nodes from the sequence:
    node1 >> node2 = false
    node2 >> node1 = false
    node1 >> node1 = false

 








Related examples in the same category

1.Define sequence
2.A list of sequence