如何使用 xslt 从源 xml 中删除命名空间和前缀属性? [英] how to remove namespace and prefix attibutes from a source xml using xslt?

查看:50
本文介绍了如何使用 xslt 从源 xml 中删除命名空间和前缀属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个xml如下

<Publication xmlns:n1="http://www.w3.org/2001/XMLSchema-instance" n1:noNamespaceSchemaLocation="InformaKMSStructure.xsd"

我想复制所有 XML 但不是 xmlns:n1="http://www.w3.org/2001/XMLSchema-instance" 和 n1:noNamespaceSchemaLocation="InformaKMSStructure.xsd"

i want copy all XML but not xmlns:n1="http://www.w3.org/2001/XMLSchema-instance" and n1:noNamespaceSchemaLocation="InformaKMSStructure.xsd"

推荐答案

这种转变:

<xsl:stylesheet version="1.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="*">
     <xsl:element name="{name()}" namespace="{namespace-uri()}">
       <xsl:apply-templates select="node()|@*"/>
     </xsl:element>
 </xsl:template>

 <xsl:template match="node()[not(self::*)]|@*">
  <xsl:copy/>
 </xsl:template>

 <xsl:template match=
 "@*[namespace-uri() = 'http://www.w3.org/2001/XMLSchema-instance']"/>
</xsl:stylesheet>

应用于以下 XML 文档时(未提供!):

<Publication
 xmlns:n1="http://www.w3.org/2001/XMLSchema-instance"
 n1:noNamespaceSchemaLocation="InformaKMSStructure.xsd">
  <a x="y">
   <b/>
  </a>

  <c/>
</Publication>

产生想要的、正确的结果:

<Publication>
   <a x="y">
      <b/>
   </a>
   <c/>
</Publication>

说明:

这遵循覆盖身份规则的通常模式,但是身份规则被两个模板替换——一个匹配任何元素,第二个匹配任何其他节点或属性.

This follows the usual pattern of overriding the identity rule, however the identity rule is replaced by two templates -- one matching any element anf the second matching any other node or attribute.

这篇关于如何使用 xslt 从源 xml 中删除命名空间和前缀属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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