如何使用 XSLT 从 XML 中删除名称空间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5268182/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 14:36:09  来源:igfitidea点击:

How to remove namespaces from XML using XSLT

xmlxsltxml-namespaces

提问by GaneshT

I have a 150 MB (it can go even more sometimes) XML file. I need to remove all the namespaces. It's on Visual Basic 6.0, so I'm using DOM to load the XML. Loading is okay, I was skeptical at first, but somehow that part works fine.

我有一个 150 MB(有时甚至更大)的 XML 文件。我需要删除所有命名空间。它在 Visual Basic 6.0 上,所以我使用 DOM 加载 XML。加载没问题,起初我持怀疑态度,但不知何故这部分工作正常。

I am trying the following XSLT, but it removes all the other attributes also. I want to keep all the attributes and elements, I just need to remove the namespaces. Apparently it's because I have xsl:elementbut not attribute. How can I include the attributes there?

我正在尝试以下XSLT,但它也删除了所有其他属性。我想保留所有的属性和元素,我只需要删除命名空间。显然这是因为我有xsl:element但没有属性。我如何在那里包含属性?

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="UTF-8" />
    <xsl:template match="*">
        <xsl:element name="{local-name()}">
            <xsl:apply-templates select="@* | node()"/>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

回答by jasso

Your XSLT removes attributes also, because you don't have a template that would copy them. <xsl:template match="*">matches only elements, not attributes (or text, comments or processing instructions).

您的 XSLT 也会删除属性,因为您没有可以复制它们的模板。<xsl:template match="*">只匹配元素,不匹配属性(或文本、注释或处理指令)。

Below is a stylesheet that removes all namespace definitions from the processed document but copies all other nodes and values: elements, attributes, comments, text and processing instructions. Please pay attention to 2 things

下面是一个样式表,它从处理过的文档中删除了所有命名空间定义,但复制了所有其他节点和值:元素、属性、注释、文本和处理指令。请注意2件事

  1. Copying the attributes as such is not enough to remove all namespaces. Also an attribute can belong to a namespace, even when the containing element doesn't belong to a namespace. Therefore also attributes need to be created, like elements. Creating attributes is done with <xsl:attribute>element.
  2. A valid XML document cannot contain an element that has two or more attributes with same expanded name but elements can contain multiple attributes with same local name if the attributes have different namespaces.This means that removing the namespace prefix from an attribute name will cause dataloss if there is an element that has at leas two attributes with same local name. Other one of these attributes will be removed (or overwritten).
  1. 像这样复制属性不足以删除所有命名空间。即使包含元素不属于命名空间,属性也可以属于命名空间。因此,还需要创建属性,如元素。创建属性是通过<xsl:attribute>元素完成的。
  2. 有效的 XML 文档不能包含具有两个或多个扩展名称相同的属性的元素,但如果属性具有不同的名称空间,则元素可以包含多个具有相同本地名称的属性。这意味着如果元素至少具有两个具有相同本地名称的属性,则从属性名称中删除命名空间前缀将导致数据丢失。这些属性之一将被删除(或覆盖)。

...and the code:

...和代码:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output indent="yes" method="xml" encoding="utf-8" omit-xml-declaration="yes"/>

    <!-- Stylesheet to remove all namespaces from a document -->
    <!-- NOTE: this will lead to attribute name clash, if an element contains
        two attributes with same local name but different namespace prefix -->
    <!-- Nodes that cannot have a namespace are copied as such -->

    <!-- template to copy elements -->
    <xsl:template match="*">
        <xsl:element name="{local-name()}">
            <xsl:apply-templates select="@* | node()"/>
        </xsl:element>
    </xsl:template>

    <!-- template to copy attributes -->
    <xsl:template match="@*">
        <xsl:attribute name="{local-name()}">
            <xsl:value-of select="."/>
        </xsl:attribute>
    </xsl:template>

    <!-- template to copy the rest of the nodes -->
    <xsl:template match="comment() | text() | processing-instruction()">
        <xsl:copy/>
    </xsl:template>

</xsl:stylesheet>

You could also use <xsl:template match="node()">instead of that last template but then you should use priorityattribute to prevent elements matching to this template.

您也可以使用<xsl:template match="node()">而不是最后一个模板,但是您应该使用priority属性来防止元素与此模板匹配。

回答by Emiliano Poggi

How can I include the attributes there?

我如何在那里包含属性?

Just append this template to the one you already have:

只需将此模板附加到您已有的模板:

<xsl:template match="@*">
    <xsl:copy/>
</xsl:template>

回答by Konstantin

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" encoding="UTF-8"/>
<xsl:template match="/">
    <xsl:copy>
        <xsl:apply-templates/>
    </xsl:copy>
</xsl:template>
<xsl:template match="@*">
    <xsl:attribute name="{local-name()}">
        <xsl:value-of select="current()"/>
    </xsl:attribute>
</xsl:template>
<xsl:template match="*">
    <xsl:element name="{local-name()}">
        <xsl:apply-templates select="@* | * | text()"/>
    </xsl:element>
</xsl:template>
<xsl:template match="text()">
    <xsl:copy>
        <xsl:value-of select="current()"/>
    </xsl:copy>
</xsl:template>
</xsl:stylesheet>