XML/XSLT stylesheet/sequence

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

Comparing sequences with values

File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<report month="8" year="2006">
  <title>Chocolate bar sales</title>
  <brand>
    <name>name 1</name>
    <units>2</units>
  </brand>
  <brand>
    <name>name 2</name>
    <units>3</units>
  </brand>
</report>

File: Transform.xslt
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema">
  
  <xsl:output method="text"/>
  <xsl:template match="/">
    
    <xsl:value-of select="/report/brand/units" separator=", "/>
    <xsl:text>&#xA;&#xA;      /report/brand/units &gt;  1 : </xsl:text>
    <xsl:value-of select="/report/brand/units &gt; 1"/>
    <xsl:text>&#xA;      /report/brand/units &gt;= 2 : </xsl:text>
    <xsl:value-of select="/report/brand/units &gt;= 2"/>
    <xsl:text>&#xA;      /report/brand/units &lt;  3  : </xsl:text>
    <xsl:value-of select="/report/brand/units &lt; 3"/>
    <xsl:text>&#xA;      /report/brand/units =  1 : </xsl:text>
    <xsl:value-of select="/report/brand/units = 1"/>
    <xsl:text>&#xA;      /report/brand/units =  2 : </xsl:text>
    <xsl:value-of select="/report/brand/units = 3"/>
  </xsl:template>
</xsl:stylesheet>
Output:
2, 3
      /report/brand/units >  1 : true
      /report/brand/units >= 2 : true
      /report/brand/units <  3  : true
      /report/brand/units =  1 : false
      /report/brand/units =  2 : true



list of string type variable

File: Transform.xslt

<?xml version="1.0"?>
<xsl:stylesheet version="2.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xsl:output method="text"/>
 
  <xsl:variable name="English-months" as="xs:string*"
    select="("January", "February", "March", "April", 
             "May", "June", "July", "August", 
             "September", "October", "November", 
             "December")"/>
  <xsl:variable name="German-months" as="xs:string*"
    select="("Januar", "Februar", "M&#xE4;rz", "April", 
             "Mai", "Juni", "Juli", "August", 
             "September", "Oktober", "November", 
             "Dezember")"/>
  <xsl:template match="/">
    <xsl:value-of 
      select="for $m in ($English-months, $German-months) return
                if (starts-with($m, "J")) 
                  then concat ($m, " starts with J!&#xA;")
                  else """
      separator=""/>
  </xsl:template>
</xsl:stylesheet>
Output:
January starts with J!
June starts with J!
July starts with J!
Januar starts with J!
Juni starts with J!
Juli starts with J!



Sequence of node()

File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<favorite-books>
  <booklist>
    <book isbn="1111111111" 
      favorite="f1">XSLT</book>
    <book isbn="2222222222" 
      favorite="Doug">Java</book>
    <book isbn="3333333333" 
      favorite="Doug">C++</book>
    <book isbn="4444444444" 
      favorite="f1">SQL</book>
    <book isbn="5555555555" 
      favorite="Sheri">Oracle</book>
    <book isbn="0375724443" 
      favorite="Sheri">XML</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:variable name="Sheris-favorites" as="node()*">
    <xsl:sequence select="/favorite-books/booklist/book[contains(@favorite, "Sheri")]"/>
  </xsl:variable>
  <xsl:template match="/">
    <xsl:text>&#xA;All the books we like: </xsl:text>
    <xsl:text>&#xA;&#xA;  </xsl:text>
    <xsl:for-each select="$Dougs-favorites union $Sheris-favorites">
      <xsl:sort select="."/>
      <xsl:value-of select="."/>
      <xsl:text>&#xA;  </xsl:text>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>
Output:

All the books we like: 
  XML
  C++
  Java
  Oracle