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

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

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

Check two attributes

File: Data.xml
<?xml version="1.0" standalone="yes"?>
<poem author="jm" year="1667">
  <verse>line 1</verse>
  <verse>line 2</verse>
  <verse>line 3</verse>
  <verse>line 4</verse>
</poem>
File: Transform.xslt
<?xml version="1.0" standalone="yes"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:output method="text" />
  <xsl:template match="poem">
    <xsl:if test="(@author = "bd") or (@year="1667")">
      Either the author is "bd" or the year is "1667".
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>
Output:

      Either the author is "bd" or the year is "1667".


if test="not(preceding-sibling::address[zip=$lastZip])"

File: Data.xml
<?xml version="1.0"?>
<addressbook>
  <address>
    <name>
      <title>Ms.</title>
      <first-name>Doris</first-name>
      <last-name>Smith</last-name>
    </name>
    <street>707 Main Way</street>
    <city>New York</city>
    <state>ME</state>
    <zip>00218</zip>
  </address>
  <address>
    <name>
      <first-name>Jane</first-name>
      <last-name>Smith</last-name>
    </name>
    <street>283 First Avenue</street>
    <city>Vancouver</city>
    <state>MA</state>
    <zip>02718</zip>
  </address>
</addressbook>
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" indent="no"/>
  <xsl:variable name="newline">
  <xsl:text></xsl:text>
  </xsl:variable>
  <xsl:template match="/">
    <xsl:text>Addresses sorted by zip code</xsl:text>
    <xsl:value-of select="$newline"/>
    <xsl:for-each select="addressbook/address">
      <xsl:sort select="zip"/>
      <xsl:sort select="name/last-name"/>
      <xsl:sort select="name/first-name"/>
      <xsl:variable name="lastZip" select="zip"/>
      <xsl:if test="not(preceding-sibling::address[zip=$lastZip])">
        <xsl:text>Zip code </xsl:text>
        <xsl:value-of select="zip"/>
        <xsl:text>: </xsl:text>
        <xsl:value-of select="$newline"/>
        <xsl:for-each select="/addressbook/address[zip=$lastZip]">
          <xsl:sort select="name/last-name"/>
          <xsl:sort select="name/first-name"/>
          <xsl:if test="name/title">
            <xsl:value-of select="name/title"/>
            <xsl:text> </xsl:text>
          </xsl:if>
          <xsl:value-of select="name/first-name"/>
          <xsl:text> </xsl:text>
          <xsl:value-of select="name/last-name"/>
          <xsl:value-of select="$newline"/>
          <xsl:value-of select="street"/>
          <xsl:value-of select="$newline"/>
          <xsl:value-of select="$newline"/>
        </xsl:for-each>
      </xsl:if>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>
Output:
Addresses sorted by zip codeZip code 00218: Ms. Doris Smith707 Main WayZip code 02718: Jane Smith283 First Avenue


if test="(position() mod

File: Data.xml
<?xml version="1.0"?>
<list>
  <title>Java</title>
  <listitem>Item 1</listitem>
  <listitem>Item 2</listitem>
  <listitem>Item 3</listitem>
  <listitem>Item 4</listitem>
  <listitem>Item 5</listitem>
  <listitem>Item 6</listitem>
  <listitem>Item 7</listitem>
  <listitem>Item 8</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="text"/>
  <xsl:variable name="newline">
  <xsl:text></xsl:text>
  </xsl:variable>
  <xsl:template match="/">
    <xsl:value-of select="$newline"/>
    <xsl:text>Here are the odd-numbered items from the list:</xsl:text>
    <xsl:value-of select="$newline"/>
    <xsl:for-each select="list/listitem">
      <xsl:if test="(position() mod 2) = 1">
        <xsl:number format="1. "/>
        <xsl:value-of select="."/>
        <xsl:value-of select="$newline"/>
      </xsl:if>
    </xsl:for-each>
  </xsl:template>
  
</xsl:stylesheet>
Output:
Here are the odd-numbered items from the list:1. Item 13. Item 35. Item 57. Item 7


Nested if statement

File: Data.xml
<?xml version="1.0"?>
<poem author="jm" year="1667">
  <verse>line 1</verse>
  <verse>line 2</verse>
  <verse>line 3</verse>
  <verse>line 4</verse>
