XML/XSLT stylesheet/for each

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

for-each loop and table rows

File: Data.xml

<?xml version="1.0"?>
<emailList>
  <person>
    <name>name 1</name>
    <email>g@gmail.ru</email>
  </person>
  <person>
    <name>name 2</name>
    <email>n@hotmail.ru</email>
  </person>
</emailList>

File: Transform.xslt
<html xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xsl:version="1.0">
  <head>
    <title>Email Listing</title>
  </head>
  <body>
    <table>
      <tr>
        <th>Name</th>
        <th>E-mail Address</th>
      </tr>
      <xsl:for-each select="emailList/person">
        <tr>
          <td><xsl:value-of select="name"/></td>
          <td><xsl:value-of select="email"/></td>
        </tr>
      </xsl:for-each>
    </table>
  </body>
</html>
Output:
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>Email Listing</title>
   </head>
   <body>
      <table>
         <tr>
            <th>Name</th>
            <th>E-mail Address</th>
         </tr>
         <tr>
            <td>name 1</td>
            <td>g@gmail.ru</td>
         </tr>
         <tr>
            <td>name 2</td>
            <td>n@hotmail.ru</td>
         </tr>
      </table>
   </body>
</html>



for-each select="child::parentTagName[@attributeName="your value"]"

File: Data.xml
<winelist>
  <wine grape="Chardonnay">
    <winery>shop 2</winery>
    <product>product 2</product>
    <year>1997</year>
    <prices>
      <list>10.99</list>
      <discounted>9.50</discounted>
    </prices>
  </wine>
  <wine grape="Cabernet">
    <winery>shop 1</winery>
    <product>product 1</product>
    <year>1998</year>
    <prices>
      <list>6.99</list>
      <discounted>5.99</discounted>
    </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="winelist">
    <xsl:for-each select="child::wine[@grape="Cabernet"]">
      <xsl:value-of select="winery" />
    </xsl:for-each>
  </xsl:template>
  
</xsl:stylesheet>

Output:
shop 1



for-each select="child::parentTagName", value-of select="childTagName"

File: Data.xml
<winelist>
  <wine grape="Chardonnay">
    <winery>shop 2</winery>
    <product>product 2</product>
    <year>1997</year>
    <prices>
      <list>10.99</list>
      <discounted>9.50</discounted>
    </prices>
  </wine>
  <wine grape="Chardonnay">
    <winery>shop 1</winery>
    <product>product 1</product>
    <year>1998</year>
    <prices>
      <list>6.99</list>
      <discounted>5.99</discounted>
    </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="winelist">
    <xsl:for-each select="child::wine">
      <xsl:value-of select="winery" />
    </xsl:for-each>
  </xsl:template>
  
</xsl:stylesheet>
Output:
shop 2shop 1



for-each select="node()", value-of select="."

File: Data.xml

<book author="jm">
  <chapter>this is a test</chapter>
  <?xml-stylesheet href="ss.css" type="text/css" ?>
</book>
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="book">
    <xsl:for-each select="node()">
      <xsl:value-of select="." />
    </xsl:for-each>
  </xsl:template>
  
</xsl:stylesheet>
Output:

  this is a test
  href="ss.css" type="text/css"



for-each select="*", value-of="."

File: Data.xml
<book author="jm">
  <chapter>this is a test</chapter>
  <?xml-stylesheet href="ss.css" type="text/css" ?>
</book>
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="book">
    <xsl:for-each select="*">
      <xsl:value-of select="." />
    </xsl:for-each>
  </xsl:template>
  
</xsl:stylesheet>
Output:
this is a test



Nested for-each statement

File: Data.xml
<?xml version="1.0" ?>
<customer-list>
  <customer>
    <name>
      <first>Peter</first>
      <last>Whimsey</last>
    </name>
    <order>25 cases Earl Grey tea</order>
    <order>12 bottles brandy</order>
  </customer>
  <customer>
    <name>
      <first>Charlie</first>
      <last>Chan</last>
    </name>
    <order>1 Chinese-English dictionary</order>
    <order>5 cases mustache wax</order>
  </customer>
  <customer>
    <name>
      <first>Nora</first>
      <last>Charles</last>
    </name>
    <order>5 bottles wine</order>
    <order>24 sandwiches</order>
  </customer>
  <customer>
    <name>
      <first>Nick</first>
      <last>Charles</last>
    </name>
    <order>12 cases gin</order>
    <order>4 cartons dog food</order>
  </customer>
