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

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

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

apply template for a document

   <source lang="xml">


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>

</source>
   
  


Apply template to a certain node in a document

   <source lang="xml">

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>

</source>
   
  


Format table cell during transforming

   <source lang="xml">

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>
<xsl:apply-templates/>
Number Name Hire Date Address Phone Fax Email
     </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"/>
       
<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]"/>
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>
Number Name Hire Date Address Phone Fax Email
1 JBrianS 2008-10-29 1 Drive
Vancouver, BC
WK:
              HM: 
303-4667357 a@a.ru
  </body>

</html>

</source>
   
  


Put xml document to a table layout

   <source lang="xml">

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>
<xsl:for-each select="emailList/person"> </xsl:for-each>
Name E-mail Address
<xsl:value-of select="name"/> <xsl:value-of select="email"/>
     </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>
Name E-mail Address
name 1 g@gmail.ru
name 2 n@hotmail.ru
  </body>

</html>

</source>
   
  


Variable for document

   <source lang="xml">

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>

</source>