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

Re: Filtering elements of a tree

Subject: Re: Filtering elements of a tree
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Wed, 8 May 2002 09:43:23 +0100
treenode element javascript
Hi Venkatesh,

> I want to select the element artist with id=7 and its parent, grand
> parent and so on.

It's easiest to do this with a top-down transformation, only creating
a TreeNode element for an element that you encounter if it has an
element with id="7" somewhere in its descendants (or rather, if it is
an ancestor of an element with id="7".

First, you should hold all the elements that are ancestors of, or are
themselves, elements with an id of 7, in a global variable, so that you
only have to search through the tree for them once:

<xsl:variable name="selected"
              select="//*[@id = '7']/ancestor-of-self::*" />

Then you need a template that matches any element:

<xsl:template match="*">
  ...
</xsl:template>

and tests to see whether it is one of the selected nodes:

<xsl:template match="*">
  <xsl:if test="$selected[generate-id() = generate-id(current())]">
    ...
  </xsl:if>
</xsl:template>

If it is, then you want to create a TreeNode element, with copied
attributes from this one, and move on to process its child elements in
the same way:

<xsl:template match="*">
  <xsl:if test="$selected[generate-id() = generate-id(current())]">
    <TreeNode>
      <xsl:copy-of select="@*" />
      <xsl:apply-templates select="*" />
    </TreeNode>
  </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.