XML Tutorial/XPath/ancestor

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

for-each select="ancestor::*"

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>
        <tr>
          <th colspan="2">Axis: ancestor</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="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"?><table><tr><th colspan="2">Axis: ancestor</th></tr><tr><td>Element</th><td>Node-set</th></tr></table>


Is product the ancestor of "Java"?

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


select ancestor::*

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">
    <h1>Unaccounted for para element</h1>
    <h2>Ancestors</h2>
    <xsl:for-each select="ancestor::*">
      <xsl:value-of select="name()" />
      <xsl:if test="position() != last()">
        <xsl:text>, </xsl:text>
      </xsl:if>
    </xsl:for-each>
    <h2>Content</h2>
    <xsl:apply-templates />
  </xsl:template>
  
  <xsl:template match="book">
    <html>
      <body>
        <xsl:apply-templates />
      </body>
    </html>
  </xsl:template>
  <xsl:template match="title">
    <h1>
      <xsl:apply-templates />
    </h1>
  </xsl:template>
  <xsl:template match="chapter/para">
    <paragraph>
      <font face="times">
        <xsl:apply-templates />
      </font>
    </paragraph>
  </xsl:template>
  <xsl:template match="appendix/para">
    <paragraph>
      <font face="arial">
        <xsl:apply-templates />
      </font>
    </paragraph>
  </xsl:template>
</xsl:stylesheet>


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

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>