XML/XSLT stylesheet/ancestor or self

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

ancestor-or-self demo

File: Data.xml
<chapter>
  <section xml:lang="de">
    <warning>this is a test</warning>
  </section>
</chapter>
File: Transform.xslt
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:output method="xml" omit-xml-declaration="yes" indent="no" />
  <xsl:template match="warning">
    <xsl:if test="ancestor-or-self::*[@xml:lang][1]/@xml:lang="en"">
      <p>
        <b>Warning!</b>
        <xsl:apply-templates />
      </p>
    </xsl:if>
    <xsl:if
      test="ancestor-or-self::*[@xml:lang][1]/@xml:lang="de"">
      <p>
        <b>test!</b>
        <xsl:apply-templates />
      </p>
    </xsl:if>
  </xsl:template>
  
</xsl:stylesheet>
Output:

  
    <p><b>test!</b>this is a test</p>