XML Tutorial/XSLT stylesheet/choose

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

Change style for even and odd

   <source lang="xml">

File: Data.xml <?xml version="1.0"?>

<list xml:lang="en">

 <title>title 1</title>
 <listitem>item 1</listitem>
 <listitem>item 2</listitem>
 <listitem>item 3</listitem>
 <listitem xml:lang="sw">item 4</listitem>
 <listitem xml:lang="en-gb">item 5</listitem>
 <listitem xml:lang="zu">item 6</listitem>
 <listitem xml:lang="jz">item 7</listitem>

</list>

File: Transform.xslt <?xml version="1.0"?>

<xsl:stylesheet version="1.0"

 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="html"/>
 <xsl:template match="/">
   <html>
     <head>
       <title>XSLT and CSS Demo</title>
       <style>
         <xsl:comment> 
           p.big   {font-size: 125%; font-weight: bold;} 
           p.odd   {color: purple; font-weight: bold;}
           p.even  {color: blue; font-style: italic; font-weight: bold;}
         </xsl:comment>
       </style>
     </head>
     <body style="font-family: sans-serif;">
       <xsl:apply-templates select="list/title"/>
       <xsl:apply-templates select="list/listitem"/>
     </body>
   </html>
 </xsl:template>
 <xsl:template match="title">

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

 </xsl:template>
 <xsl:template match="listitem">
   <xsl:choose>
     <xsl:when test="position() mod 2">

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

     </xsl:when>
     <xsl:otherwise>

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

     </xsl:otherwise>
   </xsl:choose>
 </xsl:template>
 

</xsl:stylesheet> Output: <html>

  <head>
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     <title>XSLT and CSS Demo</title><style>
        </style></head>
  <body style="font-family: sans-serif;">

title 1

item 1

item 2

item 3

item 4

item 5

item 6

item 7

  </body>

</html></source>


choose statement

   <source lang="xml">

File: Data.xml <?xml version="1.0" encoding="ISO-8859-1"?> <america>

<source>
 <title>F</title>
 <url>http://www.wbex.ru</url>
 <populations estimate="true" year="2002"/>
</source>
<nation>
 <name>USA</name>
 <capital>DC</capital>
 <population>32277942</population>
 <cc>dz</cc>
</nation>

</america> File: Transform.xslt <?xml version="1.0" encoding="US-ASCII"?> <xsl:stylesheet version="1.0"

 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text" />
 <xsl:template match="america">
   <xsl:apply-templates select="nation" />
 </xsl:template>
 <xsl:template match="nation">
   <xsl:choose>
     <xsl:when test="population > 10000000">
       <xsl:value-of select="name" />
       <xsl:text>
</xsl:text>
     </xsl:when>
   </xsl:choose>
 </xsl:template>

</xsl:stylesheet> Output: USA</source>


choose with otherwise statement

   <source lang="xml">

File: Data.xml <?xml version="1.0" encoding="ISO-8859-1"?> <america>

<source>
 <title>A</title>
 <url>http://www.wbex.ru</url>
 <populations estimate="true" year="2002"/>
</source>
<nation>
 <name>USA</name>
 <capital>DC</capital>
 <population>32277942</population>
 <cc>dz</cc>
</nation>

</america>

File: Transform.xslt <?xml version="1.0" encoding="US-ASCII"?> <xsl:stylesheet version="1.0"

 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text" />
 <xsl:template match="america">
   <xsl:apply-templates select="nation" />
 </xsl:template>
 <xsl:template match="nation">
   <xsl:choose>
     <xsl:when test="population = 10000000">
       <xsl:value-of select="name" />
       <xsl:text> = 10M</xsl:text>
       <xsl:text>
</xsl:text>
     </xsl:when>
     <xsl:when test="population = 1000000">
       <xsl:value-of select="name" />
       <xsl:text> = 1M</xsl:text>
       <xsl:text>
</xsl:text>
     </xsl:when>
     <xsl:otherwise>
       <xsl:message terminate="yes">Not found!</xsl:message>
     </xsl:otherwise>
   </xsl:choose>
 </xsl:template>

