Subject: Re: ### Outputting the "full path". Is this possible ?? ### From: "Khalid Asad" <asad@xxxxxxxxxxxxx> Date: Fri, 26 May 2000 07:14:25 -0400 |
Interesting that this keeps coming up. I asked this questions some time ago and I found out that there were multiple ways of getting the full path (none which were complete). The technique described here works for simple cases, but what if an element contains a sequence of elements, such as <departments> <employees> <name>Joe Shmo</name> <name>Bob Shmo</name> </employees> </departments> Also, how about getting the full path for attributes? I would like to see better support from processors for outputting XPATH strings. Using SAX, when I get an element event, I'd like to see the path to that element in the document - it makes it easier for me to update the document. I'd like an XSLT processor to have a function to generate an XPATH string for nodes in the tree, rather than me having to write one. In fact, if there was a standard function to do this, then all XSLT processor providers would end up supporting it and XSL stylesheet writers would not be burdened with trying to figure out how to get it right. Khalid ----- Original Message ----- From: "Jeni Tennison" <Jeni.Tennison@xxxxxxxxxxxxxxxx> To: "Jonathan Asbell" <jonathan.asbell@xxxxxxxxxxxxxx> Cc: <xsl-list@xxxxxxxxxxxxxxxx> Sent: Thursday, May 25, 2000 1:04 PM Subject: Re: ### Outputting the "full path". Is this possible ?? ### Jonathan, >Considering this xml: > ><departments> > <employees> > <name>Joe Shmo</name> > </employees> ></employees> > >If the current node is "name", is there a simple XSL: way of outputting the >"full path", ie: >"departments/employees/name/Joe Shmo" The 'full path' that you're describing are the names of the ancestor elements of the current node, plus the current node, plus the current node's value, separated by '/'s. There are two ways of doing this: iteration and recursion. I'm going to show you iteration because it keeps everything in one place. You do iteration using an xsl:for-each element. We want to iterate over the list of the ancestor elements of the current node, but we also want to include the current node, and helpfully there is an XPath axis that enables us to do just that: ancestor-of-self. We don't care what the ancestor elements are called. So: <xsl:for-each select="ancestor-of-self::*"> ... </xsl:for-each> OK, so for each of those nodes we want to output the name of the node, followed by a forward shash and no whitespace: <xsl:for-each select="ancestor-of-self::*"> <xsl:value-of select="name()" /><xsl:text>/</xsl:text> </xsl:for-each> Once we've outputted the information about the ancestor elements, we want to round it off with information about the value of the current node: <xsl:value-of select="." /> So, the complete template is: <xsl:template match="name"> <xsl:for-each select="ancestor-of-self::*"> <xsl:value-of select="name()" /><xsl:text>/</xsl:text> </xsl:for-each> <xsl:value-of select="." /> </xsl:template> Easy, eh? Hope that helps, Jeni Dr Jeni Tennison Epistemics Ltd, Strelley Hall, Nottingham, NG8 6PE Telephone 0115 9061301 . Fax 0115 9061304 . Email jeni.tennison@xxxxxxxxxxxxxxxx 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 |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: ### Outputting the "full path"., Jeni Tennison | Thread | Re: ### Outputting the "full path"., David Carlisle |
Re: Context node/Predicates in Expr, David Carlisle | Date | Re: XSL Performance, Khalid Asad |
Month |