XML/XSLT stylesheet/Variable

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

Assign value from variable to attribute

File: Data.xml
<poem>
   <author>author 1</author>
   <date>1912</date>
   <title>Song</title>
  <stanza>
      <line>line 1</line>
      <line>line 2</line>
      <line>line 3</line>
      <line>line 4</line>
   </stanza>
</poem>

File: Transform.xml
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="2.0">
  <xsl:template match="poem">
    <poem>
      <xsl:copy-of select="title, author, date" />
      <xsl:apply-templates select="stanza" />
    </poem>
  </xsl:template>
  <xsl:template match="stanza">
    <xsl:variable name="file"
      select="concat("verse", string(position()), ".xml")" />
    <verse number="{position()}" href="{$file}" />
    <xsl:result-document href="{$file}">
      <xsl:copy-of select="." />
    </xsl:result-document>
  </xsl:template>
</xsl:stylesheet>



Compare variable

File: Data.xml
<sample>
    <line lid="u1">hello</line>
    <line color="red" lid="u2">hello</line>
    <line color="blue" lid="u3">hello</line>
    <line lid="u4">hello there</line>
    <line color="blue" lid="u5">hello there</line>
    <line color="blue" lid="u6">hello</line>
</sample>
File: Transform.xslt
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:output method="xml" omit-xml-declaration="yes" />
  <xsl:template match="line">
    <xsl:variable name="contents" select="." />
    <xsl:variable name="colorVal" select="@color" />
    <xsl:if test="not(preceding::line[(. = $contents) and (@color = $colorVal)])">
      <xsl:copy>
        <xsl:apply-templates select="@*|node()" />
      </xsl:copy>
    </xsl:if>
  </xsl:template>
  
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()" />
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>
Output:
<sample>
    <line lid="u1">hello</line>
    <line color="red" lid="u2">hello</line>
    <line color="blue" lid="u3">hello</line>
    <line lid="u4">hello there</line>
    <line color="blue" lid="u5">hello there</line>
    
</sample>



Define a variable and then use it in for loop

File: Data.xml 
<?xml version="1.0"?>
<programme>
   <opera>
      <title>A</title>
      <composer>Mozart</composer>
      <date>1787</date>
   </opera>
   <composer name="Mozart">
      <fullname>Mozart</fullname>
      <born>1756</born>
      <died>1791</died>
   </composer>
</programme>
File: Transform.xslt
<html xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xsl:version="1.0">
  <body>
    <center>
      <h1>Programme</h1>
      <xsl:for-each select="/programme/composer">
        <h2>
          <xsl:value-of
            select="concat(fullname, " (", born, "-", died, ")")" />
        </h2>
        <xsl:variable name="c" select="." />
        <xsl:for-each
          select="/programme/opera[composer=$c/@name]">
          <p>
            <xsl:value-of select="title" />
          </p>
        </xsl:for-each>
      </xsl:for-each>
    </center>
  </body>
</html>
Output:
<html>
   <body>
      <center>
         <h1>Programme</h1>
         <h2>Mozart (1756-1791)</h2>
         <p>A</p>
      </center>
   </body>
</html>



Define number type variable

File: Data.xml
<story>
  <chapter>
    <title>Chapter 1</title>
    <para>para 1</para>
    <para>item 1</para>
  </chapter>
</story>

File: Transform.xslt
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:output method="xml" omit-xml-declaration="yes" />
  <xsl:template match="chapter">
    <xsl:variable name="chapNum">
      <xsl:number />
    </xsl:variable>
    <xsl:document href="chap{$chapNum}.html">
      <html>
        <body>
          <xsl:apply-templates />
        </body>
      </html>
    </xsl:document>
  </xsl:template>

  <xsl:template match="chapter/title">
    <h1>
      <xsl:apply-templates />
    </h1>
  </xsl:template>

  <xsl:template match="para">
    <p>
      <xsl:apply-templates />
    </p>
  </xsl:template>

