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

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

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

attribute omitted

File: Data.xml
<wine price="10.99" year="1997">A</wine>
File: Transform.xslt
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:template match="wine">
    <wine vintage="{@year}">
      <xsl:apply-templates />
    </wine>
  </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?><wine vintage="1997">A</wine>



compare attribute value

File: Data.xml
<data>
    <line lid="u1">hello</line>
    <line color="red" lid="u2">hello</line>
    <line color="blue" lid="u3">hello</line>
    <line lid="u4">hello there</line>
    <line color="blue" lid="u5">hello there</line>
    <line color="blue" lid="u6">hello</line>
</data>
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" />
  <xsl:template match="line">
    <xsl:if test="not(@color = preceding::line/@color)">
      <xsl:copy>
        <xsl:apply-templates select="@*|node()" />
      </xsl:copy>
    </xsl:if>
  </xsl:template>
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()" />
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>
Output:
<data>
    <line lid="u1">hello</line>
    <line color="red" lid="u2">hello</line>
    <line color="blue" lid="u3">hello</line>
    <line lid="u4">hello there</line>
    
    
</data>



Get attribute from different level

File: Data.xml
<data grape="A">
  <winery year="1998">B</winery>
</data>
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="winery">
    <wine>
      <xsl:value-of select="@year" />
      <xsl:text/>
      <xsl:value-of select="../@grape" />
    </wine>
  </xsl:template>
</xsl:stylesheet>
Output:

  <wine>1998A</wine>



get attribute name

File: Data.xml
<poem year="1667" type="epic">
  <verse>line 3</verse>
  <verse>line 4</verse>
</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="poem">
    <xsl:element name="{@type}">
      <author>John</author>
      <year>
        <xsl:value-of select="@year" />
      </year>
      <xsl:apply-templates />
    </xsl:element>
  </xsl:template>
  <xsl:template match="verse">
    <verse>
      <xsl:apply-templates />
    </verse>
  </xsl:template>
  
</xsl:stylesheet>
Output:
<epic><author>John</author><year>1667</year>
  <verse>line 3</verse>
  <verse>line 4</verse>
</epic>



Get value of attribute with @

File: Data.xml
<?xml version="1.0"?>
<Book>
  <Title>this is the title</Title>
  <Authors>
    <Author>A</Author>
    <Author>B</Author>
    <Author>C</Author>
  </Authors>
  <Year>2007</Year>
  <Chapters>
    <Chapter number="1" title="title 1">chapter 1</Chapter>
    <Chapter number="2" title="title 2">chapter 2</Chapter>
    <Chapter number="3" title="title 3">chapter 3</Chapter>
    <Chapter number="4" title="title 4">chapter 4</Chapter>
    <Chapter number="5" title="title 5">chapter 5</Chapter>
  </Chapters>
</Book>

File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet 
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
     version="1.0">
    <xsl:template match="Chapter" mode="TOC">
     <paragraph>
         <b>
            <xsl:value-of select="@number" />:
         </b> 
         <xsl:value-of select="@title" />
    </paragraph>
    </xsl:template>

</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
  this is the title
  
    A
    B
    C
  
  2007
  
    chapter 1
    chapter 2
    chapter 3
    chapter 4
    chapter 5



if there is an attribute

File: Data.xml
<para color="blue" flavor="mint" author="jm">
  test
</para>
File: Transform.xslt
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:output method="text" />
  <xsl:template match="para">
    <xsl:if test="@flavor">flavor</xsl:if>
    <xsl:if test="@font">font</xsl:if>
    <xsl:if test="@author = "jm"">author</xsl:if>
  </xsl:template>
</xsl:stylesheet>
Output:
flavorauthor



List the attribute names and values

File: Data.xml
<para color="blue" flavor="mint" author="jm">
  test
</para>
File: Transform.xslt

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:template match="para">
    Color:
    <xsl:value-of select="@color" />
    <xsl:for-each select="@*">
      attribute name:
      <xsl:value-of select="name()" />
      attribute value:
      <xsl:value-of select="." />
    </xsl:for-each>
  </xsl:template>
  
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
    Color:
    blue
      attribute name:
      color
      attribute value:
      blue
      attribute name:
      flavor
      attribute value:
      mint
      attribute name:
      author
      attribute value:
      jm



select node by attribute value

File: Data.xml
<?xml version="1.0"?>
<employees>
  <employee eid="98145" dept="programming">
    <title>Java Programmer</title>
    <contact addInfo="info1">
      <name>
        <firstName>Joe</firstName>
        <middleName int="B">Brian</middleName>
        <lastName>Smith</lastName>
      </name>
      <address>
        <street>1 Drive</street>
        <city>Vancouver</city>
        <state>BC</state>
        <zipcode>80210</zipcode>
      </address>
      <phone>
        <tel type="wk">111-1111111</tel>
        <tel type="hm">222-222222</tel>
        <fax>303-4667357</fax>
      </phone>
      <email>a@a.ru</email>
    </contact>
    <hireDate>2008-10-29</hireDate>
  </employee>
