XML/XSLT stylesheet/if

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

An example of if-then-else logic in XSLT 1.0

File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:param name="x" select=""10""/>
  <xsl:output method="text"/>
  <xsl:template match="/">
    <xsl:text>&#xA;An example of if-then-else logic in XSLT 1.0:</xsl:text>
    <xsl:text>&#xA;&#xA;  If $x is larger than 10, print "Big", </xsl:text>
    <xsl:text>&#xA;    otherwise print "Little"</xsl:text>
    <xsl:text>&#xA;&#xA;           </xsl:text>
    <xsl:choose>
      <xsl:when test="$x &gt; 10">
        <xsl:text>Big</xsl:text>
      </xsl:when>
      <xsl:otherwise>
        <xsl:text>Little</xsl:text>
      </xsl:otherwise>
    </xsl:choose>
    <xsl:text>&#xA;</xsl:text>
  </xsl:template>
</xsl:stylesheet>
Output:

An example of if-then-else logic in XSLT 1.0:
  If $x is larger than 10, print "Big", 
    otherwise print "Little"
           Little



Branching

File: Data.xml
<?xml version="1.0" standalone="no" ?>
<transcript>
  <student id="STU12345" name="name 1" status="active">
    <home_address>35 Wall Street, Wonderland, NJ</home_address>
    <interests>
      <interest>interest 1</interest>
      <interest>interest 2</interest>
      <interest>interest 3</interest>
    </interests>
  </student>
  <term>
    <heading name="Winter 1999" />
    <course>
      <course-name>course 1</course-name>
      <grade>A-</grade>
      <credits>4</credits>
    </course>
    <course>
      <course-name>course 2</course-name>
      <grade>A</grade>
      <credits>3</credits>
    </course>
  </term>
  <summary>summary</summary>
  <comments>
    comments
  </comments>
</transcript>
File: Transform.xslt

<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="transcript">
    Student received A"s in the following courses:
    <xsl:apply-templates
      select="term/course[starts-with(grade,"A")]" />
  </xsl:template>
  <xsl:template match="course">
    <xsl:value-of select="course-name" />
    <xsl:if test="not(position()=last())">,</xsl:if>
  </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
    Student received A"s in the following courses:
    course 1,course 2



Compare value of attribute with if statement

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>select</title>
      </head>
      <body>
        <h3>header 3.</h3>
        <xsl:apply-templates select="/Characters/Character" />
      </body>
    </html>
  </xsl:template>
  <xsl:template match="Character">
    <xsl:if test="@age &gt; 2 ">
      <paragraph>
        <b>
          <xsl:value-of select="." />
        </b>
        is older than expected.
        <b>
          <xsl:value-of select="@age" />
        </b>
        , is correct.
        </paragraph>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>
Output:
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>select</title>
   </head>
   <body>
      <h3>header 3.</h3>
      <paragraph><b>Character 3</b>
                 is older than expected.
                 <b>3</b>
                 , is correct.
                 
      </paragraph>
      <paragraph><b>Character 4</b>
                 is older than expected.
                 <b>4</b>
                 , is correct.
                 
      </paragraph>
      <paragraph><b>Character 5</b>
                 is older than expected.
                 <b>5</b>
                 , is correct.
                 
      </paragraph>
   </body>
</html>



if statement and value compare

File: Data.xml
<poem>
  <a>line 1</a>
  <b>line 1</b>
  <c>line 1</c>
  <d>
    line 1
  </d>
</poem>
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" indent="no" />
  <xsl:template match="a">
    <xsl:if test=". = "line 1"">
      1. a = "line 1"
    </xsl:if>
    <xsl:if test=". = ../b">2. a = ../b</xsl:if>
    <xsl:if test=". = ../c">3. a = ../c</xsl:if>
    <xsl:if test=". != ../c">4. a != ../c</xsl:if>
    <xsl:if
      test="translate(.,"abcdefghijklmnopqrstuvwxyz","ABCDEFGHIJKLMNOPQRSTUVWXYZ") = translate(../c,"abcdefghijklmnopqrstuvwxyz","ABCDEFGHIJKLMNOPQRSTUVWXYZ")">
      5. a = ../c (ignoring case)
    </xsl:if>
    <xsl:if test=". = ../d">6. a = ../d</xsl:if>
    <xsl:if test=". = normalize-space(../d)">
      7. a = normalize-space(../d)
    </xsl:if>
  </xsl:template>
  <xsl:template match="b|c|d" />
