XML Tutorial/XPath/following

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

following axis contains all nodes that come after the context node, but excludes all descendant nodes.

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: following</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="following::*">
            <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 border="1" cellpadding="6"><tr><th colspan="2">Axis: following</th></tr><tr><td>Element</th><td>Node-set</th></tr></table>


if test="not(descendant::*[name()=name(current())] | following::*[name()=name(current())])"

File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<data>
    <AAA>
      <BBB id="b1">
        <CCC name="q" size="12"/>
        <EEE id="e1">
          <SSS/>
        </EEE>
        <CCC weight="45"/>
        <CCC/>
      </BBB>
    </AAA>
    <AAA>
      <EEE id="e2"/>
      <CCC>
        <DDD/>
        <DDD/>
      </CCC>
    </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" width="90%">
        <xsl:apply-templates select="//*"/>
      </TABLE>
    </xsl:template>
    <xsl:template match="*">
      <xsl:if test="not(descendant::*[name()=name(current())] | following::*[name()=name(current())])">
        <TR>
          <TD>
            <xsl:value-of select="name()"/>
          </TD>
          <TD>
            <xsl:apply-templates select="." mode="list"/>
          </TD>
        </TR>
      </xsl:if>
    </xsl:template>
    <xsl:template match="*" mode="list">
      <xsl:for-each select="//*[name()=name(current())]/*">
        <xsl:value-of select="name()"/>
      </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>  
Output:
<?xml version="1.0" encoding="UTF-8"?><TABLE border="1" width="90%"><TR><TD>data</TD><TD>AAAAAA</TD></TR><TR><TD>BBB</TD><TD>CCCEEECCCCCC</TD></TR><TR><TD>SSS</TD><TD/></TR><TR><TD>AAA</TD><TD>BBBEEECCC</TD></TR><TR><TD>EEE</TD><TD>SSS</TD></TR><TR><TD>CCC</TD><TD>DDDDDD</TD></TR><TR><TD>DDD</TD><TD/></TR></TABLE>