Re: [xsl] regarding xpath

Subject: Re: [xsl] regarding xpath
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Wed, 24 Apr 2002 11:21:38 +0100
Hi William,

> to print the college names,
> if i write an XPATH expression like
> -----------------------------------------------
> <xsl:for-each select="/colleges/college/name">
> <xsl:value-of select="."/>.
> </xsl:for-each>
> -----------------------------------------------

This selects all the name elements that are children of college
elements that are children of the colleges element that is the
document element (the top-most element in your XML document).

> -----------------------------------------------
> <xsl:for-each select="//colleges/college/name">
> <xsl:value-of select="."/>.
> </xsl:for-each>
> -----------------------------------------------

This selects all the name elements that are children of college
elements that are children of colleges elements at any level of the
document. This will give you a different result from the path above if
you have a colleges element (with college children with name children)
elsewhere in your document.

> -----------------------------------------------
> <xsl:for-each select="//college/name">
> <xsl:value-of select="."/>.
> </xsl:for-each>
> -----------------------------------------------

This selects all the name elements that are children of college
elements at any level of the document. This will give you a different
result from the path above if you have college elements (with name
children) anywhere other than within colleges elements.

> for all the above XPATH expressions in select , i seem to be getting
> the names. If so then what is the significant difference between the
> three statements??

They are significantly different. You should use the first one --
there's no point getting the processor to look for colleges elements
throughout the document if you know the colleges element is always the
document element. Similarly there's no point getting the processor to
look for college elements throughout the document if you know that
they only appear as children of the colleges element.

> the above code is in <xsl:template match = "/"> ( from root )

That makes no difference -- all your paths are absolute paths (they
start with '/') which means that wherever they occur within your
stylesheet they will get the same thing (well, assuming you don't
switch to a different source document, that is).

Cheers,

Jeni

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


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


Current Thread