Re: [xsl] ancestor-or-self and text nodes

Subject: Re: [xsl] ancestor-or-self and text nodes
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Thu, 11 Aug 2005 17:15:00 -0400
Hi Kenneth,

At 04:53 PM 8/11/2005, you wrote:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
    version="1.0"
        xmlns:exsl-common="http://exslt.org/common";
        extension-element-prefixes="exsl-common">

        <xsl:variable name="rtf">
                <a>
                        <b>
                                <c>some text</c>
                        </b>
                        <d>
                                <e>more text</e>
                        </d>
                </a>
        </xsl:variable>

        <xsl:template match="/">
                <xsl:apply-templates select="exsl-common:node-set($rtf)/*" />
        </xsl:template>

        <xsl:template match="node()[not(ancestor-or-self::b)]">
                <xsl:copy>
                        <xsl:apply-templates select="node()" />
                </xsl:copy>
        </xsl:template>

</xsl:stylesheet>

This produces the following output :

<?xml version="1.0" encoding="utf-8"?><a>some text<d><e>more text</e></d></a>

    Why is it that "some text" appears in the output? Surely "b" is an
ancestor of the text node "some text"?

Because failing to match your template match="node()[not(ancestor-or-self::b)]">, the <b> element is matching the built-in template for elements:


<xsl:template match="*">
  <xsl:apply-templates/>
</xsl:template>

... and so on down the tree, until the built-in template for text nodes is matched to the text node containing "some text" -- at which point it's written out.

What I think you want would be more easily accomplished with two templates, not one:

<xsl:template match="*">
  <xsl:copy>
    <xsl:apply-templates select="node()"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="b"/>

Good luck,
Wendell


====================================================================== Wendell Piez mailto:wapiez@xxxxxxxxxxxxxxxx Mulberry Technologies, Inc. http://www.mulberrytech.com 17 West Jefferson Street Direct Phone: 301/315-9635 Suite 207 Phone: 301/315-9631 Rockville, MD 20850 Fax: 301/315-8285 ---------------------------------------------------------------------- Mulberry Technologies: A Consultancy Specializing in SGML and XML ======================================================================

Current Thread