Re: [xsl] Copy XPATH

Subject: Re: [xsl] Copy XPATH
From: "Martin Honnen martin.honnen@xxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Mon, 3 Feb 2025 22:42:17 -0000
On 03/02/2025 23:32, Vasu Chakkera vasucv@xxxxxxxxx wrote:
will this work?


That depends on your requirements you would know better than we do. It
all depends on how you want to process the "xpath" attribute value.


<xsl:stylesheet version="3.0"
B  B  xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
B  B  xmlns:x="http://www.w3.org/2005/xpath-functions";
B  B  exclude-result-prefixes="x">

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

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

</xsl:stylesheet>

Current Thread