xsl-list
[Top] [All Lists]

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

2021-05-22 14:03:36
On 22.05.2021 20:51, Sam Spade anonymousjuly1(_at_)yahoo(_dot_)ca wrote:
As you can see,  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)]"/>`.
--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--


<Prev in Thread] Current Thread [Next in Thread>