</xsl:stylesheet>
Output:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="xml" omit-xml-declaration="yes"/>
  <xsl:template match="chapter">
    <xsl:variable name="chapNum">
      <xsl:number/>
    </xsl:variable>
    <xsl:document href="chap{$chapNum}.html">
      <html>
        <body>
          <xsl:apply-templates/>
        </body>
      </html>
    </xsl:document>
  </xsl:template>

  <xsl:template match="chapter/title">
    <h1>
      <xsl:apply-templates/>
    </h1>
  </xsl:template>

  <xsl:template match="para">
    <p>
      <xsl:apply-templates/>
    </p>
  </xsl:template>

</xsl:stylesheet>



Define variable

File: Data.xml
<?xml version="1.0"?>
<Employees>
  <Person>
     this is a test
  </Person>
</Employees>
File: Transform.xslt
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <html>
      <body>
        <h3>String handling</h3>
        <xsl:variable name="sentence"
          select=""this is a test"" />
        Case-sensitive match against "test":
        <xsl:value-of select="matches($sentence, "test")" />
        <br />
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>
Output:
<html>
   <body>
      <h3>String handling</h3>
              Case-sensitive match against "test":
              true<br></body>
</html>



Define variable and set value

File: Data.xml
<?xml version="1.0"?>
<addressbook>
  <address>
    <name>
      <title>Mr.</title>
      <first-name>Jason</first-name>
      <last-name>Lee</last-name>
    </name>
    <street>1234 Main Street</street>
    <city>New York</city>
    <state>WI</state>
    <zip>12345</zip>
  </address>
</addressbook>

File: Transform.xsl

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:co="http://wbex.ru/xslt">
  <xsl:variable name="co:company-name"
    select=""A Inc"" />
  <xsl:variable name="co:copyright"
    select="concat("Copyright ?", $co:company-name)" />
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
  
    
      Mr.
      Jason
      Lee
    
    1234 Main Street
    New York
    WI
    12345



Fill variable with the returned value from a function

File: Data.xml
<wine grape="Cabernet">
  <winery>shop 1</winery>
  <product>product 1</product>
  <year>1996</year>
  <price>11.99</price>
</wine>
File: Transform.xslt
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:output method="html" />
  <xsl:variable name="baseFontSize" select="8" />
  <xsl:variable name="bodyTextSize" select="concat($baseFontSize+2,"pt")" />
  <xsl:template match="winery">
    <b>
      <font size="{$bodyTextSize}">
        <xsl:apply-templates />
        <xsl:text> </xsl:text>
        <xsl:value-of select="../@grape" />
      </font>
    </b>
    <br />
  </xsl:template>
  <xsl:template match="product">
    <i>
      <font size="{$bodyTextSize}">
        <xsl:apply-templates />
      </font>
    </i>
    <br />
  </xsl:template>
  <xsl:template match="year | price">
    <font size="{$bodyTextSize}">
      <xsl:apply-templates />
    </font>
    <br />
  </xsl:template>
</xsl:stylesheet>
Output:

  <b><font size="10pt">shop 1 Cabernet</font></b><br>
  <i><font size="10pt">product 1</font></i><br>
  <font size="10pt">1996</font><br>
  <font size="10pt">11.99</font><br>



Math calculation

File: Data.xml
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:output method="xml" omit-xml-declaration="yes" />
  <xsl:template match="/">
    <test>
      <xsl:variable name="myVar">10</xsl:variable>
      A.
      <atvtest at1="hello world" />
      B.
      <atvtest at1="3+2+$myVar" />
      C.
      <atvtest at1="{3+2+$myVar}" />
      D.
      <atvtest at1="u{3+2}" />
      E.
      <atvtest at1="yo, substring("hello world",7)" />
      F.
      <atvtest at1="yo, {substring("hello world",7)}" />
    </test>
  </xsl:template>
</xsl:stylesheet>

File: Transform.xslt
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:output method="xml" omit-xml-declaration="yes" />
  <xsl:template match="/">
    <test>
      <xsl:variable name="myVar">10</xsl:variable>
      A.
      <atvtest at1="hello world" />
      B.
      <atvtest at1="3+2+$myVar" />
      C.
      <atvtest at1="{3+2+$myVar}" />
      D.
      <atvtest at1="u{3+2}" />
      E.
      <atvtest at1="yo, substring("hello world",7)" />
      F.
      <atvtest at1="yo, {substring("hello world",7)}" />
    </test>
  </xsl:template>
