xsl-list
[Top] [All Lists]

Re: ancestor-or-self and text nodes

2005-08-11 14:15:00
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(_at_)mulberrytech(_dot_)com
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
======================================================================


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



<Prev in Thread] Current Thread [Next in Thread>