使用 XSL 向 XML 添加命名空间 + 前缀

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2803063/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 13:05:47  来源:igfitidea点击:

add namespace + prefix to XML using XSL

xmlxsltnamespaces

提问by Jan

I hope you can help... Let's assume I have following XML:

我希望你能帮上忙……假设我有以下 XML:

<data>
   <token>
      <sessionId>12345</sessionId>         
      <userId>john</userId>
      <moreInfo>
         <bla> .....
         </bla>
      </moreInfo>
   </token>
</data>

And I need this to become

我需要这个成为

<login:data xmlns:login="http://my.ns.uri">
       <login:token>
          <login:sessionId>12345</sessionId>         
          <login:userId>john</userId>
          <login:moreInfo>
             <login:bla> .....
             </login:bla>
          </login:moreInfo>
       </login:token>
    </login:data>

Can I do this with XSL? I did try but failed miserably ... Any help would be greatly appreciated!

我可以用 XSL 做到这一点吗?我确实尝试过,但惨遭失败......任何帮助将不胜感激!

Thanks, Jan

谢谢,简

采纳答案by Dimitre Novatchev

Use:

使用

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:login="http://my.ns.uri">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

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

 <xsl:template match="*">
  <xsl:element name="login:{name()}" namespace="http://my.ns.uri">
    <xsl:copy-of select="namespace::*"/>
    <xsl:apply-templates select="node()|@*"/>
  </xsl:element>
 </xsl:template>
</xsl:stylesheet>

When this transformation is applied on the provided XML document, the wanted, correct result is produced:

当此转换应用于提供的 XML 文档时,会产生所需的正确结果

<login:data xmlns:login="http://my.ns.uri">
   <login:token>
      <login:sessionId>12345</login:sessionId>
      <login:userId>john</login:userId>
      <login:moreInfo>
         <login:bla> .....
         </login:bla>
      </login:moreInfo>
   </login:token>
</login:data>

回答by Mandy

XSLT 2.0 is more efficient and compact. It supports to add namespaces to node directly. We don't need to define anything in the starting of stylesheet as well.

XSLT 2.0 更加高效和紧凑。它支持直接向节点添加命名空间。我们也不需要在样式表的开头定义任何内容。

Here is the spec : creating namespace prefix

这是规范:创建命名空间前缀

Use :

用 :

<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:template match="*">
  <xsl:element name="login:{name()}" xmlns:login="http://my.ns.uri">
   <xsl:namespace name="login">http://my.ns.uri</xsl:namespace>
   <xsl:value-of select="node()"/>
   <xsl:apply-templates select="*"/>
  </xsl:element>
 </xsl:template>
</xsl:stylesheet>

It will give the output :

它将给出输出:

<login:data xmlns:login="http://my.ns.uri">
   <login:token>
      <login:sessionId>12345</login:sessionId>
      <login:userId>john</login:userId>
      <login:moreInfo>
         <login:bla> .....
         </login:bla>
      </login:moreInfo>
   </login:token>
</login:data>

回答by Boldewyn

<xsl:template match="*">
  <xsl:element name="{local-name()}" namespace="http://my.ns.uri">
    <xsl:apply-templates />
  </xsl:element>
</xsl:template>