</xsl:stylesheet>
Output:
<test>
      A.
      <atvtest at1="hello world"/>
      B.
      <atvtest at1="3+2+$myVar"/>
      C.
      <atvtest at1="15"/>
      D.
      <atvtest at1="u5"/>
      E.
      <atvtest at1="yo, substring("hello world",7)"/>
      F.
      <atvtest at1="yo, world"/></test>



number level="any" count="chapter|sect1|sect2|sect3" format="w - " lang="de"

File: Data.xml
<?xml version="1.0"?>
<book>
  <title>XSLT Topics</title>
  <chapter>
    <title>XPath</title>
  </chapter>
  <chapter>
    <title>Stylesheet Basics</title>
  </chapter>
  <chapter>
    <title>Branching and Control Elements</title>
  </chapter>
  <chapter>
    <title>Functions</title>
  </chapter>
</book>

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:template match="book">
    <xsl:for-each select=".//sect2">
      <xsl:number level="any" count="chapter|sect1|sect2|sect3" format="w - " lang="de"/>
        <xsl:value-of select="title"/>
        <xsl:text>&#xA;</xsl:text>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>



number level="any" count="chapter|sect1|sect2|sect3" format="w - " lang="pl"

File: Data.xml
<?xml version="1.0"?>
<book>
  <title>XSLT Topics</title>
  <chapter>
    <title>XPath</title>
    <para>text</para>
  </chapter>
  <chapter>
    <title>Stylesheet Basics</title>
    <para>text</para>
  </chapter>
  <chapter>
    <title>Branching and Control Elements</title>
    <para>text</para>
  </chapter>
</book>
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:template match="book">
    <xsl:for-each select=".//sect2">
      <xsl:number level="any" count="chapter|sect1|sect2|sect3" format="w - " lang="pl"/>
        <xsl:value-of select="title"/>
        <xsl:text>&#xA;</xsl:text>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>



number level="any" count="chapter|sect1|sect2|sect3" format="Ww - " ordinal="yes"

File: Data.xml
<?xml version="1.0"?>
<!-- chapters.xml -->
<book>
  <title>XSLT Topics</title>
  <chapter>
    <title>XPath</title>
  </chapter>
  <chapter>
    <title>Stylesheet Basics</title>
  </chapter>
  <chapter>
    <title>Branching and Control Elements</title>
  </chapter>
  <chapter>
    <title>Functions</title>
  </chapter>
</book>
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:template match="book">
    <xsl:for-each select=".//sect2">
      <xsl:number level="any" count="chapter|sect1|sect2|sect3" format="Ww - " ordinal="yes"/>
        <xsl:value-of select="title"/>
        <xsl:text>&#xA;</xsl:text>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>



Output variable

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:acme="http://wbex.ru/xslt" exclude-result-prefixes="acme">
  <xsl:output encoding="iso-8859-1" indent="yes" />
  <xsl:variable name="wbex:company-name"
    select=""A Limited"" />
  <xsl:template match="/">
    <c>
      <xsl:value-of select="$wbex:company-name" />
    </c>
  </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="iso-8859-1"?>
<c>A Limited</c>



Reference variable three times

File: Data.xml

<wine grape="Cabernet">
  <winery>shop 1</winery>
  <product>product 1</product>
  <year>1996</year>
  <price>11.99</price>
</wine>
File: Transform.xslt
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:output method="html" />
  <xsl:param name="bodyTextSize">10pt</xsl:param>
  <xsl:template match="winery">
    <b>
      <font size="{$bodyTextSize}">
        <xsl:apply-templates />
        <xsl:text> </xsl:text>
        <xsl:value-of select="../@grape" />
      </font>
    </b>
    <br />
  </xsl:template>
  <xsl:template match="product">
    <i>
      <font size="{$bodyTextSize}">
        <xsl:apply-templates />
      </font>
    </i>
    <br />
  </xsl:template>
  <xsl:template match="year | price">
    <font size="{$bodyTextSize}">
      <xsl:apply-templates />
    </font>
    <br />
  </xsl:template>
