简体   繁体   中英

XSLT Remove all attribute for one element from xml using XSLT

I have the following root element of a big XML file:

<Interchange xmlns='http://www.e2b.no/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'   
xsi:schemaLocation='http://www.e2b.no/XMLSchema Interchange'>

I need to get

<Interchange>

Please advise. Here is a minimal document with the template I'm trying (I won't include my full attempts because they're much longer):

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>

<xsl:template match="Interchange/@xmlns|@xmlns:xsi|@xsi:schemaLocation"/>
</xsl:stylesheet>

Namespace declarations are not the same as attributes. That's why you cannot exclude them from your identity templates the way you tried it.

Instead, simply match the element and output another Interchange that is not "burdened" with the namespaces above:

<xsl:template match="eb:Interchange">
  <Interchange>
    <xsl:apply-templates/>
  </Interchange>
</xsl:template>

Make sure that you define the required namespace in your XSLT stylesheet as well:

xmlns:eb='http://www.e2b.no/XMLSchema'

It is irrelevant which prefix ( eb in this case) is used. But this makes sure that the Interchange element is found. This is crucial because <Interchange> and <Interchange xmlns='http://www.e2b.no/XMLSchema'> are different elements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM