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

RE: Node Position & Relationship!

Subject: RE: Node Position & Relationship!
From: "Andrew Welch" <ajwelch@xxxxxxxxxxxxxxx>
Date: Fri, 11 Feb 2005 14:41:41 -0000
xsl if position
> Given the following xml structure, I want to create an xsl if
> element that tests the current node to see if it is a level 0
> tree_node element.
>
> <?xml version="1.0"?>
> <tree>
>   <tree_node id="7" value="Test">
>       <tree_node id="8" value="Test Sub"/>
>       <tree_node id="9" value="Test Sub One">
>           <tree_node id="10" value="Test Sub Two"/>
>     </tree_node>
>   </tree_node>
> </tree>
>
>
> Here is my attached, pretty sad!!!!!
>
> <xsl:if test="{count(self::*)=1}">
>   	  <xsl:apply-templates select="tree_node"/>
> </xsl:if
>
> Can someone give me the correct way to achieve this.
> Also how can I found out if a particular node is a child,
> ancestor or peer
> Of any given node.
>
> Thanks to anyone who response, muchly appreciated.

You need to read about the "xpath axis'" (plural)

To test if a node is the child of a tree node:

  <xsl:if test="parent::tree">

To test if a tree node is the top-most tree node (dependant on
structure):

  <xsl:if test="not(parent::tree)">

Or, (again depending on the structure of your xml):

  <xsl:if test="not(ancestor::tree)">

To test if a node has siblings:

  <xsl:if test="preceding-sibling::tree or following-sibling::tree">

Or:

  <xsl:if test="../tree">

There are many, many ways.  If you are certain of the structure of your
xml you could even do it at the template match level:

  <xsl:template match="/tree">
    (level 0 tree)


  <xsl:template match="/tree/tree">
    (level 1 tree)

...and so on.

cheers
andrew

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.