XSLT Remove all attribute for one element from xml

2019-08-17 00:07发布

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>

标签: xml xslt
1条回答
男人必须洒脱
2楼-- · 2019-08-17 00:49

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.

查看更多
登录 后发表回答