</customer-list>
File: Transform.xslt
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match="/">
    <HTML>
      <HEAD><TITLE>Customers</TITLE></HEAD>
      <BODY>
        <TABLE BORDER="1" >
          <xsl:for-each select="customer-list/customer">
            <TR><TH ALIGN="left">
              <xsl:apply-templates select="name"/>
            </TH></TR>
            <xsl:for-each select="order">
              <TR><TD>
                <xsl:apply-templates/>
              </TD></TR>
            </xsl:for-each>
          </xsl:for-each>
        </TABLE>
      </BODY>
    </HTML>
  </xsl:template>
  <xsl:template match="name">
    <xsl:value-of select="last" />, 
    <xsl:value-of select="first" />
  </xsl:template>
</xsl:stylesheet>
Output:
<HTML>
   <HEAD>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <TITLE>Customers</TITLE>
   </HEAD>
   <BODY>
      <TABLE BORDER="1">
         <TR>
            <TH ALIGN="left">Whimsey, 
                   Peter
            </TH>
         </TR>
         <TR>
            <TD>25 cases Earl Grey tea</TD>
         </TR>
         <TR>
            <TD>12 bottles brandy</TD>
         </TR>
         <TR>
            <TH ALIGN="left">Chan, 
                   Charlie
            </TH>
         </TR>
         <TR>
            <TD>1 Chinese-English dictionary</TD>
         </TR>
         <TR>
            <TD>5 cases mustache wax</TD>
         </TR>
         <TR>
            <TH ALIGN="left">Charles, 
                   Nora
            </TH>
         </TR>
         <TR>
            <TD>5 bottles wine</TD>
         </TR>
         <TR>
            <TD>24 sandwiches</TD>
         </TR>
         <TR>
            <TH ALIGN="left">Charles, 
                   Nick
            </TH>
         </TR>
         <TR>
            <TD>12 cases gin</TD>
         </TR>
         <TR>
            <TD>4 cartons dog food</TD>
         </TR>
      </TABLE>
   </BODY>
</HTML>



Use for-each loop

File: Data.xml
<?xml version="1.0"?>
<Objects>
  <Object name="Car">
    <Characteristic>A</Characteristic>
    <Characteristic>B</Characteristic>
    <Characteristic>C</Characteristic>
    <Characteristic>D</Characteristic>
  </Object>
  <Object name="Orange">
    <Characteristic>1</Characteristic>
    <Characteristic>2</Characteristic>
    <Characteristic>3</Characteristic>
    <Characteristic>4</Characteristic>
  </Object>
</Objects>

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>
        <h3>
          Characteristics of
          <xsl:value-of select="Objects/Object/@name" />
        </h3>
        <xsl:apply-templates select="/Objects/Object" />
      </body>
    </html>
  </xsl:template>
  <xsl:template match="Object">
    <ul>
      <xsl:for-each select="Characteristic">
        <li>
          <xsl:value-of select="." />
        </li>
      </xsl:for-each>
    </ul>
  </xsl:template>
</xsl:stylesheet>
Output:
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>title</title>
   </head>
   <body>
      <h3>
                   Characteristics of
                   Car
      </h3>
      <ul>
         <li>A</li>
         <li>B</li>
         <li>C</li>
         <li>D</li>
      </ul>
      <ul>
         <li>1</li>
         <li>2</li>
         <li>3</li>
         <li>4</li>
      </ul>
   </body>
</html>



Use for-each to loop through each tags

File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="Transform.xslt"?>
<emailList>
  <person>
    <name>person1</name>
    <email>p@hotmail.ru</email>
  </person>
  <person>
    <name>person2</name>
    <email>p@hotmail.ru</email>
  </person>
  <person>
    <name>person3</name>
    <email>p3@hotmail.ru</email>
  </person>
</emailList>

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:output method="html"/>
  <xsl:template match="/">
    <html>
      <head>
        <title>Email Listing</title>
      </head>
      <body>
        <table>
          <tr>
            <th>Name</th>
            <th>E-mail Address</th>
          </tr>
          <xsl:for-each select="emailList/person">
            <tr>
              <td><xsl:value-of select="name"/></td>
              <td><xsl:value-of select="email"/></td>
            </tr>
          </xsl:for-each>
        </table>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

Output:
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>Email Listing</title>
   </head>
   <body>
      <table>
         <tr>
            <th>Name</th>
            <th>E-mail Address</th>
         </tr>
         <tr>
            <td>person1</td>
            <td>p@hotmail.ru</td>
         </tr>
         <tr>
            <td>person2</td>
            <td>p@hotmail.ru</td>
         </tr>
         <tr>
            <td>person3</td>
            <td>p3@hotmail.ru</td>
         </tr>
      </table>
   </body>
</html>