</xsl:stylesheet>
Output:

  <b><font size="10pt">shop 1 Cabernet</font></b><br>
  <i><font size="10pt">product 1</font></i><br>
  <font size="10pt">1996</font><br>
  <font size="10pt">11.99</font><br>



substring function with variable

File: Data.xml
<test>
  <color>red</color>
  <color>blue</color>
  <color>yellow</color>
</test>
File: Transform.xslt
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:output omit-xml-declaration="yes" />
  <xsl:variable name="fieldWidth">12</xsl:variable>
  <xsl:template match="color">
    <xsl:variable name="valueLength" select="string-length(.)" />
    <xsl:variable name="padding" select="$fieldWidth - $valueLength" />
    <xsl:value-of select="substring("                  ",1,$padding)" />
    <xsl:value-of select="." />
  </xsl:template>
</xsl:stylesheet>
Output:

           red
          blue
        yellow



Thai numbering

File: Data.xml
<?xml version="1.0"?>
<book>
  <title>XSLT Topics</title>
  <chapter>
    <title>XPath</title>
  </chapter>
  <chapter>
    <title>Stylesheet Basics</title>
  </chapter>
  <chapter>
    <title>Branching and Control Elements</title>
  </chapter>
</book>
File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet version="2.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html"/>
  <xsl:template match="book">
    <html>
      <head>
        <title>Thai numbering</title>
      </head>
      <body style="font-family: sans-serif;">
        <h1>Thai numbering</h1>
        <p style="font-size: 150%">
          <xsl:for-each select=".//sect2">
            <xsl:number level="any" count="sect2"
              format="&#x0E51; "/>
            <xsl:value-of select="title"/>
            <br/>
          </xsl:for-each>
        </p>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>
Output:
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>Thai numbering</title>
   </head>
   <body style="font-family: sans-serif;">
      <h1>Thai numbering</h1>
      <p style="font-size: 150%"></p>
   </body>
</html>



Use constant value from variable

File: Data.xml
<wine grape="Cabernet">
  <winery>shop 1</winery>
  <product>product 1</product>
  <year>1996</year>
  <price>11.99</price>
</wine>
File: Transform.xslt
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:output method="html" />
  
  <xsl:variable name="bodyTextSize">10pt</xsl:variable>
  <xsl:template match="winery">
    <b>
      <font size="{$bodyTextSize}">
        <xsl:apply-templates />
        <xsl:text> </xsl:text>
        <xsl:value-of select="../@grape" />
      </font>
    </b>
    <br />
  </xsl:template>
  <xsl:template match="product">
    <i>
      <font size="{$bodyTextSize}">
        <xsl:apply-templates />
      </font>
    </i>
    <br />
  </xsl:template>
  <xsl:template match="year | price">
    <font size="{$bodyTextSize}">
      <xsl:apply-templates />
    </font>
    <br />
  </xsl:template>
  
</xsl:stylesheet>
Output:

  <b><font size="10pt">shop 1 Cabernet</font></b><br>
  <i><font size="10pt">product 1</font></i><br>
  <font size="10pt">1996</font><br>
  <font size="10pt">11.99</font><br>



Use for-each to loop through variable

File: Data.xml
<?xml version="1.0" encoding="iso-8859-1"?>
<booklist>
  <book category="S">
    <title>title 1</title>
    <author>author 1</author>
  </book>
</booklist>

