从 xml 的根元素中删除特定的 xmlns [英] Remove a specific xmlns from root element of xml

查看:52
本文介绍了从 xml 的根元素中删除特定的 xmlns的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对 XML 文档进行转换,但我找不到解决方案,因为我不知道 XSLT.我有 XML 文档:

I'm trying to make a transformation for an XML document but i cannot find a solution since i do not know XSLT. I have the XML document:

<?xml version="1.0" encoding="UTF-8"?>
<addresses xmlns="http://www.test.org/xml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:noNamespaceSchemaLocation='http://whatever/test.xsd'>

  <address>
    <name>Joe Tester</name>
    <street>Baker street 5</street>
  </address>

</addresses>

我想制作:

<?xml version="1.0" encoding="UTF-8"?>
<addresses xmlns="http://www.test.org/xml">

  <address>
    <name>Joe Tester</name>
    <street>Baker street 5</street>
  </address>

</addresses>

(考虑到 xsi:noNamespaceSchemaLocation=..." 已经排除在此之前使用另一个 XSLT).

(Consider that xsi:noNamespaceSchemaLocation="..." has already excluded using another XSLT before this one).

有人可以帮我找到解决方案吗?

Can someone help me find a solution?

用于消除 xsi:noNamespaceSchemaLocation 的 XSLT 是:

The XSLT used to eliminate the xsi:noNamespaceSchemaLocation is:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>

<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="@* | node()">
 <xsl:copy>
  <xsl:apply-templates select="@* | node()" />
 </xsl:copy>
</xsl:template>

<xsl:template match="@xsi:noNamespaceSchemaLocation"/>

</xsl:stylesheet>

推荐答案

请试一试:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" exclude-result-prefixes="xsi"
>

  <xsl:output omit-xml-declaration="yes" indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()" />
    </xsl:copy>
  </xsl:template>

  <xsl:template match="*">
    <xsl:element name="{name()}" namespace="{namespace-uri()}">
      <xsl:copy-of select="namespace::*[not(. = 'http://www.w3.org/2001/XMLSchema-instance')]" />
      <xsl:apply-templates select="@*|node()"/>
    </xsl:element>
  </xsl:template>

  <xsl:template match="@xsi:noNamespaceSchemaLocation"/>

</xsl:stylesheet>

这篇关于从 xml 的根元素中删除特定的 xmlns的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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