简体   繁体   English

XSLT使用XSLT从xml删除一个元素的所有属性

[英]XSLT Remove all attribute for one element from xml using XSLT

I have the following root element of a big XML file: 我有一个大XML文件的以下根元素:

<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: 相反,只需匹配元素并输出另一个不带有上述名称空间的“负担”的Interchange

<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: 确保同时在XSLT样式表中定义所需的名称空间:

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

It is irrelevant which prefix ( eb in this case) is used. 使用哪个前缀(在这种情况下为eb )是无关紧要的。 But this makes sure that the Interchange element is found. 但这可以确保找到Interchange元素。 This is crucial because <Interchange> and <Interchange xmlns='http://www.e2b.no/XMLSchema'> are different elements. 这是至关重要的,因为<Interchange><Interchange xmlns='http://www.e2b.no/XMLSchema'>不同的元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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