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

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

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

All axes were used in this example

File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<doc>
  <ancprec>
    <paragraph>
      Preceeding Ancestor.
      <br />
    </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">
    <br />
  </xsl:template>
  <xsl:template match="me" priority="10">
    <html>
      <head>
        <title>
          <xsl:text>Document</xsl:text>
        </title>
      </head>
      <body>
        <H2>Following Axis</H2>
        <xsl:apply-templates select="following::*/paragraph" />
        <H2>Descendant or Self Axis</H2>
        <xsl:apply-templates select="descendant-or-self::*/paragraph" />
        <H2>Descendant Axis</H2>
        <xsl:apply-templates select="descendant::*/paragraph" />
        <H2>Self Axis</H2>
        <xsl:apply-templates select="self::*/paragraph" />
        <H2>Child Axis</H2>
        <xsl:apply-templates select="child::*/paragraph" />
        <H2>Following Axis</H2>
        <xsl:apply-templates select="following::*/paragraph" />
        <H2>Following Sibling Axis</H2>
        <xsl:apply-templates select="following-sibling::*" />
        <H2>Attribute Axis</H2>
        <xsl:apply-templates select="attribute::*" />
        <H2>Parent Axis</H2>
        <xsl:apply-templates select="parent::*/paragraph" />
        <H2>Ancestor or Self Axis</H2>
        <xsl:apply-templates select="ancestor-or-self::*/paragraph" />
        <H2>Ancestor Axis</H2>
        <xsl:apply-templates select="ancestor::*/paragraph" />
        <H2>Preceding Sibling Axis</H2>
        <xsl:apply-templates select="preceding-sibling::*/paragraph" />
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>


Check current element value, the output

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>
        <ul>
          <xsl:for-each select="books/book/translation">
            <xsl:if test=". = "Japanese"">
              <li>
                <strong>
                  <xsl:value-of select="../title" />
                </strong>
                - Edition:
                <strong>
                  <xsl:value-of select="@edition" />
                </strong>
              </li>
            </xsl:if>
          </xsl:for-each>
        </ul>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>
Output:
<html>
   <body>
      <ul>
         <li><strong>Java How to Program</strong>
                            - Edition:
                            <strong>1</strong></li>
         <li><strong>Java How to Program</strong>
                            - Edition:
                            <strong>2</strong></li>
         <li><strong>C++ How to Program</strong>
                            - Edition:
                            <strong>3</strong></li>
      </ul>
   </body>
</html>


Decendents of

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


Element level matching

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


* for level

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>


If an element has a child

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.


If an element has more than 3 child elements

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.


If element has value

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>&#xA;</xsl:text>
    <xsl:value-of select="street"/>
    <xsl:text>&#xA;</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>&#xA;</xsl:text>
    <xsl:text>&#xA;</xsl:text>
  </xsl:template>
</xsl:stylesheet>
Output:
Mr. Bond James
1234 Main Street
LA, WI  48392


If one element has at least one child element

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>


match element by name

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


match="@*|node()"

File: Data.xml
<colors>
  <color>red</color>
  <color>yellow</color>
  <color>blue</color>
  <!-- 
    Next color element has whitespace content. 
  -->
  <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>
  <!-- 
    Next color element has whitespace content. 
  -->
  <color/>
</colors>


Math calculation with current value

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>&#10;</xsl:text>
    <xsl:value-of select="." />
    <xsl:text> * 25 = </xsl:text>
    <xsl:value-of select=". * 25" />
    <xsl:text>&#10;</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


Modeling XML Documents with Axis

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


select dot for value-of

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


select element by index

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>


selects only elements which occurs first in each level

File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<data>
    <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>
</data>
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="/">
      <TABLE>
        <xsl:for-each select="//*[1]">
          <xsl:call-template name="generalTemplate"/>
        </xsl:for-each>
      </TABLE>
    </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"?><TABLE><TR><TD>data</TD></TR><TR><TD>contact</TD></TR><TR><TD>name</TD></TR><TR><TD>home</TD></TR><TR><TD>name</TD></TR><TR><TD>home</TD></TR></TABLE>