</employees>

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="employees">
    <html>
      <head>
        <title>Employee Data</title>
      </head>
      <body>
        <table>
          <tr>
            <th>Name</th>
            <th>Hire Date</th>
            <th>Address</th>
            <th>Phone</th>
            <th>Fax</th>
            <th>Email</th>
          </tr>
          <xsl:for-each select="employee">
            <tr>
              <td><xsl:value-of select="contact/name/name"/> <xsl:value-of select="contact/name/middleName"/> <xsl:value-of select="contact/name/lastName"/></td>
              <td><xsl:value-of select="hireDate"/></td>
              <td><xsl:value-of select="contact/address/street"/>
                  <br />
                  <xsl:value-of select="contact/address/city"/>, <xsl:value-of select="contact/address/state"/> <xsl:value-of select="contact/address/zip"/>
              </td>
              <td>WK: <xsl:value-of select="contact/phone/tel[@type=wk]"/>
                  <br />
                  HM: <xsl:value-of select="contact/phone/tel[@type=hm]"/>
              </td>
              <td><xsl:value-of select="contact/phone/fax"/></td>
              <td><xsl:value-of select="contact/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>Employee Data</title>
   </head>
   <body>
      <table>
         <tr>
            <th>Name</th>
            <th>Hire Date</th>
            <th>Address</th>
            <th>Phone</th>
            <th>Fax</th>
            <th>Email</th>
         </tr>
         <tr>
            <td>BrianSmith</td>
            <td>2008-10-29</td>
            <td>1 Drive<br>Vancouver, BC
            </td>
            <td>WK: <br>
               HM: 
            </td>
            <td>303-4667357</td>
            <td>a@a.ru</td>
         </tr>
      </table>
   </body>
</html>



Set attribute value in tranformation

File: Data.xml 
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="Transform.xslt" ?>
<html>
  <head>
  <title>A simple HTML page</title>
  <style type="text/css">
    body { font-family: Verdana, Arial, sans-serif; font-size: 12px;}
  </style>
  </head>
<body>
  <h1>Our neighbours</h1>
  <h2>Venus</h2>
  <ul>
    <li><strong>A</strong>  5</li>
    <li><strong>B:</strong> 0</li>
    <li><strong>C:</strong> 4</li>
    <li><strong>D:</strong> 2</li>
    <li><strong>E:</strong> 4</li>
  </ul> 
  <h2>Mars</h2>
  <ul>
    <li><strong>A</strong>  4</li>
    <li><strong>B:</strong> 2</li>
    <li><strong>C:</strong> 3</li>
    <li><strong>D:</strong> 4</li>
    <li><strong>E:</strong> 6</li>
  </ul>
   </body>
</html>
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" version="4.0" indent="yes"/>
  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="body">
    <body>
      <p>
        <a href="http://www.nasa.gov/">Visit NASA!</a> | <a href="http://www.nineplanets.org/">Tour the solar system</a>
      </p>
      <h2>Quick reference</h2>
      <ul>
        <xsl:for-each select="h2">
          <li>
            <a>
              <xsl:attribute name="href">
                #<xsl:value-of select="text()"/></xsl:attribute>
              <xsl:value-of select="text()"/>
            </a>
          </li>
        </xsl:for-each>
      </ul>
      <xsl:apply-templates/>
      <hr/>
      Copyright Planetary Fun 2006.
    </body>
  </xsl:template>
  <xsl:template match="h2">
    <a>
      <xsl:attribute name="name"><xsl:value-of select="text()"/></xsl:attribute>
      <h2>
        <xsl:apply-templates/>
      </h2>
    </a>
  </xsl:template>
</xsl:stylesheet>

Output:
<?xml-stylesheet type="text/xsl" href="Transform.xslt" ><html>
   
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      
      <title>A simple HTML page</title>
      <style type="text/css">
    body { font-family: Verdana, Arial, sans-serif; font-size: 12px;}
  </style>
      </head>
   
   <body>
      <p><a href="http://www.nasa.gov/">Visit NASA!</a> | <a href="http://www.nineplanets.org/">Tour the solar system</a></p>
      <h2>Quick reference</h2>
      <ul>
         <li><a href="%0A                #Venus">Venus</a></li>
         <li><a href="%0A                #Mars">Mars</a></li>
      </ul>
      
      <h1>Our neighbours</h1>
      <a name="Venus">
         <h2>Venus</h2></a>
      
      <ul>
         
         <li><strong>A</strong>  5
         </li>
         
         <li><strong>B:</strong> 0
         </li>
         
         <li><strong>C:</strong> 4
         </li>
         
         <li><strong>D:</strong> 2
         </li>
         
         <li><strong>E:</strong> 4
         </li>
         
      </ul> 
      <a name="Mars">
         <h2>Mars</h2></a>
      
      <ul>
         
         <li><strong>A</strong>  4
         </li>
         
         <li><strong>B:</strong> 2
         </li>
         
         <li><strong>C:</strong> 3
         </li>
         
         <li><strong>D:</strong> 4
         </li>
         
         <li><strong>E:</strong> 6
         </li>
         
      </ul>
      
      <hr>
      Copyright Planetary Fun 2006.
      
   </body>
   
</html>