[xsl] Copy XPATH

Subject: [xsl] Copy XPATH
From: "Byomokesh Sahoo sahoo.byomokesh@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Mon, 11 Nov 2024 15:28:51 -0000
Hi,

I am trying to copy XPATH along with attributes value in an OUTPUT xml file.

XML:
<bb>
<as id="ENCA" major.version="6" minor.version="0">
    <tl>The Title</tl>
    <b/>
    <ts id="ENCA_GC">
        <tl>TS Title text</tl>
        <st jr="IB" id="EA_T1">
            <tl>ST Title Text</tl>
            <st.body>
                <pr>
                    <sp>
                        <sp.txt align="centre" cont="n">
                            <emphasis type="bold"
shading="nil">INTRODUCTION</emphasis>
                        </sp.txt>
                    </sp>
                </pr>
            </st.body>
        </st>
    </ts>
</as>
</bb>

Output Required

<as title="The Title" xpath="bb/as[1]" id="ACLL.CA">
        <ts title="TS Title text" xpath="bb/as[1]/ts[1]" id="ENCA_GC">
            <st jr="IB" title="ST Title Text" xpath="/bb/as[1]/ts[1]/st[1]"
id="EA_T1"/>
            <st.body xpath="bb/as[1]/ts[1]/st[1]/st.body[1]">
                <pr xpath="bb/as[1]/ts[1]/st[1]/st.body[1]/pr[1]">
                    <sp
xpath="bb/as[1]/ts[1]/st[1]/st.body[1]/pr[1]/sp[1]"/>
                    <sp.txt count="n"
xpath="bb/as[1]/ts[1]/st[1]/st.body[1]/pr[1]/sp[1]/sp.txt[1]">
                </sp.txt>
            </pr>
        </st.body>
    </ts>
</as>

XSL:

<xsl:template match="as">
        <xsl:element name="as">
            <xsl:attribute name="title">
                <xsl:value-of select="title/node()"/>
            </xsl:attribute>
        <xsl:attribute name="xpath">
            <xsl:value-of select ="local-name()"/>
        </xsl:attribute>
        </xsl:element>
    </xsl:template>

    <xsl:template match="ts">
        <xsl:element name="ts">
            <xsl:attribute name="title">
                <xsl:value-of select="title/node()"/>
            </xsl:attribute>
            <xsl:attribute name="xpath">
                <xsl:value-of select="fn:generateXPath(guidecard)"/>
            </xsl:attribute>
        </xsl:element>
    </xsl:template>

    <xsl:function name="fn:generateXPath" as="xs:string">
        <xsl:param name="pNode" as="node()"/>
        <xsl:for-each select="$pNode/ancestor::*">
            <xsl:value-of select="name()" />
        </xsl:for-each>
    </xsl:function>

    <xsl:template match="st.body" >
        <st.body>
        <xsl:value-of select="fn:generateXPath(st.body)"/>
        </st.body>
    </xsl:template>

Please suggest.

Thanks

Current Thread