Search code examples
xmlxsltxml-namespacesfunctx

functx:remove-attributes adds an empty xmlns


The function functx:remove-attributes adds automatically an empty @xmlns to the node and I would like to prevent this behavior. Example xml:

<mei meiversion="3.0.0" xmlns="http://www.music-encoding.org/ns/mei">
<note accid.ges="f" doxml.id="d28e18935" dur="8" oct="4" pname="e" tstamp="1.75" xml:id="note_112296"/>
</mei>

XSLT excerpt:

<xsl:copy-of select="functx:remove-attributes(., $attributes2remove)" />

Current (false) XML output:

<note accid.ges="f" doxml.id="d28e18935" dur="8" oct="4" pname="e" tstamp="1.75" xml:id="note_112296" xmlns=""/>

Solution

  • In the function body, I think you want to use xsl:copy instead of xsl:element:

    <xsl:function name="functx:remove-attributes" as="element()"
                  xmlns:functx="http://www.functx.com">
      <xsl:param name="elements" as="element()*"/>
      <xsl:param name="names" as="xs:string*"/>
    
       <xsl:for-each select="$elements">
         <xsl:copy>
           <xsl:sequence
             select="(@*[not(functx:name-test(name(),$names))],
                     node())"/>
         </xsl:copy>
       </xsl:for-each>
    
    </xsl:function>
    

    https://xsltfiddle.liberty-development.net/ejivJrP