Re: [xsl] figuring out what grandparent(?) is

Subject: Re: [xsl] figuring out what grandparent(?) is
From: "Joerg Heinicke" <joerg.heinicke@xxxxxx>
Date: Fri, 29 Mar 2002 14:34:08 +0100
Hi Eric,

here the normal template:

<xsl:template match="link">
</xsl:template>

So the context node is <link>. Then there are two ways to test on the
grandparent. Either with name() or with a specific axes and test whether the
nodeset is empty or not.

1. name()

<xsl:if test="name() = 'link' ">the name of the current node is
'link'</xsl:if>

name() without parameter returns the name of the context node. name() with
parameter returns the name of the first node in this nodeset. So name(//*)
returns the name of the first element in the whole document, which should be
the root element. For your example you need to select the grandparent from
the current node. The easiest thing is '../..' (two steps up). So
name(../..) should give you 'article' or 'smallbox' and you can test on it.

2. axes/nodeset

The other way is using axes. You can find a list of them here:
http://www.zvon.org/xxl/XSLTreference/Output/index.html. For your problem
parent and ancestor(-or-self) are the important ones. You can use the axes
with name() too: name(parent::*/parent::*) is the same as name(../..). But I
want to show you another way testing on the nodeset directly. You must
specify the name exactly instead of using parent::*:

<xsl:if test="parent::p/parent::article">the grandparent is named
'article'</xsl:if>

The XPATH expression parent::p/parent::article returns a nodeset with all
nodes, (which are grandparent of the current node and which are named
article and (which have a child p, which is the parent of the current
node) ). It's a bit difficult to write it from the eyes of 'link'. The
grandparent can be only one node and so the returned nodeset can only
contain one node or no node. This is converted to boolean false or true and
so you can use it for your test.

Hope this helps,

Joerg


> Simplified, part of my XML looks like this:
>
> <article>
>   <p>
>     <link/>
>   </p>
>   <smallbox>
>     <p>
>       <link/>
>     </p>
>   </smallbox>
> <article>
>
> When processing the <link> template, I need to know whether I'm inside of
> the <smallbox> or just in the <article>; that is, I need something like:
>
> <xsl:choose>
>   <xsl:when [descendant of smallbox]>
>     do this
>   </xsl:when>
>   <xsl:otherwise>...
>
> Explanations on the beginner level please!  Thanks much! Eric  : )
>
>
>
> ________________________________________________________________
> GET INTERNET ACCESS FROM JUNO!
> Juno offers FREE or PREMIUM Internet access for less!
> Join Juno today!  For your FREE software, visit:
> http://dl.www.juno.com/get/web/.
>
>  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
>


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


Current Thread