</xsl:stylesheet>
Output:

  
      1. a = "line 1"
    2. a = ../b3. a = ../c
      5. a = ../c (ignoring case)
    
      7. a = normalize-space(../d)



if statement in for-each loop

File: Data.xml 
<?xml version="1.0"?>
<book>
     <title>title 4</title>
     <author>author 1</author>
     <author>author 2</author>
     <author>author 3</author>
     <author>author 4</author>
</book>
File: Transform.xslt
<?xml version="1.0"?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:template match="book">
    <xsl:value-of select="title" />
    by
    <xsl:for-each select="author">
      <xsl:value-of select="." />
      <xsl:if test="position()!=last()">,</xsl:if>
      <xsl:if test="position()=last()-1">and</xsl:if>
    </xsl:for-each>
  </xsl:template>
</xsl:transform>

Output:
<?xml version="1.0" encoding="UTF-8"?>title 4
    by
    author 1,author 2,author 3,andauthor 4



if statement with and operator

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"
                xmlns="http://www.wbex.ru"
                xmlns:xsd="http://www.w3.org/2000/10/XMLSchema"
                version="1.0">
 
    <xsl:output method="xml"/>
    <xsl:template match="*">
        <xsl:element name="{name(.)}">
            <xsl:for-each select="@*">
                <xsl:if test="(name(.) != "minOccurs") and (name(.) != "maxOccurs")">
                    <xsl:attribute name="{name(.)}">
                        <xsl:value-of select="."/>
                    </xsl:attribute>
                </xsl:if>
            </xsl:for-each>
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?><Characters xmlns="http://www.wbex.ru">
  <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>



Use boolean operator in if statement

File: Data.xml
<?xml version="1.0"?>
<booklist>
  <book category="S">
    <title>title 1</title>
    <author>author 1</author>
  </book>
  <book category="FC">
    <title>author 1</title>
    <author>author 1</author>
  </book>
  <book category="FC">
    <title>title 3</title>
    <author>author 1</author>
  </book>
  <book category="CS">
    <title>title 4</title>
    <author>author 1</author>
    <author>author 2</author>
    <author>author 3</author>
    <author>author 4</author>
  </book>
</booklist>
File: Transform.xslt
<?xml version="1.0"?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="2.0">
  <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>
          <p>
            <i>by</i>
            <xsl:value-of select="author[1]" />
            <xsl:if test="count(author)!=1">
              and others
            </xsl:if>
          </p>
          <xsl:variable name="others" select="$all-books[./@category=current()/@category and not(. is current())]" />
          <xsl:if test="$others">
            <p>Other books in this category:</p>
            <ul>
              <xsl:for-each select="$others">
                <li>
                  <xsl:value-of select="title" />
                </li>
              </xsl:for-each>
            </ul>
          </xsl:if>
        </xsl:for-each>
      </body>
    </html>
  </xsl:template>
</xsl:transform>
Output:
<html>
   <body>
      <h1>title 1</h1>
      <p><i>by</i>author 1
      </p>
      <h1>author 1</h1>
      <p><i>by</i>author 1
      </p>
      <p>Other books in this category:</p>
      <ul>
         <li>title 3</li>
      </ul>
      <h1>title 3</h1>
      <p><i>by</i>author 1
      </p>
      <p>Other books in this category:</p>
      <ul>
         <li>author 1</li>
      </ul>
      <h1>title 4</h1>
      <p><i>by</i>author 1
                       and others
                     
      </p>
   </body>
</html>