</poem>
File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:output method="text" />
  <xsl:template match="poem">
    <xsl:if test="@year &lt; "1850"">
      The poem is old.
      <xsl:if test="@year &lt; "1700"">
        The poem is very old.
      </xsl:if>
      <xsl:if test="@year &lt; "1500"">
        The poem is very, very old.
      </xsl:if>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>
Output:

      The poem is old.
      
        The poem is very old.


select with if them else statement

File: Data.xml
<?xml version="1.0"?>
<cars>
  <make geography="Europe">Alfa Romeo</make>
  <make geography="Europe">Bentley</make>
  <make geography="North America">Chevrolet</make>
  <make geography="North America">Dodge</make>
  <make geography="North America">GMC</make>
  <make geography="Asia">Honda</make>
  <make geography="Asia">Isuzu</make>
  <make geography="?">Quantum</make>
</cars>
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="/">
    <xsl:for-each select="cars/make">
      <xsl:text>&#xA;  Car: </xsl:text>
      <xsl:value-of select="."/>
      <xsl:text> - </xsl:text>
      <xsl:value-of 
        select="if (@geography = "North America") then 
                  "Domestic car"
                else if (@geography = "Europe") then 
                  "Import from Europe"
                else if (@geography = "Asia") then 
                  &quot;It"s from Asia&quot;
                (: If it"s anything else :)
                else 
                   "We don""t know!""/>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>
Output:

  Car: Alfa Romeo - Import from Europe
  Car: Bentley - Import from Europe
  Car: Chevrolet - Domestic car
  Car: Dodge - Domestic car
  Car: GMC - Domestic car
  Car: Honda - It"s from Asia
  Car: Isuzu - It"s from Asia
  Car: Quantum - We don"t know!


The <xsl:if> Element: Conditional Processing

The xsl:if element tests whether a Boolean condition is true or false. 
If it is true, then the content of the xsl:if element is instantiated. 
If it is false, then nothing specified inside the xsl:ifelement is added to the result tree. 
File: Data.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<sales>
 <source>
  <title>Book</title>
  <url>http://www.wbex.ru</url>
  <amounts estimate="true" year="2002"/>
<nation>
 <name>USA</name>
 <capital>DC</capital>
 <amount>32277942</amount>
 <cc>dz</cc>
</nation>
<nation>
 <name>Canada</name>
 <capital>Ottwa</capital>
 <amount>6373002</amount>
 <cc>bi</cc>
</nation>
<nation>
 <name>Maxico</name>
 <capital>Maxico City</capital>
 <amount>4465651</amount>
 <cc>er</cc>
</nation>

</sales>

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="sales">
   <xsl:apply-templates select="nation" />
 </xsl:template>
 <xsl:template match="nation">
   <xsl:text> * </xsl:text>
   <xsl:value-of select="name" />
   <xsl:if test="amount > 10000000">
     <xsl:text> (> 10M)</xsl:text>
   </xsl:if>
   <xsl:text>
</xsl:text>
 </xsl:template>

</xsl:stylesheet> Output:

* USA (> 10M)
* Canada
* Maxico</source>
   
  

Use if statement to check whether it is the last

File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<list>
  <entry name="A"/>
  <entry name="B"/>
  <entry name="C"/>
  <entry name="D"/>
</list>
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="list">
      <xsl:for-each select="entry">
        <xsl:value-of select="@name"/>
        <xsl:if test="not (position()=last())">
          <xsl:text>, </xsl:text>
        </xsl:if>
      </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>A, B, C, D


Use if statement to test if an element as an attribute

File: Data.xml
<?xml version="1.0" standalone="yes"?>
<poem author="jm" year="1667">
  <verse>line 1</verse>
  <verse>line 2</verse>
  <verse>line 3</verse>
  <verse>line 4</verse>
</poem>
File: Transform.xslt
<?xml version="1.0" standalone="yes"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:output method="text" />
  <xsl:template match="poem">
    <xsl:if test="@year">
      The poem has a flavor attribute.
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>
Output:

      The poem has a flavor attribute.


xsl:if instruction enables conditional processing

File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<list>
    <entry name="A"/>
    <entry name="B"/>
    <entry name="C"/>
    <entry name="D"/>
</list>
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="list">
      <xsl:for-each select="entry">
        <xsl:value-of select="@name"/>
        <xsl:text>, </xsl:text>
      </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>A, B, C, D,