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

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

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

apply template for a document

File: Data.xml
<shirts>
  <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="xml" omit-xml-declaration="yes" />
  <xsl:template match="shirts">
    <shirts>
      <xsl:apply-templates select="document("Data.xml")" />
      <xsl:apply-templates />
    </shirts>
  </xsl:template>
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()" />
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>



Apply template to a certain node in a document

File: Data.xml
<shirts>
  <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="xml" omit-xml-declaration="yes" indent="no" />
  <xsl:template match="shirts">
    <shirts>
      <xsl:apply-templates select="document("Data.xml")//*[@cid="c7"]" />
      <xsl:apply-templates />
    </shirts>
  </xsl:template>
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()" />
    </xsl:copy>
  </xsl:template>
  
</xsl:stylesheet>



Format table cell during transforming

File: Data.xml
<?xml version="1.0"?>
<employees>
  <employee eid="98145" dept="programming">
    <title>Java Programmer</title>
    <contact addInfo="info1">
      <name>
        <firstName>J</firstName>
        <middleName int="B">Brian</middleName>
        <lastName>S</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 cellpadding="5" bgcolor="#cccccc">
          <tr>
            <th>Number</th>
            <th>Name</th>
            <th>Hire Date</th>
            <th>Address</th>
            <th>Phone</th>
            <th>Fax</th>
            <th>Email</th>
          </tr>
          <xsl:apply-templates/>
        </table>
      </body>
    </html>
  </xsl:template>
  <xsl:template match="employee">
    <tr>
      <td><xsl:number/></td>
      <xsl:apply-templates select="contact"/>
    </tr>
   
  </xsl:template>
  <xsl:template match="contact">
    <td><xsl:value-of select="name/firstName"/> <xsl:value-of select="name/middleName"/> <xsl:value-of select="name/lastName"/></td>
    <td><xsl:value-of select="../hireDate"/></td>
    <td><xsl:value-of select="address/street"/>
        <br />
        <xsl:value-of select="address/city"/>, <xsl:value-of select="address/state"/> <xsl:value-of select="address/zip"/>
    </td>
    <td>WK: <xsl:value-of select="phone/tel[@type=wk]"/>
            <br />
        HM: <xsl:value-of select="phone/tel[@type=hm]"/>
    </td>
    <td><xsl:value-of select="phone/fax"/></td>
    <td><xsl:value-of select="email"/></td>
  </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 cellpadding="5" bgcolor="#cccccc">
         <tr>
            <th>Number</th>
            <th>Name</th>
            <th>Hire Date</th>
            <th>Address</th>
            <th>Phone</th>
            <th>Fax</th>
            <th>Email</th>
         </tr>
           
         <tr>
            <td>1</td>
            <td>JBrianS</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>



Put xml document to a table layout

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
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
       version="1.0"
       xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <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>name 1</td>
            <td>g@gmail.ru</td>
         </tr>
         <tr>
            <td>name 2</td>
            <td>n@hotmail.ru</td>
         </tr>
      </table>
   </body>
</html>



Variable for document

File: Data.xml
<?xml version="1.0" encoding="UTF-8"?>
<employee eid="1" dept="programming">
  <contact type="spouse">
    <name>
      <firstName>J</firstName>
      <middleName int="A">A</middleName>
      <lastName>S</lastName>
    </name>
    <address>
      <street>1 Drive</street>
      <city>Vancouver</city>
      <state>BC</state>
      <zipcode>80210</zipcode>
    </address>
    <phone>
      <tel type="wk">303-4668903</tel>
      <tel type="hm">222-222222</tel>
      <fax>303-4667357</fax>
    </phone>
    <email>j@hotmail.ru</email>
  </contact>
</employee>
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="xml" indent="yes" />
  <xsl:param name="doc"
    select="employees/employee[1]/contact/@addInfo" />
  <xsl:variable name="contacts" select="document($doc)" />
  <xsl:template match="/">
    <html>
      <head>
        <title>Email Listing</title>
      </head>
      <body>
        Your search brought the following results:
        <xsl:value-of select="$contacts/*/*/*/firstName" />
        <xsl:text> </xsl:text>
        <xsl:copy-of select="$contacts/*/*/*/lastName/text()" />
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
<html>
   <head>
      <title>Email Listing</title>
   </head>
   <body>
        Your search brought the following results:
         </body>
</html>