As Martin Honnen noted, in XPath or XSLT ver 3+ one can use the standard
path() function - https://www.w3.org/TR/xpath-functions-31/#func-path
For a solution with XSLT 1.0 (or 2.0) see this Stack Overflow answer:
https://stackoverflow.com/a/4747858/36305
Thanks,
Dimitre
On Mon, Nov 11, 2024 at 7:28b/AM Byomokesh Sahoo sahoo.byomokesh@xxxxxxxxx <
xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:
>
> 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
>
> XSL-List info and archive <http://www.mulberrytech.com/xsl/xsl-list>
> EasyUnsubscribe <http://lists.mulberrytech.com/unsub/xsl-list/782854> (by
> email <>)
|