XML Tutorial/XPath/axis — различия между версиями

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

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

All axes were used in this example

   <source lang="xml">

File: Data.xml <?xml version="1.0" encoding="utf-8"?> <doc>

 <ancprec>
   <paragraph>
     Preceeding Ancestor.
     
</paragraph> </ancprec>

</doc> 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="/">
   <xsl:apply-templates select="//me" />
 </xsl:template>
 <xsl:template match="br">
   
</xsl:template> <xsl:template match="me" priority="10"> <html> <head> <title> <xsl:text>Document</xsl:text> </title> </head> <body>

Following Axis

       <xsl:apply-templates select="following::*/paragraph" />

Descendant or Self Axis

       <xsl:apply-templates select="descendant-or-self::*/paragraph" />

Descendant Axis

       <xsl:apply-templates select="descendant::*/paragraph" />

Self Axis

       <xsl:apply-templates select="self::*/paragraph" />

Child Axis

       <xsl:apply-templates select="child::*/paragraph" />

Following Axis

       <xsl:apply-templates select="following::*/paragraph" />

Following Sibling Axis

       <xsl:apply-templates select="following-sibling::*" />

Attribute Axis

       <xsl:apply-templates select="attribute::*" />

Parent Axis

       <xsl:apply-templates select="parent::*/paragraph" />

Ancestor or Self Axis

       <xsl:apply-templates select="ancestor-or-self::*/paragraph" />

Ancestor Axis

       <xsl:apply-templates select="ancestor::*/paragraph" />

Preceding Sibling Axis

       <xsl:apply-templates select="preceding-sibling::*/paragraph" />
     </body>
   </html>
 </xsl:template>

</xsl:stylesheet></source>


Check current element value, the output

   <source lang="xml">

File: Data.xml <?xml version = "1.0"?> <books>

  <book> 
     <title>Java How to Program</title>
     <translation edition = "1">Spanish</translation>
     <translation edition = "1">Chinese</translation>
     <translation edition = "1">Japanese</translation>
     <translation edition = "2">French</translation>
     <translation edition = "2">Japanese</translation>
  </book>
  <book> 
     <title>C++ How to Program</title>
     <translation edition = "1">Korean</translation>
     <translation edition = "2">French</translation>
     <translation edition = "2">Spanish</translation>
     <translation edition = "3">Italian</translation>
     <translation edition = "3">Japanese</translation>
  </book>

</books>

File: Transform.xslt <?xml version = "1.0"?> <xsl:stylesheet version="1.0"

 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:template match="/">
   <html>
     <body>
    <xsl:for-each select="books/book/translation"> <xsl:if test=". = "Japanese"">
  • <xsl:value-of select="../title" /> - Edition: <xsl:value-of select="@edition" />
  •            </xsl:if>
             </xsl:for-each>
    
     </body>
   </html>
 </xsl:template>

</xsl:stylesheet> Output: <html>

  <body>
  • Java How to Program - Edition: 1
  • Java How to Program - Edition: 2
  • C++ How to Program - Edition: 3
  </body>

</html></source>


Decendents of

   <source lang="xml">

File: Data.xml <?xml version = "1.0"?> <product>

  <books>
     <book>
        Getting Started with Microsoft Visual C++ 
     </book>
     <book>C How to Program</book>
  </books>

</product> File: Transform.xslt <?xml version = "1.0"?> <xsl:stylesheet version = "1.0"

        xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">
  <xsl:template match = "/product">
           Decendents of 
           <xsl:apply-templates select = "books//node()"/>
  </xsl:template>

</xsl:stylesheet> Output: <?xml version="1.0" encoding="UTF-8"?>

           Decendents of 
           
     
        Getting Started with Microsoft Visual C++ 
     
        Getting Started with Microsoft Visual C++ 
     
     C How to ProgramC How to Program</source>
   
  

Element level matching

   <source lang="xml">

File: Data.xml <?xml version="1.0"?> <CATS>

 <SIAMESE>
   <NAME>A</NAME>
   <NAME>B</NAME>
 </SIAMESE>

</CATS> File: Transform.xslt <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

 version="1.0">
 <xsl:output method="text" />
 <xsl:template match="/">
   <xsl:apply-templates select="CATS/SIAMESE/NAME" />
 </xsl:template>
 <xsl:template match="CATS/SIAMESE/NAME">
   <xsl:value-of select="." />
   <xsl:text> </xsl:text>
 </xsl:template>

</xsl:stylesheet> Output: A B</source>


* for level

   <source lang="xml">

