xml - 移除 xml 的命名空间
问题描述
我正在低于 XML 作为源,必须通过添加段进行转换。但也必须删除根节点中添加的命名空间。但无法删除命名空间。
有人可以分享我在哪里添加到 XSLT。
源 XML:
<?xml version="1.0" encoding="UTF-8"?>
<Header
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<Main>
<Parent2>
<status>12</status>
<statusmsg>Helo</statusmsg>
</Parent2>
<Parent3>
<Child1>112</Child1>
<Child2>Hai</Child2>
</Parent3>
<Parent4>
<Child3>Valley</Child3>
<Parent5>
<Child7>Kind</Child7>
<Child8>Pls</Child8>
</Parent5>
</Parent4>
</Main>
</Header>
目标 XML:
<Header>
<Main Mainattribute="1">
<Parent2 childattribute="1">
<status>12</status>
<statusmsg>Helo</statusmsg>
</Parent2>
<Parent3 childattribute="1">
<Child1>112</Child1>
<Child2>Hai</Child2>
</Parent3>
<Parent4 childattribute="1">
<Child3>Valley</Child3>
<Parent5>
<Child7>Kind</Child7>
<Child8>Pls</Child8>
</Parent5>
</Parent4>
</Main>
</Header>
XSLT 从以下链接尝试: 从第 4 个父节点向 XML 文件的所有父节点填充属性和值
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="Main">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:attribute name="Mainattribute"><xsl:value-of select="1"/></xsl:attribute>
<xsl:apply-templates mode="parent_mode"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*" mode="parent_mode">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:attribute name="childattribute"><xsl:value-of select="1"/></xsl:attribute>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="*">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
解决方案
XSLT 2.0 有一种更简单的方法,即使用
<xsl:copy-of select="/" copy-namespaces="no"/>
它执行深层复制删除所有未使用的命名空间。
推荐阅读
- amazon-web-services - AWS Redshift 清除策略自动化
- c# - 使用 X509Certificate2 密钥的 RSA 加密
- php - pregmatch 在href中获取文本
- sql-server - SQL Server 数据更改跟踪并显示给定时间内的数据状态
- angular - Angular routerLink 相对导航没有矩阵参数
- forms - 如何在 Symfony 4 中将表单的错误转换为字符串?
- xaml - XamarinForms 中的 UWP 流畅设计
- javascript - 音频包含在另一个 PHP 文件中时不起作用
- python-2.7 - 将训练集和测试集的数据除以训练集中的所有主题
- apache-kafka - 每当消息到达主题而不是在特定时间间隔轮询时,Kafka 是否可以在消费者端接收消息