[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: How can I get the XPATH of the current node with M

Subject: RE: 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
xpath current node
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:  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(@*) &gt; 0)">
                <xsl:text>[</xsl:text>

                <xsl:for-each select="attribute::node()">
                    <xsl:value-of select="local-name()"/>
                    <xsl:text>=&#34;</xsl:text>
                    <xsl:value-of select="."/>
                    <xsl:text>&#34;</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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.