XML/XSLT stylesheet/subsequence

Материал из Web эксперт
Перейти к: навигация, поиск

A list of sequence

File: Data.xml

File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet version="2.0" 
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>
  <xsl:template match="/">
    <xsl:variable name="cities" as="xs:string*">
      <xsl:sequence select="addressbook/address/city"/>
      <xsl:sequence select="("A", "B", "C")"/>
      <xsl:sequence select="("D", "E", "F")"/>
    </xsl:variable>
    <xsl:text>Our customers live in these cities:&#xA;&#xA;</xsl:text>
    <xsl:value-of select="$cities" separator="&#xA;"/>
  </xsl:template>
</xsl:stylesheet>
Output:
Our customers live in these cities:
A
B
C
D
E
F



Define sequence

File: Data.xml

File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet version="2.0" 
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>
  <xsl:template match="/">
    <xsl:variable name="months" as="xs:string*">
      <xsl:sequence 
        select="("January", "February", "March", "April", 
                "May", "June", "July", "August", 
                "September", "October", "November", 
                "December")"/>
    </xsl:variable>
    <xsl:text>Here are the months of the year:&#xA;&#xA;</xsl:text>
    <xsl:value-of select="$months" separator="&#xA;"/>
  </xsl:template>
</xsl:stylesheet>
Output:
Here are the months of the year:
January
February
March
April
May
June
July
August
September
October
November
December



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