Re: [xsl] Transform selected attribute to element when element already has a value | XSLT 2.0

Subject: Re: [xsl] Transform selected attribute to element when element already has a value | XSLT 2.0
From: "Martin Honnen martin.honnen@xxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Sat, 22 May 2021 19:03:31 -0000
On 22.05.2021 20:51, Sam Spade anonymousjuly1@xxxxxxxx wrote:
As you can see,B  the unwanted attribute *currencyScheme* still persists.
Is there a way to remove the attribute which is not in the $keepAttr list?

Perhaps in this case it is better to filter when applying templates:


<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
  version="3.0"
  xmlns:xs="http://www.w3.org/2001/XMLSchema";
  exclude-result-prefixes="#all">

  <xsl:param name="namespace"
as="xs:string">http://fc.fasset/product</xsl:param>

  <xsl:param name="root" as="xs:string">requestProduct</xsl:param>
  <xsl:param name="keepAttr" static="yes" as="xs:string*"
select="'href', 'id'"/>

  <xsl:strip-space elements="*"/>
  <xsl:output indent="yes"/>

  <xsl:template match="/*">
      <xsl:element name="{$root}" namespace="{$namespace}">
          <xsl:apply-templates select="@*[local-name() = $keepAttr],
node()"/>
      </xsl:element>
  </xsl:template>

  <xsl:template match="*">
      <xsl:element name="{local-name()}" namespace="{$namespace}">
          <xsl:apply-templates select="@*[local-name() = $keepAttr],
node()"/>
      </xsl:element>
  </xsl:template>

  <xsl:template match="*[@* and text()[normalize-space()]]">
      <xsl:element name="{local-name()}" namespace="{$namespace}">
          <xsl:apply-templates/>
      </xsl:element>
      <xsl:apply-templates select="@*[local-name() = $keepAttr]"/>
  </xsl:template>

  <xsl:template match="@*">
      <xsl:element name="{local-name()}" namespace="{$namespace}">
        <xsl:value-of select="."/>
      </xsl:element>
  </xsl:template>

</xsl:stylesheet>


As the built-in templates copy attribute values through I think you would otherwise need to prevent that with `<xsl:template match="@*[not(local-name() = $keepAttr)]"/>`.

Current Thread