OASIS Mailing List ArchivesView the OASIS mailing list archive below
or browse/search using MarkMail.


Help: OASIS Mailing Lists Help | MarkMail Help



   RE: [xml-dev] XSLT & XPATH absolute path

[ Lists Home | Date Index | Thread Index ]

The suggested template fails if there is more than one occurrence of the
path the the elements, not because of the match, but because the path
produced leads to a nodeset containing more than one element.  

Here is a document that the original stylesheet below will fail on.  It will
produce /bookmarks/bookmark/item/findme, which does not uniquely identify
either of the abc or def findme elements in this document:
  <?xml version="1.0" ?>

To fix this problem, you also need to introduce explicit [1], [2], etc at
each step.  If the document is stable, then all you need is the position of
the elements, not the names.
An XSLT wizard such as Michael Kay probably has a better way to do this, but
try this version of the stylesheet, which instead of names uses a repeated
"/*[n]" construct to address an element uniquely, and which leaves off the
trailing slash.

<xsl:template match="findme">
   <xsl:for-each select="ancestor-or-self::*">
      <xsl:value-of select="1+count(preceding-sibling::*)"/>

If you need to produce an XPath expression that finds the node even if the
rest of the document has been modified, then you can stick in an ID
addtibute using generate-id before the modifications.  If you can't do that,
you need more help.

-----Original Message-----
From: Mitch Amiano [mailto:mamiano@nc.rr.com] 
Sent: Friday, September 05, 2003 5:21 AM
To: Alejandro López
Cc: 'xml-dev@lists.xml.org'
Subject: Re: [xml-dev] XSLT & XPATH absolute path

The ancestor-or-self:: axis pretty much gives you that information.

Assuming your node is an element named "findme", you can do something like

<xsl:template match="findme">
   <xsl:for-each select="ancestor-or-self::*">
      <xsl:value-of select="name()"/><xsl:text>/</xsl:text>

An xsl:choose can help you figure out which nodes are elements versus text
or attributes, in which case you can deal with the last path segment
separately by using the "ancestor::" axis instead of "ancestor-or-self::".

Alejandro López wrote:
> Hello everybody.
> Does anybody know if there is a way in xpath to know the absolute path of
> node in order to use with transformation functions?
> Thanks


News | XML in Industry | Calendar | XML Registry
Marketplace | Resources | MyXML.org | Sponsors | Privacy Statement

Copyright 2001 XML.org. This site is hosted by OASIS