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

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

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

for-each and select="."

File: Data.xml
<?xml version="1.0"?>
<root ref="rootref" name="blue">
  <glossary>
    <item ref="blue" name="rootref">rootref</item>
  </glossary>
  <glossary>
    <item ref="itemref" name="itemref">itemref</item>
  </glossary>
</root>
File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <xsl:for-each select="root/node1/node2/node3">
      <xsl:value-of select="." />
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>



for-each select="listitem[position() > 1]"

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;">
        <xsl:apply-templates select="list"/>
      </body>
    </html>
  </xsl:template>
  <xsl:template match="list">
    <table border="1" cellpadding="5" cellspacing="5">
      <tr>
        <td style="background: black; color: white; 
                   font-weight: bold; font-size: 125%;" 
                   width="100" align="right">
          <xsl:if test="count(listitem) > 1">
            <xsl:attribute name="rowspan">
              <xsl:value-of select="count(listitem)"/>
            </xsl:attribute>
          </xsl:if>
          <xsl:value-of select="title"/>
        </td>
        <td>
          <xsl:value-of select="listitem[1]"/>
        </td>
      </tr>
      <xsl:for-each select="listitem[position() &gt; 1]">
        <tr>
          <td>
            <xsl:value-of select="."/>
          </td>
        </tr>
      </xsl:for-each>
    </table>
  </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;">
      <table border="1" cellpadding="5" cellspacing="5">
         <tr>
            <td style="background: black; color: white;                     font-weight: bold; font-size: 125%;" width="100" align="right" rowspan="7">title 1</td>
            <td>item 1</td>
         </tr>
         <tr>
            <td>item 2</td>
         </tr>
         <tr>
            <td>item 3</td>
         </tr>
         <tr>
            <td>item 4</td>
         </tr>
         <tr>
            <td>item 5</td>
         </tr>
         <tr>
            <td>item 6</td>
         </tr>
         <tr>
            <td>item 7</td>
         </tr>
      </table>
   </body>
</html>



List All customers: /report/customer

<?xml version="1.0" standalone="yes"?>
<report type="Unfilled Orders">
  <customer number="CUST111" type="VIP">
    <name>name 1</name>
    <order-list count="2">
      <order number="ORD200" owner="CUST111" total="650.00"
        status="late">
        <item-list>
          <item quantity="5" price="100">
            item 1
          </item>
          <item quantity="2" price="50">item 2</item>
          <item quantity="1" price="50">item 3</item>
        </item-list>
      </order>
      <order number="ORD105" owner="CUST111" total="150.00"
        status="backordered">
        <item-list>
          <item quantity="6" price="25">
            item 4
          </item>
        </item-list>
      </order>
    </order-list>
  </customer>
  <customer number="CUST222" type="normal">
    <name>Alice Liddle</name>
    <order-list count="2">
      <order number="ORD102" owner="CUST222" total="3490.00"
        status="late">
        <item-list>
          <item quantity="20" price="100">
            item 1
          </item>
          <item quantity="10" price="50">item 2</item>
          <item quantity="10" price="50">item 3</item>
          <item quantity="10" price="25">
            item 4
          </item>
          <item quantity="2" price="120">
            item 5
          </item>
        </item-list>
      </order>
    </order-list>
  </customer>
</report>
File: Transform.xslt
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" />
  <xsl:template match="/report/customer">
      <xsl:apply-templates />
  </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
  
    name 1
    
      
        
          
            item 1
          
          item 2
          item 3
        
      
      
        
          
            item 4
          
        
      
    
  
  
    Alice Liddle
    
      
        
          
            item 1
          
          item 2
          item 3
          
            item 4
          
          
            item 5



Match all from root

File: Data.xml
<?xml version="1.0"?>
<message>
  You can add processing instructions to a document with the
  <courier>processing-instruction</courier>
  element.
</message>

File: Transform.xslt
<stylesheet version="1.0"
  xmlns="http://www.w3.org/1999/XSL/Transform">
  <output method="text" />
  <template match="/">
    Message:
    <apply-templates />
  </template>
</stylesheet>
Output:

    Message:
    
  You can add processing instructions to a document with the
  processing-instruction
  element.



match element with certain attribute value

File: Data.xml
<employees>
  <employee hireDate="04/23/1999" officer="yes">
    <last>A</last>
    <first>B</first>
    <salary>100000</salary>
  </employee>
  <employee hireDate="09/01/1998" officer="no">
    <last>C</last>
    <first>D</first>
    <salary>95000</salary>
  </employee>
  <employee hireDate="08/20/2000">
    <last>E</last>
    <first>F</first>
    <salary>89000</salary>
  </employee>