File: Transform.xslt
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="2.0">
  <xsl:key name="biog" match="author" use="@name" />
  <xsl:variable name="biogs" select="document("authors.xml")" />
  <xsl:template match="/">
    <html>
      <body>
        <xsl:variable name="all-books" select="//book" />
        <xsl:for-each select="$all-books">
          <h1>
            <xsl:value-of select="title" />
          </h1>
          <h2>
            Author
            <xsl:if test="count(author)!=1">s</xsl:if>
          </h2>
          <xsl:for-each select="author">
            <!-- for each author of this book -->
            <xsl:variable name="name" select="." />
            <h3>
              <xsl:value-of select="$name" />
            </h3>
            <!--locate the biography by key lookup -->
            <xsl:variable name="auth"
              select="$biogs/key("biog", $name)" />
            <p>
              <xsl:value-of
                select="concat($auth/born, " - ", $auth/died)" />
            </p>
            <p>
              <xsl:value-of select="$auth/biog" />
            </p>
          </xsl:for-each>
        </xsl:for-each>
      </body>
    </html>
  </xsl:template>
</xsl:transform>
Output:
<html>
   <body>
      <h1>title 1</h1>
      <h2>
                     Author
                     
      </h2>
      <h3>author 1</h3>
      <p> - </p>
      <p></p>
   </body>
</html>



value-of and math calculation

File: Data.xml
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:output method="xml" omit-xml-declaration="yes" />
  <xsl:template match="/">
    <xsl:variable name="myVar">10</xsl:variable>
    <test>
      A.
      <xsl:value-of select="3+2+$myVar" />
      B.
      <xsl:value-of select="substring("hello world",7)" />
    </test>
  </xsl:template>
</xsl:stylesheet>

File: Transform.xslt
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:output method="xml" omit-xml-declaration="yes" />
  <xsl:template match="/">
    <xsl:variable name="myVar">10</xsl:variable>
    <test>
      A.
      <xsl:value-of select="3+2+$myVar" />
      B.
      <xsl:value-of select="substring("hello world",7)" />
    </test>
  </xsl:template>
</xsl:stylesheet>
Output:
<test>
      A.
      15
      B.
      world</test>



Variable scope

File: Data.xml

<wine grape="Cabernet">
  <winery>shop 1</winery>
  <product>product 1</product>
  <year>1996</year>
  <price>11.99</price>
</wine>
File: Transform.xslt
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:output method="html" />
  <xsl:template match="wine">
    <xsl:variable name="bodyTextSize">10pt</xsl:variable>
    <xsl:apply-templates select="winery" />
    <i>
      <font size="{$bodyTextSize}">
        <xsl:apply-templates select="product" />
      </font>
    </i>
    <br />
    <font size="{$bodyTextSize}">
      <xsl:apply-templates select="year" />
    </font>
    <br />
    <font size="{$bodyTextSize}">
      <xsl:apply-templates select="price" />
    </font>
    <br />
  </xsl:template>
  <xsl:template match="winery">
    <xsl:variable name="bodyTextSize">12pt</xsl:variable>
    <b>
      <font size="{$bodyTextSize}">
        <xsl:apply-templates />
        <xsl:text> </xsl:text>
        <xsl:value-of select="../@grape" />
      </font>
    </b>
    <br />
  </xsl:template>
</xsl:stylesheet>
Output:
<b><font size="12pt">shop 1 Cabernet</font></b><br><i><font size="10pt">product 1</font></i><br><font size="10pt">1996</font><br><font size="10pt">11.99</font><br>



Variable with number type

File: Data.xml
<?xml version="1.0"?>
<book>
  <title>XSLT Topics</title>
  <chapter>
    <title>XPath</title>
  </chapter>
  <chapter>
    <title>Stylesheet Basics</title>
  </chapter>
  <chapter>
    <title>Branching and Control Elements</title>
  </chapter>
  <chapter>
    <title>Functions</title>
  </chapter>
</book>
File: Transform.xslt

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>
  <xsl:template match="book">
    <xsl:for-each select="chapter|.//sect1|.//sect2|.//sect3">
      <xsl:variable name="value1">
        <xsl:number level="any" count="chapter|sect1|sect2|sect3"/>
      </xsl:variable>
      <xsl:number value="$value1 + 999"
        grouping-separator="," grouping-size="3"/>
      <xsl:text>. </xsl:text>
      <xsl:value-of select="title"/>
      <xsl:text>&#xA;</xsl:text>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>
Output:
1,000. XPath
1,001. Stylesheet Basics
1,002. Branching and Control Elements
1,003. Functions