XML/XSLT stylesheet/Choose — различия между версиями

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

Текущая версия на 11:26, 26 мая 2010

Choose when statement

   <source lang="xml">

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

 <Character age="1">Character 1</Character>
 <Character age="2">Character 2</Character>
 <Character age="3">Character 3</Character>
 <Character age="4">Character 4</Character>
 <Character age="5">Character 5</Character>

</Characters>

File: Transform.xslt <?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

 version="1.0">
 <xsl:template match="/">
   <html>
     <head>
       <title>title</title>
     </head>
     <body>

header 3.

       <xsl:apply-templates select="/Characters/Character" />
     </body>
   </html>
 </xsl:template>
 <xsl:template match="Character">
   <xsl:choose>
     <xsl:when test="@age > 10 ">
       <paragraph>
         
           <xsl:value-of select="." />
         
         age,
         
           <xsl:value-of select="@age" />
         
         .
       </paragraph>
     </xsl:when>
     <xsl:otherwise>
       <paragraph>
         
           <xsl:value-of select="." />
         
         - ok
       </paragraph>
       .
     </xsl:otherwise>
   </xsl:choose>
 </xsl:template>

</xsl:stylesheet> Output: <html>

  <head>
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     <title>title</title>
  </head>
  <body>

header 3.

     <paragraph>Character 1
                  - ok
                
     </paragraph>
             .
           
     <paragraph>Character 2
                  - ok
                
     </paragraph>
             .
           
     <paragraph>Character 3
                  - ok
                
     </paragraph>
             .
           
     <paragraph>Character 4
                  - ok
                
     </paragraph>
             .
           
     <paragraph>Character 5
                  - ok
                
     </paragraph>
             .
           
  </body>

</html>

</source>
   
  


when test="position()=last()"

   <source lang="xml">

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

 <head:header xmlns:head="http://www.domain.ru/namespace/header">
   <title>Email List</title>
   <maintainer>Joe</maintainer>
 </head:header>
 <person type="personal" id="p001">
   <name>person1</name>
   <email>p@hotmail.ru</email>
 </person>
 <person type="work" id="p002">
   <name>person2</name>
   <email>p@hotmail.ru</email>
 </person>
 <person type="personal" id="p003">
   <name>person3</name>
   <email>p3@hotmail.ru</email>
 </person>

</emailList> 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" indent="yes" />
  <xsl:template match="/">
     <xsl:apply-templates select="emailList/person" />
  </xsl:template>
  <xsl:template match="person">
     <xsl:choose>
        <xsl:when test="position()=last()">
        The last entry in your email list is
        <xsl:value-of select="name" />.</xsl:when>
        <xsl:otherwise>
           <xsl:value-of select="name" />,
           <xsl:value-of select="email" />
        </xsl:otherwise>
     </xsl:choose>
  </xsl:template>

</xsl:stylesheet> Output:

person1,
  p@hotmail.ru
person2,
  p@hotmail.ru

The last entry in your email list is person3.

</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">Appointment</note>
     <note time="1620">Physics class</note>
   </date>
   <date month="7" day="4">
     <note>Independence Day</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>

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="../@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 > "1700" 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>

Appointments
Year:2002 Day: 7 / 4Entire day:Independence Day
Day: 7 / 15 Afternoon ( 1430 ): Appointment
Day: 7 / 15 Afternoon ( 1620 ): Physics class

</body></html>
</source>