[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: xpath - how to return all nodes but the node match

Subject: Re: xpath - how to return all nodes but the node matching a value in an arbitrary tree?
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Sat, 4 May 2002 18:56:01 +0100
xpath all nodes except
Hi Scott,

> In order to "delete" a node, I'm trying to write an xpath that, when
> given an id, will return the entire tree with the exception of the
> matching node for the id. I've tried many possibilities but can't
> seem to get the thing to work -- many times it just skips the match
> all together and returns the whole tree. Since the heirarchical
> structure is arbitrary, I cannot just pinpoint the exact location of
> the node to be removed -- I suppose I could with two stylesheets,
> but I have a feeling there is a more elegant solution with a single
> xpath.
>
> Can someone please help me with the xpath that would return the
> whole tree, minus the node?

Trying to select everything bar the node you want to delete, and then
copying those nodes, won't work because you'll lose the structure. The
XSLT processor will create a deep copy of every node that you select,
and you'll end up with a a copy of the entire file for the root node,
another copy of the entire file for the document element, a copy of
the first element and all its descendants for the first element child
of the document element and so on.

The best approach for a filtering stylesheet such as this is to have
an identity template that moves through the file recursively copying
each node except for the one that you want to omit.

Start with the basic identity template:

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

Then modify it so that the copying of the matched node doesn't happen
if it has an id attribute whose value is the value of the $delete_id
parameter:

<xsl:template match="@*|node()">
  <xsl:if test="not(@id = $delete_id)">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()" />
    </xsl:copy>
  </xsl:if>
</xsl:template>

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.