[xsl] How can I get the XPATH of the current node with MSXML?

Subject: [xsl] How can I get the XPATH of the current node with MSXML?
From: René de Vries <RdVries@xxxxxxxxxxx>
Date: Wed, 31 Jul 2002 12:44:05 +0200
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


Current Thread