XML Tutorial/XPath/child

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

Child Axis

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


Child of title element for the second completeTrainingCourse is

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>


for-each children

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>


for-each select="child::*"

File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<data>
    <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>
</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 border="1" cellpadding="6">
        <tr>
          <th colspan="2">Axis: child</th>
        </tr>
        <tr>
          <td>Element</th>
          <td>Node-set</th>
        </tr>
        <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"?><table border="1" cellpadding="6"><tr><th colspan="2">Axis: child</th></tr><tr><td>Element</th><td>Node-set</th></tr></table>


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

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


match="name[4]/@title"

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>


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

File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<data>
  <pedestrian>
    <firstName>Joe</firstName>
    <surname>Smith</surname>
  </pedestrian>
  <driver>
    <firstName>Doris</firstName>
    <surname>Smith</surname>
  </driver>
</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="driver/*">
      <i>
        <xsl:value-of select="."/>
      </i>
    </xsl:template>
    <xsl:template match="pedestrian/*">
      <B>
        <xsl:value-of select="."/>
      </B>
    </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
  
    <B>Joe</B>
    <B>Smith</B>
  
  
    <i>Doris</i>
    <i>Smith</i>


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

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>


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

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