XML/XSLT stylesheet/Variable

Материал из Web эксперт
Версия от 11:26, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Assign value from variable to attribute

   <source lang="xml">

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>

</source>
   
  


Compare variable

   <source lang="xml">

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>

</source>
   
  


Define a variable and then use it in for loop

   <source lang="xml">

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>

Programme

     <xsl:for-each select="/programme/composer">

<xsl:value-of select="concat(fullname, " (", born, "-", died, ")")" />

       <xsl:variable name="c" select="." />
       <xsl:for-each
         select="/programme/opera[composer=$c/@name]">

<xsl:value-of select="title" />

       </xsl:for-each>
     </xsl:for-each>
 </body>

</html> Output: <html>

  <body>

Programme

Mozart (1756-1791)

A

  </body>

</html>

</source>
   
  


Define number type variable

   <source lang="xml">

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

<xsl:apply-templates />

 </xsl:template>
 <xsl:template match="para">

<xsl:apply-templates />

 </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">

<xsl:apply-templates/>

 </xsl:template>
 <xsl:template match="para">

<xsl:apply-templates/>

 </xsl:template>

</xsl:stylesheet>

</source>
   
  


Define variable

   <source lang="xml">

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>

String handling

       <xsl:variable name="sentence"
         select=""this is a test"" />
       Case-sensitive match against "test":
       <xsl:value-of select="matches($sentence, "test")" />
       
</body> </html> </xsl:template>

</xsl:stylesheet> Output: <html>

  <body>

String handling

             Case-sensitive match against "test":
             true
</body>

</html>

</source>
   
  


Define variable and set value

   <source lang="xml">

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
 
</source>
   
  


Fill variable with the returned value from a function

   <source lang="xml">

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">
   
     
       <xsl:apply-templates />
       <xsl:text> </xsl:text>
       <xsl:value-of select="../@grape" />
     
   
   
</xsl:template> <xsl:template match="product"> <xsl:apply-templates />
</xsl:template> <xsl:template match="year | price"> <xsl:apply-templates />
</xsl:template>

</xsl:stylesheet> Output:

 shop 1 Cabernet
product 1
1996
11.99
</source>


Math calculation

   <source lang="xml">

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>
</source>
   
  


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

   <source lang="xml">

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>
</xsl:text>
   </xsl:for-each>
 </xsl:template>

</xsl:stylesheet>

</source>
   
  


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

   <source lang="xml">

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>
</xsl:text>
   </xsl:for-each>
 </xsl:template>

</xsl:stylesheet>

</source>
   
  


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

   <source lang="xml">

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="Ww - " ordinal="yes"/>
       <xsl:value-of select="title"/>
       <xsl:text>
</xsl:text>
   </xsl:for-each>
 </xsl:template>

</xsl:stylesheet>

</source>
   
  


Output variable

   <source lang="xml">

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

</source>
   
  


Reference variable three times

   <source lang="xml">

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">
   
     
       <xsl:apply-templates />
       <xsl:text> </xsl:text>
       <xsl:value-of select="../@grape" />
     
   
   
</xsl:template> <xsl:template match="product"> <xsl:apply-templates />
</xsl:template> <xsl:template match="year | price"> <xsl:apply-templates />
</xsl:template>

</xsl:stylesheet> Output:

 shop 1 Cabernet
product 1
1996
11.99
</source>


substring function with variable

   <source lang="xml">

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
</source>
   
  


Thai numbering

   <source lang="xml">

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;">

Thai numbering

<xsl:for-each select=".//sect2"> <xsl:number level="any" count="sect2" format="๑ "/> <xsl:value-of select="title"/>
</xsl:for-each>

     </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;">

Thai numbering

  </body>

</html>

</source>
   
  


Use constant value from variable

   <source lang="xml">

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">
   
     
       <xsl:apply-templates />
       <xsl:text> </xsl:text>
       <xsl:value-of select="../@grape" />
     
   
   
</xsl:template> <xsl:template match="product"> <xsl:apply-templates />
</xsl:template> <xsl:template match="year | price"> <xsl:apply-templates />
</xsl:template>

</xsl:stylesheet> Output:

 shop 1 Cabernet
product 1
1996
11.99
</source>


Use for-each to loop through variable

   <source lang="xml">

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

<xsl:value-of select="title" />

Author <xsl:if test="count(author)!=1">s</xsl:if>

         <xsl:for-each select="author">
           
           <xsl:variable name="name" select="." />

<xsl:value-of select="$name" />

           <xsl:variable name="auth"
             select="$biogs/key("biog", $name)" />

<xsl:value-of select="concat($auth/born, " - ", $auth/died)" />

<xsl:value-of select="$auth/biog" />

         </xsl:for-each>
       </xsl:for-each>
     </body>
   </html>
 </xsl:template>

</xsl:transform> Output: <html>

  <body>

title 1

Author

author 1

-

  </body>

</html>

</source>
   
  


value-of and math calculation

   <source lang="xml">

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>
</source>
   
  


Variable scope

   <source lang="xml">

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" />
   
     
       <xsl:apply-templates select="product" />
     
   
   
<xsl:apply-templates select="year" />
<xsl:apply-templates select="price" />
</xsl:template> <xsl:template match="winery"> <xsl:variable name="bodyTextSize">12pt</xsl:variable> <xsl:apply-templates /> <xsl:text> </xsl:text> <xsl:value-of select="../@grape" />
</xsl:template>

</xsl:stylesheet> Output: shop 1 Cabernet
product 1
1996
11.99

</source>
   
  


Variable with number type

   <source lang="xml">

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

</source>