xml - 从元素中删除 1 个命名空间

标签 xml xslt xslt-1.0 xslt-2.0

目前我有一个 XML 文档,其中根节点有 2 个命名空间,如下所示

<?xml version="1.0" encoding="UTF-8"?>
<Document xmlns = "www.ns1.com"
    xmlns:ns2 = "www.ns2.com">
    <node1>
            <node2 addy1="something">value</node2>
    </node1>
</Document>

我当前的转换如下所示

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="node()">
        <xsl:copy>
            <xsl:copy-of select="@addy1"/>
            <xsl:apply-templates />
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

生成的 xml 几乎是完美的,除了我想从 Document 元素中排除 xmlns:ns2 (xmlns 很好)我需要添加什么才能仅复制 Document 的默认命名空间,而不是 xmlns2

最佳答案

使用

<xsl:template match="node()">
    <xsl:copy copy-namespaces="no">
        <xsl:copy-of select="@addy1"/>
        <xsl:apply-templates />
    </xsl:copy>
</xsl:template>

https://xsltfiddle.liberty-development.net/3NzcBsN

关于xml - 从元素中删除 1 个命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49906068/

相关文章:

xml - 如何使用 xslt 中的 xml 数据源将 unicode 转换为 iso?

java - 无法生成签名的APK?

python - 解析 xml 文件并将其存储到数据库中

java - ImageView 不随折叠工具栏折叠

html - 页面中的div排列

xml - XSLT 1.0 在同一级别上使用不同的值对多个相同节点进行分组

xml - Scala:jaxb 或类似的?

XSLT 从 DTD 获取属性类型

html - xslt: css 在 html 页面中不工作

xslt - XPath 谓词 "is parent the current node?"