XML/XSLT stylesheet/Attribute

Материал из Web эксперт
Версия от 11:26, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

attribute omitted

   <source lang="xml">

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>

</source>
   
  


compare attribute value

   <source lang="xml">

File: Data.xml

   <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>

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:

   <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>
   
   

</source>
   
  


Get attribute from different level

   <source lang="xml">

File: Data.xml

 <winery year="1998">B</winery>

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>
</source>
   
  


get attribute name

   <source lang="xml">

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>

</source>
   
  


Get value of attribute with @

   <source lang="xml">

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>
        
           <xsl:value-of select="@number" />:
         
        <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
 
</source>
   
  


if there is an attribute

   <source lang="xml">

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

</source>
   
  


List the attribute names and values

   <source lang="xml">

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
     
</source>
   
  


select node by attribute value

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

</html>

</source>
   
  


Set attribute value in tranformation

   <source lang="xml">

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>

Our neighbours

Venus

  • A 5
  • B: 0
  • C: 4
  • D: 2
  • E: 4

Mars

  • A 4
  • B: 2
  • C: 3
  • D: 4
  • E: 6
  </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>

<a href="http://www.nasa.gov/">Visit NASA!</a> | <a href="http://www.nineplanets.org/">Tour the solar system</a>

Quick reference

    <xsl:for-each select="h2">
  • <a> <xsl:attribute name="href"> #<xsl:value-of select="text()"/></xsl:attribute> <xsl:value-of select="text()"/> </a>
  •        </xsl:for-each>
    
     <xsl:apply-templates/>

     Copyright Planetary Fun 2006.
   </body>
 </xsl:template>
 <xsl:template match="h2">
   <a>
     <xsl:attribute name="name"><xsl:value-of select="text()"/></xsl:attribute>

<xsl:apply-templates/>

   </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>

<a href="http://www.nasa.gov/">Visit NASA!</a> | <a href="http://www.nineplanets.org/">Tour the solar system</a>

Quick reference

  • <a href="%0A #Venus">Venus</a>
  • <a href="%0A #Mars">Mars</a>

Our neighbours

     <a name="Venus">

Venus

</a>
  • A 5
  • B: 0
  • C: 4
  • D: 2
  • E: 4
     <a name="Mars">

Mars

</a>
  • A 4
  • B: 2
  • C: 3
  • D: 4
  • E: 6

     Copyright Planetary Fun 2006.
     
  </body>
  

</html>

</source>