</xsl:stylesheet></source>


when test="position() mod

   <source lang="xml">

File: Data.xml <?xml version="1.0"?>

<list xml:lang="en">

 <title>title 1</title>
 <listitem>item 1</listitem>
 <listitem>item 2</listitem>
 <listitem>item 3</listitem>
 <listitem xml:lang="sw">item 4</listitem>
 <listitem xml:lang="en-gb">item 5</listitem>
 <listitem xml:lang="zu">item 6</listitem>
 <listitem xml:lang="jz">item 7</listitem>

</list> File: Transform.xslt <?xml version="1.0"?> <xsl:stylesheet version="1.0"

 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="html"/>
 <xsl:template match="/">
   <html>
     <head>
       <title>
         <xsl:value-of select="list/title"/>
       </title>
     </head>
     <body style="font-family: sans-serif; color: white;">

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

<xsl:for-each select="list/listitem"> </xsl:for-each>
               <xsl:attribute name="style">
                 <xsl:choose>
                   <xsl:when test="position() mod 4 = 0">
                     <xsl:text>background: yellow; color: black;</xsl:text>
                   </xsl:when>
                   <xsl:when test="position() mod 4 = 1">
                     <xsl:text>background: blue;</xsl:text>
                   </xsl:when>
                   <xsl:when test="position() mod 4 = 2">
                     <xsl:text>background: white; color: black;</xsl:text>
                   </xsl:when>
                   <xsl:otherwise>
                     <xsl:text>background: black;</xsl:text>
                   </xsl:otherwise>
                 </xsl:choose>
               </xsl:attribute>
               <xsl:value-of select="."/>
     </body>
   </html>
 </xsl:template>

</xsl:stylesheet> Output: <html>

  <head>
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     <title>title 1</title>
  </head>
  <body style="font-family: sans-serif; color: white;">

title 1

item 1
item 2
item 3
item 4
item 5
item 6
item 7
  </body>

</html></source>


xsl:choose: check the value of an attribute

   <source lang="xml">

File: Data.xml <?xml version = "1.0"?> <planner>

  <year value = "2000">
     <date month = "7" day = "15">
        <note time = "1430">meeting</note>
        <note time = "1620">course</note>
     </date>
     <date month = "7" day = "4">
        <note>Independence Day</note>
     </date>
     <date month = "7" day = "20">
        <note time = "0900">Meeting A</note>
     </date>
     <date month = "7" day = "20">
        <note time = "1900">Meeting B</note>
     </date>
     <date month = "7" day = "20">
        <note time = "1300">Meeting C</note>
     </date>
  </year>

</planner>

File: Transform.xslt <?xml version = "1.0"?> <xsl:stylesheet version="1.0"

 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:template match="/">
   <html>
     <body>
       Appointments
       
<xsl:apply-templates select="planner/year" /> </body> </html> </xsl:template> <xsl:template match="year"> Year: <xsl:value-of select="@value" />
<xsl:for-each select="date/note"> <xsl:sort select="../@day" order="ascending" data-type="number" /> Day: <xsl:value-of select="../@day" /> / <xsl:value-of select="../@month" />
<xsl:choose> <xsl:when test="@time > "0500" and @time < "1200""> Morning ( <xsl:value-of select="@time" /> ): </xsl:when> <xsl:when test="@time > "1200" and @time < "1700""> Afternoon ( <xsl:value-of select="@time" /> ): </xsl:when> <xsl:when test="@time > "1200" and @time < "1700""> Evening ( <xsl:value-of select="@time" /> ): </xsl:when> <xsl:when test="@time > "1200" and @time < "1700""> Night ( <xsl:value-of select="@time" /> ): </xsl:when> <xsl:otherwise>Entire day:</xsl:otherwise> </xsl:choose> <xsl:value-of select="." /> <xsl:if test=". = """>n/a</xsl:if>
</xsl:for-each> </xsl:template>

</xsl:stylesheet> Output: <html>

  <body>
             Appointments
             
