XML Tutorial/XPath/child

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

Child Axis

   <source lang="xml">

The child axis is the default axis in XPath. The child axis selects nodes that are immediate child nodes of the context node. <Invoice>

   <Date>2008-01-02</Date> 
   <Item quantity="4">QD123</Item> 
   <Item quantity="5">AC345</Item> 

</Invoice> If the context node is the Invoice element node, the location path child::Item or, in abbreviated syntax Item will return a node-set containing both Item element nodes. To select both the Date element node and Item element node,you can write the following: child::* Or, in abbreviated syntax, use the following:

The * indicates any name, and the only nodes in the child axis that have names are element nodes. To select all child nodes, including comment nodes, processing instruction nodes, and text nodes: child::node() Or, in abbreviated syntax, use the following: node() If you want to select text node children of a context node, you can write the following: child::text() Or, in abbreviated syntax, use the following: text()</source>


Child of title element for the second completeTrainingCourse is

   <source lang="xml">

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">
   <xsl:value-of
     select="//child::completeTrainingCourse[ 2 ]/title" />
 </xsl:template>

</xsl:stylesheet></source>


for-each children

   <source lang="xml">

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

<scandinavia>
 <state>Finland</state>
 <state>Sweden</state>
 <state>Iceland</state>
 <state>Norway</state>
 <state>Denmark</state>
</scandinavia>

</europe> File: Transform.xslt <?xml version="1.0" encoding="UTF-8"?> <scandinavia xsl:version="1.0"

 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:for-each select="europe/scandinavia/state">
   <country>
     <xsl:value-of select="." />
   </country>
 </xsl:for-each>

</scandinavia> Output: <?xml version="1.0" encoding="UTF-8"?><scandinavia><country>Finland</country><country>Sweden</country><country>Iceland</country><country>Norway</country><country>Denmark</country></scandinavia></source>


for-each select="child::*"

   <source lang="xml">

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

   <AAA id="a1" pos="start">
     <BBB id="b1"/>
     <BBB id="b2"/>
   </AAA>
   <AAA id="a2">
     <BBB id="b3"/>
     <BBB id="b4"/>
     <CCC id="c1">
       <CCC id="c2"/>
     </CCC>
     <BBB id="b5">
       <CCC id="c3"/>
     </BBB>
   </AAA>

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="/source//*"> <xsl:call-template name="print"/> </xsl:for-each> </table> </xsl:template> <xsl:template name="print"> <tr> <td> <xsl:value-of select="name()"/> <xsl:text> id = </xsl:text> <xsl:value-of select="./@id"/> </td> <td> <xsl:for-each select="child::*"> <xsl:value-of select="./@id"/> <xsl:text/> </xsl:for-each> </td> </tr> </xsl:template> </xsl:stylesheet> Output: <?xml version="1.0" encoding="UTF-8"?>
Axis: child
Element</th>
         <td>Node-set</th>
</table></source>


match=child::name[@title = editor]

   <source lang="xml">

File: Data.xml <?xml version="1.0" encoding="ISO-8859-1"?> <names>

 <name title="editor">
   <last>A</last>
   <first>P</first>
 </name>
 <name>
   <last>B</last>
   <first>J</first>
 </name>
 <name>
   <last>S</last>
   <first>C</first>
 </name>

</names>

File: Transform.xslt <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0"

 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text" />
 <xsl:template match="child::name[@title = "editor"]">
   <xsl:text>- </xsl:text>
   <xsl:apply-templates select="child::first" />
   <xsl:text> </xsl:text>
   <xsl:apply-templates select="child::last" />
   <xsl:text> </xsl:text>
 </xsl:template>

</xsl:stylesheet> Output:

 - P A 
 
   B
   J
 
 
   S
   C</source>
   
  

match="name[4]/@title"

   <source lang="xml">

File: Data.xml <?xml version="1.0" encoding="ISO-8859-1"?> <names>

 <name title="editor">
   <last>A</last>
   <first>P</first>
 </name>
 <name>
   <last>B</last>
   <first>J</first>
 </name>
 <name>
   <last>S</last>
   <first>C</first>
 </name>

</names> File: Transform.xslt <?xml version="1.0" encoding="ISO-8859-1"?> <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="names" />
 </xsl:template>
 <xsl:template match="names">
   <xsl:apply-templates select="name[4]/@title" />
 </xsl:template>
 <xsl:template match="name[4]/@title">
   <xsl:text>The XML 1.0 WG"s </xsl:text>
   <xsl:value-of select="." />
   <xsl:text> was </xsl:text>
   <xsl:value-of select="../given" />
   <xsl:text> </xsl:text>
   <xsl:value-of select="../family" />
   <xsl:text>.</xsl:text>
 </xsl:template>

</xsl:stylesheet></source>


Process the element only if the element is a child of different element

   <source lang="xml">

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

 <pedestrian>
   <firstName>Joe</firstName>
   <surname>Smith</surname>
 </pedestrian>
 <driver>
   <firstName>Doris</firstName>
   <surname>Smith</surname>
 </driver>

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="driver/*">
     
       <xsl:value-of select="."/>
     
   </xsl:template>
   <xsl:template match="pedestrian/*">
     
       <xsl:value-of select="."/>
     
   </xsl:template>

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

   Joe
   Smith
 
 
   Doris
   Smith</source>
   
  

select="child::name[4]/attribute::title"

   <source lang="xml">

File: Data.xml <?xml version="1.0" encoding="ISO-8859-1"?> <names>

 <name>
   <last>A</last>
   <first>P</first>
 </name>
 <name>
   <last>B</last>
   <first>J</first>
 </name>
 <name>
   <last>S</last>
   <first>C</first>
 </name>
 <name>
   <last>T</last>
   <first>J</first>
 </name>

</names>

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="text" />
 <xsl:template match="/">
   <xsl:apply-templates select="child::names" />
 </xsl:template>
 <xsl:template match="child::names">
   <xsl:apply-templates select="child::name[4]/attribute::title" />
 </xsl:template>
 <xsl:template match="child::name[4]/attribute::title">
   <xsl:text>The XML 1.0 WG"s </xsl:text>
   <xsl:value-of select="self::node()" />
   <xsl:text> was </xsl:text>
   <xsl:value-of select="parent::name/child::given" />
   <xsl:text> </xsl:text>
   <xsl:value-of select="parent::name/child::family" />
   <xsl:text>.</xsl:text>
 </xsl:template>

</xsl:stylesheet></source>


select=child::name[@title = editor]

   <source lang="xml">

File: Data.xml <?xml version="1.0" encoding="ISO-8859-1"?> <names>

 <name title="editor">
   <last>A</last>
   <first>P</first>
 </name>
 <name>
   <last>B</last>
   <first>J</first>
 </name>
 <name>
   <last>S</last>
   <first>C</first>
 </name>
 <name>
   <last>T</last>
   <first>J</first>
 </name>

</names>

File: Transform.xslt <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0"

 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text" />
 <xsl:template match="child::names">
   <xsl:text>The editors of the XML recommendation were:</xsl:text>
   <xsl:text> </xsl:text>
   <xsl:apply-templates select="child::name[@title = "editor"]" />
 </xsl:template>

</xsl:stylesheet> Output: The editors of the XML recommendation were:

   A
   P</source>
Axis: child
Element</th><td>Node-set</th>