Re:  [xsl] Copy XPATH

Subject: Re:  [xsl] Copy XPATH
From: "Vasu Chakkera vasucv@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Mon, 3 Feb 2025 22:31:57 -0000
will this work?
<xsl:stylesheet version="3.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
    xmlns:x="http://www.w3.org/2005/xpath-functions";
    exclude-result-prefixes="x">

    <!-- Template to match all elements -->
    <xsl:template match="*">
        <xsl:element name="{local-name()}">
            <!-- Add title attribute if the element has a title child -->
            <xsl:if test="tl">
                <xsl:attribute name="title">
                    <xsl:value-of select="tl"/>
                </xsl:attribute>
            </xsl:if>
            <!-- Generate the XPath using the path() function and remove
Q{} -->
            <xsl:attribute name="xpath">
                <xsl:value-of select="replace(path(), 'Q\{\}', '')"/>
            </xsl:attribute>
            <!-- Copy the id attribute if present -->
            <xsl:if test="@id">
                <xsl:attribute name="id">
                    <xsl:value-of select="@id"/>
                </xsl:attribute>
            </xsl:if>
            <!-- Copy other attributes except id -->
            <xsl:apply-templates select="@*[name() != 'id']"/>
            <!-- Apply templates to child elements -->
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>

    <!-- Template to copy attributes -->
    <xsl:template match="@*">
        <xsl:attribute name="{name()}">
            <xsl:value-of select="."/>
        </xsl:attribute>
    </xsl:template>

</xsl:stylesheet>

Result : <?xml version="1.0" encoding="UTF-8"?><bb xpath="/bb[1]">
<as title="The Title" xpath="/bb[1]/as[1]" id="ENCA" major.version="6"
minor.version="0">
    <tl xpath="/bb[1]/as[1]/tl[1]">The Title</tl>
    <b xpath="/bb[1]/as[1]/b[1]"/>
    <ts title="TS Title text" xpath="/bb[1]/as[1]/ts[1]" id="ENCA_GC">
        <tl xpath="/bb[1]/as[1]/ts[1]/tl[1]">TS Title text</tl>
        <st title="ST Title Text" xpath="/bb[1]/as[1]/ts[1]/st[1]"
id="EA_T1" jr="IB">
            <tl xpath="/bb[1]/as[1]/ts[1]/st[1]/tl[1]">ST Title Text</tl>
            <st.body xpath="/bb[1]/as[1]/ts[1]/st[1]/st.body[1]">
                <pr xpath="/bb[1]/as[1]/ts[1]/st[1]/st.body[1]/pr[1]">
                    <sp
xpath="/bb[1]/as[1]/ts[1]/st[1]/st.body[1]/pr[1]/sp[1]">
                        <sp.txt
xpath="/bb[1]/as[1]/ts[1]/st[1]/st.body[1]/pr[1]/sp[1]/sp.txt[1]"
align="centre" cont="n">
                            <emphasis
xpath="/bb[1]/as[1]/ts[1]/st[1]/st.body[1]/pr[1]/sp[1]/sp.txt[1]/emphasis[1]"
type="bold" shading="nil">INTRODUCTION</emphasis>
                        </sp.txt>
                    </sp>
                </pr>
            </st.body>
        </st>
    </ts>
</as>
</bb>

On Mon, 11 Nov 2024 at 07:32, Martin Honnen martin.honnen@xxxxxx <
xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:

> XSLT/XPath 3 have a "path()" function.
>
>
> 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="        <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> <http://ACLL.CA%22%3EACLL.CA%3C/a%3E>
>
> 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/620062> (by
> email <>)
>


-- 
Vasu Chakkera
NodeLogic Limited
Oxford
www.node-logic.com
==============

Current Thread