</employees>
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="employee[@hireDate="10/16/2000"]" />
  
</xsl:stylesheet>
Output:

  
    A
    B
    100000
  
  
    C
    D
    
    95000
  
  
    E
    F
    89000



match="text()|@*"

File: Data.xml
<winelist>
  <wine grape="Chardonnay">
    <winery>shop 1</winery>
    <product>product 1</product>
    <year>1998</year>
    <prices>
      <list>6.99</list>
      <discounted>5.99</discounted>
      <case>71.50</case>
    </prices>
  </wine>
  <wine grape="Chardonnay">
    <winery>shop 2</winery>
    <product>product 2</product>
    <year>1997</year>
    <prices>
      <list>10.99</list>
      <discounted>9.50</discounted>
      <case>114.00</case>
    </prices>
  </wine>
</winelist>
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="*|/">
    <xsl:apply-templates />
  </xsl:template>
  <xsl:template match="text()|@*">
    <xsl:value-of select="." />
  </xsl:template>
  <xsl:template match="processing-instruction()|comment()" />
  
</xsl:stylesheet>
Output:

  
    shop 1
    product 1
    1998
    
      6.99
      5.99
      71.50
    
  
  
    shop 2
    product 2
    1997
    
      10.99
      9.50
      114.00



select="." (dot)

File: Data.xml
<wine grape="Cabernet">
  <winery>shop 1</winery>
  <product>product 1</product>
  <year>1996</year>
  <prices date="12/1/01">
    <list>13.99</list>
    <discounted>11.00</discounted>
  </prices>
</wine>
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="wine">
    <xsl:copy-of select="." />
  </xsl:template>
  
</xsl:stylesheet>
Output:
<wine grape="Cabernet">
  <winery>shop 1</winery>
  <product>product 1</product>
  <year>1996</year>
  <prices date="12/1/01">
    <list>13.99</list>
    <discounted>11.00</discounted>
  </prices>
</wine>



select down to a certain level and its attribute

File: Data.xml
<shirts>
  <colors>
   <color cid="c1">yellow</color>
   <color cid="c2">black</color>
   <color cid="c3">red</color>
   <color cid="c4">blue</color>
   <color cid="c5">purple</color>
   <color cid="c6">white</color>
   <color cid="c7">orange</color>
   <color cid="c7">green</color>
  </colors>
  <shirt colorCode="c4">item 1</shirt>
  <shirt colorCode="c1">item 2</shirt>
  <shirt colorCode="c6">item 3</shirt>
</shirts>
File: Transform.xslt
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:output method="text" />
  <xsl:template match="shirt">
    <xsl:variable name="shirtColorCode" select="@colorCode" />
    <xsl:value-of select="/shirts/colors/color[@cid = $shirtColorCode]" />
    <xsl:text> </xsl:text>
    <xsl:apply-templates />
  </xsl:template>
  <xsl:template match="color" />
</xsl:stylesheet>
Output:

  
   
   
   
   
   
   
   
   
  
  blue item 1
  yellow item 2
  white item 3



tag with index

File: Data.xml
<html>
  <body>
    <table>
      <tr>
        <td>A</td>
        <td>B</td>
        <td>100000</td>
        <td>4/23/1999</td>
      </tr>
      <tr>
        <td>C</td>
        <td>D</td>
        <td>95000</td>
        <td>09/01/1998</td>
      </tr>
      <tr>
        <td>E</td>
        <td>F</td>
        <td>97000</td>
        <td>10/16/2000</td>
      </tr>
    </table>
  </body>
</html>
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="table">
    <employees>
      <xsl:apply-templates />
    </employees>
  </xsl:template>
  <xsl:template match="tr">
    <employee hireDate="{td[4]}">
      <last>
        <xsl:value-of select="td[1]" />
      </last>
      <first>
        <xsl:value-of select="td[2]" />
      </first>
      <salary>
        <xsl:value-of select="td[3]" />
      </salary>
    </employee>
  </xsl:template>
</xsl:stylesheet>
Output:

  
    <employees>
      <employee hireDate="4/23/1999"><last>A</last><first>B</first><salary>100000</salary></employee>
      <employee hireDate="09/01/1998"><last>C</last><first>D</first><salary>95000</salary></employee>
      <employee hireDate="10/16/2000"><last>E</last><first>F</first><salary>97000</salary></employee>
    </employees>



template match="//customer"

