[XML-DEV Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] RE: XSLT & XPATH absolute path
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" ?> <bookmarks> <bookmark> <item> <findme>abc</findme> </item> </bookmark> <bookmark> <item> <findme>def</findme> </item> </bookmark> </bookmarks> 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:text>/*[</xsl:text> <xsl:value-of select="1+count(preceding-sibling::*)"/> <xsl:text>]</xsl:text> </xsl:for-each> <xsl:text>
</xsl:text> </xsl:template> 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. Leigh. -----Original Message----- From: Mitch Amiano [mailto:mamiano@n...] Sent: Friday, September 05, 2003 5:21 AM To: Alejandro López Cc: 'xml-dev@l...' Subject: Re: 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 this: <xsl:template match="findme"> <xsl:text>/</xsl:text> <xsl:for-each select="ancestor-or-self::*"> <xsl:value-of select="name()"/><xsl:text>/</xsl:text> </xsl:for-each> <xsl:text>
</xsl:text> </xsl:template> 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 a > node in order to use with transformation functions? > > Thanks >
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|