File: Data.xml <?xml version="1.0" encoding="UTF-8"?> <employee eid="1" dept="programming">

 <contact type="spouse">
   <name>
     <firstName>Jill</firstName>
     <middleName int="A">Alicia</middleName>
     <lastName>Smith</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>


If an element has a child

   <source lang="xml">

File: Data.xml <?xml version="1.0" standalone="yes"?> <poem author="jm" year="2008">

 <verse>line 1</verse>
 <verse>line 2</verse>
 <verse>line 3</verse>
 <verse>line 4</verse>

</poem> File: Transform.xslt <?xml version="1.0" standalone="yes"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

 version="1.0">
 <xsl:output method="text" />
 <xsl:template match="poem">
   <xsl:if test="verse">
     The poem has at least one verse child element.
   </xsl:if>
 </xsl:template>

</xsl:stylesheet> Output:

     The poem has at least one verse child element.</source>
   
  

If an element has more than 3 child elements

   <source lang="xml">

File: Data.xml <?xml version="1.0" standalone="yes"?> <poem author="jm" year="1667">

 <verse>line 1</verse>
 <verse>line 2</verse>
 <verse>line 3</verse>
 <verse>line 4</verse>

</poem> File: Transform.xslt <?xml version="1.0" standalone="yes"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

 version="1.0">
 <xsl:output method="text" />
 <xsl:template match="poem">
   <xsl:if test="count(verse) > 3">
     The poem has more than 3 verse child elements.
   </xsl:if>
 </xsl:template>

</xsl:stylesheet> Output:

     The poem has more than 3 verse child elements.</source>
   
  

If element has value

   <source lang="xml">

File: Data.xml <?xml version="1.0"?> <addressbook>

 <address>
   <name>
     <title>Mr.</title>
     <first-name>Bond</first-name>
     <last-name>James</last-name>
   </name>
   <street>1234 Main Street</street>
   <city>LA</city>
   <state>WI</state>
   <zip>48392</zip>
 </address>

</addressbook> File: Transform.xslt <?xml version="1.0"?> <xsl:stylesheet version="1.0"

 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>
 <xsl:template match="/">
   <xsl:apply-templates select="addressbook/address">
     <xsl:sort select="name/last-name"/>
     <xsl:sort select="name/first-name"/>
   </xsl:apply-templates>
 </xsl:template>
 <xsl:template match="address">
   <xsl:if test="name/title">
     <xsl:value-of select="name/title"/>
     <xsl:text> </xsl:text>
   </xsl:if>
   <xsl:value-of select="name/first-name"/>
   <xsl:text> </xsl:text>
   <xsl:value-of select="name/last-name"/>
   <xsl:text>
</xsl:text>
   <xsl:value-of select="street"/>
   <xsl:text>
</xsl:text>
   <xsl:value-of select="city"/>
   <xsl:text>, </xsl:text>
   <xsl:value-of select="state"/>
   <xsl:text>  </xsl:text>
   <xsl:value-of select="zip"/>
   <xsl:text>
</xsl:text>
   <xsl:text>
</xsl:text>
 </xsl:template>

</xsl:stylesheet> Output: Mr. Bond James 1234 Main Street LA, WI 48392</source>


If one element has at least one child element

   <source lang="xml">

File: Data.xml <?xml version="1.0" standalone="yes"?> <poem author="jm" year="2008">

 <verse>line 1</verse>
 <verse>line 2</verse>
 <verse>line 3</verse>
 <verse>line 4</verse>

</poem> File: Transform.xslt <?xml version="1.0" standalone="yes"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

 version="1.0">
 <xsl:output method="text" />
 <xsl:template match="poem">
   <xsl:if test="shipDate">
     The poem has at least one shipDate child element.
   </xsl:if>
 </xsl:template>

</xsl:stylesheet></source>


match element by name

   <source lang="xml">

File: Data.xml <?xml version="1.0" encoding="UTF-8"?> <SeasonStats>

 <Player id="p0001">
   <Name>Jordon</Name>
 </Player>

</SeasonStats>

File: Transform.xslt <?xml version="1.0" encoding="iso-8859-1" ?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

 version="1.0">
 <xsl:output method="text" />
 <xsl:template match="/">
   <xsl:apply-templates />
 </xsl:template>
 <xsl:template match="Player">
   <xsl:apply-templates select="Name" />
 </xsl:template>
 <xsl:template match="Name">
   <xsl:value-of select="." />
 </xsl:template>

</xsl:stylesheet> Output:

 Jordon</source>
   
  

match="@*|node()"

   <source lang="xml">

File: Data.xml <colors>

 <color>red</color>
 <color>yellow</color>
 <color>blue</color>
 
 <color></color>

</colors>

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="@*|node()">
   <xsl:copy>
     <xsl:apply-templates select="@*|node()" />
   </xsl:copy>
 </xsl:template>

