如何使用 XSLT 从 XML 中删除名称空间 [英] How to remove namespaces from XML using XSLT

查看:25
本文介绍了如何使用 XSLT 从 XML 中删除名称空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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:element but not attribute. How can I include the attributes there?

<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>

推荐答案

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

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).

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

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

  1. 仅复制属性不足以删除所有命名空间.即使包含元素不属于命名空间,属性也可以属于命名空间.因此,还需要创建属性,如元素.使用 元素创建属性.
  2. 有效的 XML 文档不能包含具有两个或多个扩展名称相同的属性的元素,但元素可以包含多个具有相同本地名称的属性如果属性具有不同的命名空间.这意味着删除如果元素具有至少两个具有相同本地名称的属性,则属性名称的命名空间前缀将导致数据丢失.这些属性之一将被删除(或覆盖).
  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).

...和代码:

<?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>

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

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

这篇关于如何使用 XSLT 从 XML 中删除名称空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