<?xml version="1.0" standalone="yes"?>
<report type="Unfilled Orders">
  <customer number="CUST111" type="VIP">
    <name>name 1</name>
    <order-list count="2">
      <order number="ORD200" owner="CUST111" total="650.00"
        status="late">
        <item-list>
          <item quantity="5" price="100">
            item 1
          </item>
          <item quantity="2" price="50">item 2</item>
          <item quantity="1" price="50">item 3</item>
        </item-list>
      </order>
      <order number="ORD105" owner="CUST111" total="150.00"
        status="backordered">
        <item-list>
          <item quantity="6" price="25">
            item 4
          </item>
        </item-list>
      </order>
    </order-list>
  </customer>
  <customer number="CUST222" type="normal">
    <name>Alice Liddle</name>
    <order-list count="2">
      <order number="ORD102" owner="CUST222" total="3490.00"
        status="late">
        <item-list>
          <item quantity="20" price="100">
            item 1
          </item>
          <item quantity="10" price="50">item 2</item>
          <item quantity="10" price="50">item 3</item>
          <item quantity="10" price="25">
            item 4
          </item>
          <item quantity="2" price="120">
            item 5
          </item>
        </item-list>
      </order>
    </order-list>
  </customer>
</report>
File: Transform.xslt
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" />
  <xsl:template match="//customer">
      <xsl:apply-templates />
  </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
  
    name 1
    
      
        
          
            item 1
          
          item 2
          item 3
        
      
      
        
          
            item 4
          
        
      
    
  
  
    Alice Liddle
    
      
        
          
            item 1
          
          item 2
          item 3
          
            item 4
          
          
            item 5



template match="//customer[1]/name"

<?xml version="1.0" standalone="yes"?>
<report type="Unfilled Orders">
  <customer number="CUST111" type="VIP">
    <name>name 1</name>
    <order-list count="2">
      <order number="ORD200" owner="CUST111" total="650.00"
        status="late">
        <item-list>
          <item quantity="5" price="100">
            item 1
          </item>
          <item quantity="2" price="50">item 2</item>
          <item quantity="1" price="50">item 3</item>
        </item-list>
      </order>
      <order number="ORD105" owner="CUST111" total="150.00"
        status="backordered">
        <item-list>
          <item quantity="6" price="25">
            item 4
          </item>
        </item-list>
      </order>
    </order-list>
  </customer>
  <customer number="CUST222" type="normal">
    <name>Alice Liddle</name>
    <order-list count="2">
      <order number="ORD102" owner="CUST222" total="3490.00"
        status="late">
        <item-list>
          <item quantity="20" price="100">
            item 1
          </item>
          <item quantity="10" price="50">item 2</item>
          <item quantity="10" price="50">item 3</item>
          <item quantity="10" price="25">
            item 4
          </item>
          <item quantity="2" price="120">
            item 5
          </item>
        </item-list>
      </order>
    </order-list>
  </customer>
</report>
File: Transform.xslt
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" />
  <xsl:template match="//customer[1]/name">
      <xsl:apply-templates />
  </xsl:template>
</xsl:stylesheet>

Output:
<?xml version="1.0" encoding="UTF-8"?>
  
    name 1
    
      
        
          
            item 1
          
          item 2
          item 3
        
      
      
        
          
            item 4
          
        
      
    
  
  
    Alice Liddle
    
      
        
          
            item 1
          
          item 2
          item 3
          
            item 4
          
          
            item 5



template match="//processing-instruction()"

<?xml version="1.0" standalone="yes"?>
<report type="Unfilled Orders">
  <customer number="CUST111" type="VIP">
    <name>name 1</name>
    <order-list count="2">
      <order number="ORD200" owner="CUST111" total="650.00"
        status="late">
        <item-list>
          <item quantity="5" price="100">
            item 1
          </item>
          <item quantity="2" price="50">item 2</item>
          <item quantity="1" price="50">item 3</item>
        </item-list>
      </order>
      <order number="ORD105" owner="CUST111" total="150.00"
        status="backordered">
        <item-list>
          <item quantity="6" price="25">
            item 4
          </item>
        </item-list>
      </order>
    </order-list>
  </customer>
  <customer number="CUST222" type="normal">
    <name>Alice Liddle</name>
    <order-list count="2">
      <order number="ORD102" owner="CUST222" total="3490.00"
        status="late">
        <item-list>
          <item quantity="20" price="100">
            item 1
          </item>
          <item quantity="10" price="50">item 2</item>
          <item quantity="10" price="50">item 3</item>
          <item quantity="10" price="25">
            item 4
          </item>
          <item quantity="2" price="120">
            item 5
          </item>
        </item-list>
      </order>
    </order-list>
  </customer>
</report>
File: Transform.xslt
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" />
  <xsl:template match="//processing-instruction()">
      <xsl:apply-templates />
  </xsl:template>
</xsl:stylesheet>