</xsl:stylesheet> Output: <colors>

 <color>red</color>
 <color>yellow</color>
 <color>blue</color>
 
 <color/>

</colors></source>


Math calculation with current value

   <source lang="xml">

File: Data.xml <math>

    <operand>12</operand>
    <operand>23</operand>
    <operand>45</operand>
    <operand>56</operand>
    <operand>75</operand>

</math> File: Transform.xslt <?xml version="1.0"?> <xsl:stylesheet version="1.0"

 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text" />
 <xsl:template match="operand">
   <xsl:value-of select="." />
   <xsl:text> + 25 = </xsl:text>
   <xsl:value-of select=". + 25" />
   <xsl:text>
</xsl:text>
   <xsl:value-of select="." />
   <xsl:text> * 25 = </xsl:text>
   <xsl:value-of select=". * 25" />
   <xsl:text>
</xsl:text>
 </xsl:template>

</xsl:stylesheet> Output:

    12 + 25 = 37

12 * 25 = 300

    23 + 25 = 48

23 * 25 = 575

    45 + 25 = 70

45 * 25 = 1125

    56 + 25 = 81

56 * 25 = 1400

    75 + 25 = 100

75 * 25 = 1875</source>


Modeling XML Documents with Axis

   <source lang="xml">

<?xml version="1.0" encoding="UTF-8"?> <Book>

   <Chapter>Some content</Chapter> 
   <Appendix>Some appendix content.</Appendix> 

</Book> When giving street directions, you have four basic directions: north, south, east, and west. In XPath, a direction is called an axis. In XPath, there are 13 directions called Axes.

   child axis 
   attribute axis 
   ancestor axis 
   ancestor-or-self axis 
   descendant axis 
   descendant-or-self axis 
   following axis 
   following-sibling axis 
   namespace axis (not used in XQuery, and deprecated in XPath 2.0) 
   parent axis 
   preceding axis 
   preceding-sibling axis 
   self axis 

In XPath, you might write something like this: /Book/Chapter[@number=2] A relative location path could be written as follows: Chapter[@number=2]</source>


select dot for value-of

   <source lang="xml">

File: Data.xml <?xml version="1.0"?> <root ref="rootref" name="blue">

 <node1>
   <node2>
      <node3>value</node3>
   </node2>   
 </node1>

</root> File: Transform.xslt <?xml version="1.0"?> <xsl:stylesheet version="1.0"

 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:template match="/">
   <xsl:apply-templates select="root/node1/node2/node3" />
 </xsl:template>
 <xsl:template match="node3">
   <xsl:value-of select="." />
 </xsl:template>

</xsl:stylesheet> Output: <?xml version="1.0" encoding="UTF-8"?>value</source>


select element by index

   <source lang="xml">

File: Data.xml <?xml version="1.0"?> <mammals locale="North America">

<mammal junk="yyy">A</mammal>
<mammal>B</mammal>
<mammal>C</mammal>
<mammal>D</mammal>
<mammal>E</mammal>
<mammal>F</mammal>
<mammal>G</mammal>

</mammals>

File: Transform.xslt <?xml version="1.0" encoding="US-ASCII"?> <xsl:stylesheet version="1.0"

 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="xml" indent="yes" />
 <xsl:template match="mammals">
   <north.american>
     <mammal>
       <cat>
         <xsl:apply-templates select="mammal[6]" />
       </cat>
     </mammal>
   </north.american>
 </xsl:template>

</xsl:stylesheet> Output: <?xml version="1.0" encoding="UTF-8"?> <north.american>

  <mammal>
     <cat>F</cat>
  </mammal>

</north.american></source>


selects only elements which occurs first in each level

   <source lang="xml">

File: Data.xml <?xml version="1.0" encoding="utf-8"?>

   <contact>
     <name>A</name>
     <street>B</street>
     <number>7</number>
     <tel>
       <home>1111111</home>
       <work>7777777</work>
     </tel>
   </contact>
   <contact>
     <name>C</name>
     <street>D</street>
     <number>75</number>
     <tel>
       <home>21111111</home>
       <work>333333333</work>
     </tel>
   </contact>

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="/">
<xsl:for-each select="//*[1]"> <xsl:call-template name="generalTemplate"/> </xsl:for-each>
   </xsl:template>
   <xsl:template name="generalTemplate">
     <TR>
       <TD>
         <xsl:value-of select="name(.)"/>
       </TD>
     </TR>
   </xsl:template>

</xsl:stylesheet> Output:

<?xml version="1.0" encoding="UTF-8"?>
data
contact
name
home
name
home
</source>