XML Tutorial/XPath/ancestor

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

for-each select="ancestor::*"

   <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="ancestor::*"> <xsl:if test="not(@id)"> <xsl:value-of select="name()"/> </xsl:if> <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: ancestor
Element</th>
         <td>Node-set</th>
</table></source>


Is product the ancestor of "Java"?

   <source lang="xml">

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

  <books>
     <book>
        Getting Started with Microsoft Visual C++ 
     </book>
     <book>Java</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">
   Is product the ancestor of "Java"?
   <xsl:if
     test="name(//node()[. = "Java"]/ancestor::product) = "product"">
     Yes
   </xsl:if>
 </xsl:template>

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

   Is product the ancestor of "Java"?
   
     Yes</source>
   
  

select ancestor::*

   <source lang="xml">

File: Data.xml <book>

 <chapter>
   <title>A</title>
   <para>para1</para>
   <para>para2</para>
 </chapter>
 <chapter>
   <title>B</title>
   <para>line 2</para>
   <para>line 3</para>
 </chapter>
 <afterword>
   <para>line 4</para>
 </afterword>
 <appendix>
   <title>The Author</title>
   <para>line 5</para>
   <para>line 6</para>
 </appendix>

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

 version="1.0">
 <xsl:output method="html" />
 <xsl:template match="para">

Unaccounted for para element

Ancestors

   <xsl:for-each select="ancestor::*">
     <xsl:value-of select="name()" />
     <xsl:if test="position() != last()">
       <xsl:text>, </xsl:text>
     </xsl:if>
   </xsl:for-each>

Content

   <xsl:apply-templates />
 </xsl:template>
 
 <xsl:template match="book">
   <html>
     <body>
       <xsl:apply-templates />
     </body>
   </html>
 </xsl:template>
 <xsl:template match="title">

<xsl:apply-templates />

 </xsl:template>
 <xsl:template match="chapter/para">
   <paragraph>
     
       <xsl:apply-templates />
     
   </paragraph>
 </xsl:template>
 <xsl:template match="appendix/para">
   <paragraph>
     
       <xsl:apply-templates />
     
   </paragraph>
 </xsl:template>

</xsl:stylesheet></source>


select="ancestor::names/child::name[1]/child::family"

   <source lang="xml">

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

 <name>
   <given>A</given>
   <family>B</family>
 </name>

</names> 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="child::names" />
 </xsl:template>
 <xsl:template match="child::names">
   <xsl:apply-templates select="child::name[18]" />
 </xsl:template>
 <xsl:template match="child::name[18]">
   <xsl:value-of
     select="ancestor::names/child::name[1]/child::given" />
   <xsl:text> </xsl:text>
   <xsl:value-of
     select="ancestor::names/child::name[1]/child::family" />
   <xsl:text> is first on the list, and </xsl:text>
   <xsl:value-of select="child::given" />
   <xsl:text> </xsl:text>
   <xsl:value-of select="child::family" />
   <xsl:text> is last.</xsl:text>
 </xsl:template>

</xsl:stylesheet></source>

Axis: ancestor
Element</th><td>Node-set</th>