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

"Martin Honnen [email protected]" <[email protected]> Sat, 22 May 2021 19:03:38 -0000
Newsgroups gmane.text.xml.xsl.general.mulberrytech
Organization Liberty Development
Message-ID <[email protected]>
On 22.05.2021 20:51, Sam Spade [email protected] wrote:
> As you can see,=C2=A0 the unwanted attribute *currencyScheme* still persi=
sts.
> 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=3D"http://www.w3.org/1999/XSL/Transform"
   version=3D"3.0"
   xmlns:xs=3D"http://www.w3.org/2001/XMLSchema"
   exclude-result-prefixes=3D"#all">

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

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

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

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

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

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

   <xsl:template match=3D"@*">
       <xsl:element name=3D"{local-name()}" namespace=3D"{$namespace}">
         <xsl:value-of select=3D"."/>
       </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=3D"@*[not(local-name() =3D $keepAttr)]"/>`.
--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/3329386
or by email: [email protected]
--~--