Subject: RE: [xsl] How can I get the XPATH of the current node with MSXML? From: "Macaulay,Malcolm (US)" <Malcolm.Macaulay2@xxxxxxxxx> Date: Wed, 31 Jul 2002 09:19:43 -0500 |
Hi Rene, This reply is basically the same as Jeni Tennison's, but please see my note below. Here's my 'generateXPath' named template. Call this template at the point where you want the unique XPath inserted into the output. <xsl:template name="generateXPath"> <xsl:for-each select="ancestor::*">/<xsl:value-of select="name()"/>[<xsl:number/>]</xsl:for-each>/<xsl:value-of select="name()"/>[<xsl:number/>]</xsl:template> Note: keep this template all on one line (i.e. no whitespace or nice indenting) - I use the generated XPath in a javascript function - whitespace in the template = gaps in the generate XPath = stuffed up the jscript function. Hope that helps. cheers Malcolm -----Original Message----- From: René de Vries [mailto:RdVries@xxxxxxxxxxx] Sent: Wednesday, July 31, 2002 5:44 AM To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx Subject: [xsl] How can I get the XPATH of the current node with MSXML? Hi, Is there a simple way to output the XPATH of the current node with MSXML? I'm searching the XML for certain items and want to create an XML with the XPATH's pointing to the items I searched. At the moment I take the local-name(), expand it with attributes and pass that as a parameter to sub-templates, which adds its own local-name(), expand it with attributes etc.. See example below. Is there an easier/more efficient way? It must work with MSXML, so I can't use any special functions which other parsersmay have... My XML: <Mainnode Mainattr="1"> <Subnode Subattr="2"> <SubSubnode SubSubattr="3"> <Something>There is something!</Something> </SubSubnode> </Subnode> </Mainnode> My XSL: <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" indent="yes"/> <xsl:template match="Mainnode"> <xsl:variable name="XpathString"> <xsl:call-template name="getXpath"> <xsl:with-param name="ParentName" select="'/'"/> </xsl:call-template> </xsl:variable> <root> <xsl:apply-templates select="Subnode"> <xsl:with-param name="ParentName" select="$XpathString"/> </xsl:apply-templates> </root> </xsl:template> <xsl:template match="Subnode"> <xsl:param name="ParentName"/> <xsl:variable name="XpathString"> <xsl:call-template name="getXpath"> <xsl:with-param name="ParentName" select="$ParentName"/> </xsl:call-template> </xsl:variable> <xsl:apply-templates select="SubSubnode"> <xsl:with-param name="ParentName" select="$XpathString"/> </xsl:apply-templates> </xsl:template> <xsl:template match="SubSubnode"> <xsl:param name="ParentName"/> <xsl:if test="Something"> <xsl:variable name="XpathString"> <xsl:call-template name="getXpath"> <xsl:with-param name="ParentName" select="$ParentName"/> </xsl:call-template> </xsl:variable> <output> <Xpath> <xsl:value-of select="$XpathString"/> </Xpath> <Description>Say 'Something' is out there!</Description> </output> </xsl:if> </xsl:template> <xsl:template name="getXpath"> <xsl:param name="ParentName"/> <xsl:variable name="AttrString"> <xsl:if test="(count(@*) > 0)"> <xsl:text>[</xsl:text> <xsl:for-each select="attribute::node()"> <xsl:value-of select="local-name()"/> <xsl:text>="</xsl:text> <xsl:value-of select="."/> <xsl:text>"</xsl:text> <xsl:if test="position()!=last()"> and </xsl:if> </xsl:for-each> <xsl:text>]</xsl:text> </xsl:if> </xsl:variable> <xsl:value-of select="concat($ParentName, '/',local-name(),$AttrString)"/> </xsl:template> </xsl:stylesheet> My output: <root> <output> <Xpath>//Mainnode[Mainattr="1"]/Subnode[Subattr="2"]/SubSubnode[SubSubattr=" 3"]</Xpath> <Description>Say 'Something' is out there!</Description> </output> </root> Greetings René {@ @} ^ \_/ "You don't need eyes to see, you need vision!" 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: [xsl] How can I get the XPATH o, Jeni Tennison | Thread | [xsl] Multi Level Grouping using Mu, robert . parsons |
Re: [xsl] processing-instruction(), Joerg Heinicke | Date | Re: [xsl] processing-instruction(), David Carlisle |
Month |