RE: generate xpath expression in web links
> Our web app needs to generate the XPath expression that > uniquely identifies a particular node, something like: > > <a href="/project/admin/project-manager/full-name">Some > Name</a> > Firstly, you mean "a" path expression, not "the" path expression. There are many path expressions that uniquely identify a node. Your particular version doesn't tackle namespaces, which is the biggest problem in creating such an expression. If all you are concerned about is to get back to the original element, an expression such as /*/*/*/* removes this problem. You could use <xsl:number/> rather than counting preceding siblings. Michael Kay http://www.saxonica.com/ > This is what I've come up with so far. > > =========================== > > <a><xsl:call-template name="generatePath"/><xsl:value-of > select="some-link"/></a> > > <xsl:template name="generatePath"> > <xsl:attribute name="href"> > <xsl:for-each select="ancestor-or-self::*"> > <xsl:text>/</xsl:text> > <xsl:value-of select="local-name(.)"/> > <xsl:text>[</xsl:text> > <xsl:value-of > select="count(preceding-sibling::*[local-name()=local-name(cur > rent())])+ > 1"/> > <xsl:text>]</xsl:text> > </xsl:for-each> > </xsl:attribute> > </xsl:template> > > =========================== > > It works ok, but I just wanted to check to see if I was > missing anything to make it more efficient. I'm using xslt2. > > Thanks, David
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