Year:2000
Day: 4 / 7
Entire day:Independence Day
Day: 15 / 7
Afternoon ( 1430 ): Doctor"s appointment
Day: 15 / 7
Afternoon ( 1620 ): course
Day: 20 / 7
Morning ( 0900 ): Meeting A
Day: 20 / 7
Entire day:Party at Joe"s
Day: 20 / 7
Afternoon ( 1300 ): Meeting C
</body>

</html></source>


xsl:choose element is used for selection between several possibilities

   <source lang="xml">

File: Data.xml <?xml version="1.0" encoding="utf-8"?>

   <SECTION>
     <DATA>I need a pen.
     I need some paper.
     <SUMMARY>I need a pen and some paper</SUMMARY>
   </SECTION>
   <SECTION>
     I need bread.
     I need butter.
   </SECTION>

</data> File: Transform.xslt <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet

     version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:template match="//SECTION">
     <xsl:choose>
       <xsl:when test="SUMMARY">
         <paragraph>
           <xsl:text>SUMMARY: </xsl:text>
           <xsl:value-of select="SUMMARY"/>
         </P>
       </xsl:when>
       <xsl:otherwise>
         <xsl:for-each select="DATA">
           <paragraph>
             <xsl:text>DATA: </xsl:text>
             <xsl:value-of select="."/>
           </P>
         </xsl:for-each>
       </xsl:otherwise>
     </xsl:choose>
   </xsl:template>

</xsl:stylesheet> Output: <?xml version="1.0" encoding="UTF-8"?>

   <paragraph>SUMMARY: I need a pen and some paper</P>
   <paragraph>DATA: I need bread.</P><paragraph>DATA: I need butter.</P></source>
   
  

xsl:choose, xsl:when and xsl:otherwise

   <source lang="xml">

File: Data.xml <?xml version = "1.0" encoding = "UTF-8"?> <planner>

 <year value="2002">
   <date month="7" day="15">
     <note time="1430">meeting</note>
     <note time="1620">course</note>
   </date>
   <date month="7" day="4">
     <note>Independence Day</note>
   </date>
   <date month="7" day="9">
     <note />
   </date>
 </year>

</planner> File: Transform.xslt <?xml version = "1.0" encoding = "UTF-8"?> <xsl:stylesheet version = "1.0"

  xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"
     xmlns = "http://www.w3.org/1999/xhtml">
  <xsl:output method = "xml" omit-xml-declaration = "no" 
      doctype-system = 
         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
      doctype-public = 
         "-//W3C//DTD XHTML 1.0 Strict//EN" />
  <xsl:template match = "/">
     <html>
        <head><title>Conditional Processing</title></head>
        <body>
           <paragraph>Appointments
              
<xsl:apply-templates select = "planner/year" /> </p> </body> </html> </xsl:template> <xsl:template match = "year"> Year: <xsl:value-of select = "@value" />
<xsl:for-each select = "date/note"> <xsl:sort select = "../@day" order = "ascending" data-type = "number" />
Day: <xsl:value-of select = "../@month"/>/ <xsl:value-of select = "../@day"/>
<xsl:choose> <xsl:when test = "@time > "0500" and @time < "1200""> Morning (<xsl:value-of select = "@time" />): </xsl:when> <xsl:when test = "@time > "1200" and @time < "1700""> Afternoon (<xsl:value-of select = "@time" />): </xsl:when> <xsl:when test = "@time > "1200" and @time < "2200""> Evening (<xsl:value-of select = "@time" />): </xsl:when> <xsl:when test = "@time > "2200" and @time < "500""> Night (<xsl:value-of select = "@time" />): </xsl:when> <xsl:otherwise> Entire day: </xsl:otherwise> </xsl:choose> <xsl:value-of select = "." /> <xsl:if test = ". = """> n/a </xsl:if>
</xsl:for-each> </xsl:template>

</xsl:stylesheet> Output: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html

 PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Conditional Processing</title></head><body><paragraph>Appointments

              
Year:2002

Day: 7/ 4
Entire day: Independence Day

Day: 7/ 9
Entire day: n/a

Day: 7/ 15
Afternoon (1430): Doctor"s appointment

Day: 7/ 15
Afternoon (1620): course
</p></body></html></source>