简体   繁体   English

使用xslt替换根节点的xmlns属性

[英]Replace xmlns attribute of root node using xslt

I have xml like below 我有像下面的xml

<rnp xmsns="v1">
  <ele1 line="1">
    <ele2></ele2>
  </ele1>
</rnp>

I want to change it to 我想把它改成

<rnp xmsns="v2">
  <ele1 line="1">
    <ele2></ele2>
  </ele1>
</rnp>

using xslt 1.0. 使用xslt 1.0。

I am using below xsl. 我在xsl以下使用。

<?xml version="1.0" encoding="iso-8859-1"?>

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns="v2">
    <xsl:template match="*">
        <xsl:element name="{local-name()}">
            <xsl:apply-templates select="@*|*|node()"/>
        </xsl:element>
    </xsl:template>

    <xsl:template match="rnp">
        <rnp>
            <xsl:apply-templates select="*"/>
        </rnp>
    </xsl:template> 
</xsl:stylesheet>

But this xsl does not copy the attributes so line attribute is not generated in output. 但是这个xsl不会复制属性,因此不会在输出中生成line属性。

sample output 样本输出

<?xml version="1.0" encoding="UTF-8"?><rnp xmlns="v2"><ele1>1
        <ele2/>
      </ele1></rnp>

How to change only the text of xmlns attrbiute using xslt? 如何使用xslt仅更改xmlns attrbiute的文本? Is there any other way to change xmlns using xslt? 有没有其他方法可以使用xslt更改xmlns? I have only option of xslt 1.0. 我只能选择xslt 1.0。

Thanks. 谢谢。

This transformation : 这种转变

<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:param name="pNS" select="'v2'"/>

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

 <xsl:template match="*[true()]">
  <xsl:element name="{local-name()}" namespace="{$pNS}">
       <xsl:apply-templates select="node()|@*"/>
  </xsl:element>
 </xsl:template>
</xsl:stylesheet>

when applied on the provided XML document (corrected to make it in the namespace "v1" : 当应用于提供的XML文档时 (更正为在命名空间"v1"创建它:

<rnp xmlns="v1">
  <ele1 line="1">
    <ele2></ele2>
  </ele1>
</rnp>

produces the wanted, correct result : 产生想要的,正确的结果

<rnp xmlns="v2">
   <ele1 line="1">
      <ele2/>
   </ele1>
</rnp>

Do note : 请注意

  1. The desired new default namespace is passed to the transformation as an external parameter -- thus the smae transformation without any modification can be used in every case when the default namespace must be modified. 所需的新默认命名空间作为外部参数传递给转换 - 因此,在必须修改默认命名空间的每种情况下,都可以使用没有任何修改的smae转换。

  2. This unusual looking template match: <xsl:template match="*[true()]"> makes it possible to avoid the XSLT processors "recoverable ambiguity error" messages if we had coded it just as <xsl:template match="*"> and is shorter and more elegant than specifying a priority. 这个看似不寻常的模板匹配: <xsl:template match="*[true()]">如果我们将其编码为<xsl:template match="*"> ,则可以避免XSLT处理器出现”可恢复的歧义错误“消息<xsl:template match="*">并且比指定优先级更短更优雅。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
                xmlns="v2">
    <xsl:output method="xml" indent="yes"/>

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

    <xsl:template match="*">
        <xsl:element name="{local-name()}">
            <xsl:apply-templates select="@* | node()"/>
        </xsl:element>
    </xsl:template>

</xsl:stylesheet>

Output: 输出:

<rnp xmlns="v2">
    <ele1 line="1">
        <ele2 />
    </ele1>
</rnp>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM