XSLT 更改命名空间前缀 [英] XSLT to change namespace prefix

查看:34
本文介绍了XSLT 更改命名空间前缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将属性转换为元素,与此同时,我想更改 XML 代码的命名空间前缀.XML 代码:

I am trying to convert attributes into elements, along with this, I want to change namespace prefix of my XML code. XML code:

    <lm:GetInvoiceList xmlns:lm="http://www.w3.org">
    <lm:Response>
    <lm:Bill>
    <lm:BillStatusCode typecode="1">type description</lm:BillStatusCode>
    <lm:EBillProcessStatusCode typecode="2">type description</lm:EBillProcessStatusCode>
    <lm:BillCycleCode typecode="1">type description</lm:BillCycleCode>
    <lm:BillActivityCode typecode="3">type description</lm:BillActivityCode>
    <lm:ToDate>...</lm:ToDate>
    </lm:Bill>
    </lm:Response>
    </lm:GetInvoiceList>

我有这个 XSLT 代码:

I have this XSLT code:

    <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="node()">
    <xsl:copy>
    <xsl:apply-templates/>
    </xsl:copy>
    </xsl:template>

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

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

但我没有得到想要的输出.

But I am not getting the desired output.

预期输出:

    <ns:GetInvoiceList xmlns:ns="http://my.ns.uri">
    <ns:Response>
    <ns:Bill>
    <ns:BillStatusCode>
    <ns:BillStatusCode>type description</ns:BillStatusCode>
    <ns:typecode>1</ns:typecode>
    </ns:BillStatusCode>
    <ns:EBillProcessStatusCode>
    <ns:EBillProcessStatusCode>type description</ns:EBillProcessStatusCode>
    <ns:typecode>2</ns:typecode>
    </ns:EBillProcessStatusCode>
    <ns:BillCycleCode>
    <ns:BillCycleCode>type description</ns:BillCycleCode>
    <ns:typecode>1</ns:typecode>
    </ns:BillCycleCode>
    <ns:BillActivityCode>
    <ns:BillActivityCode>type description</ns:BillActivityCode>
    <ns:typecode>3</ns:typecode>
    </ns:BillActivityCode>
    <ns:ToDate>...</ns:ToDate>
    </ns:Bill>
    </ns:Response>
    </ns:GetInvoiceList>

实际输出:

   <ns:GetInvoiceList xmlns:ns="http://my.ns.uri">
   <ns:Response>
   <ns:Bill>
   <ns:BillStatusCode>
   <ns:typecode>1</ns:typecode>type description</ns:BillStatusCode>
   <ns:EBillProcessStatusCode>
   <ns:typecode>2</ns:typecode>type description</ns:EBillProcessStatusCode>
   <ns:BillCycleCode>
   <ns:typecode>1</ns:typecode>type description</ns:BillCycleCode>
   <ns:BillActivityCode>
   <ns:typecode>3</ns:typecode>type description</ns:BillActivityCode>
   <ns:ToDate>...</ns:ToDate>
   </ns:Bill>
   </ns:Response>
   </ns:GetInvoiceList>

感谢您对此的任何帮助!

Would Appreciate any help on this!

推荐答案

鉴于您在评论中的澄清,我建议:

In view of your clarifications in the comments, I would suggest:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns="http://my.ns.uri">
<xsl:output method="xml" version="1.0" omit-xml-declaration="yes" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

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

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

<xsl:template match="@*">
    <xsl:element name="ns:{local-name()}">
        <xsl:value-of select="."/>
    </xsl:element>
</xsl:template>

</xsl:stylesheet>

有什么区别?

应用于以下示例输入时:

When applied to the following example input:

XML

<lm:GetInvoiceList xmlns:lm="http://www.w3.org">
   <lm:Response>
      <lm:Bill>
         <lm:BillPropertyA subPropertyA="first subproperty">first property</lm:BillPropertyA>
         <lm:BillPropertyB subPropertyB="second subproperty"/>
         <lm:BillPropertyC>second property</lm:BillPropertyC>
      </lm:Bill>
   </lm:Response>
</lm:GetInvoiceList>

结果:

<ns:GetInvoiceList xmlns:ns="http://my.ns.uri">
   <ns:Response>
      <ns:Bill>
         <ns:BillPropertyA>
            <ns:BillPropertyA>first property</ns:BillPropertyA>
            <ns:subPropertyA>first subproperty</ns:subPropertyA>
         </ns:BillPropertyA>
         <ns:BillPropertyB>
            <ns:BillPropertyB/>
            <ns:subPropertyB>second subproperty</ns:subPropertyB>
         </ns:BillPropertyB>
         <ns:BillPropertyC>second property</ns:BillPropertyC>
      </ns:Bill>
   </ns:Response>
</ns:GetInvoiceList>

相反:

<ns:GetInvoiceList xmlns:ns="http://my.ns.uri">
   <ns:Response>
      <ns:Bill>
         <ns:BillPropertyA>
            <ns:subPropertyA>first subproperty</ns:subPropertyA>
            <ns:BillPropertyA>first property</ns:BillPropertyA>
         </ns:BillPropertyA>
         <ns:BillPropertyB>
            <ns:subPropertyB>second subproperty</ns:subPropertyB>
         </ns:BillPropertyB>
         <ns:BillPropertyC>
            <ns:BillPropertyA>second property</ns:BillPropertyA>
         </ns:BillPropertyC>
      </ns:Bill>
   </ns:Response>
</ns:GetInvoiceList>

将第二个属性"文本节点错误地放置在 ns:BillPropertyA 的另一个实例下,并使其成为 ns:BillPropertyC 的子节点.

which places the "second property" text node incorrectly under another instance of ns:BillPropertyA and makes that a child of ns:BillPropertyC.

注意事项:

  1. 如果要更改节点的命名空间,则不能使用xsl:copy,因为这也会复制现有的命名空间;

  1. If you want to change the namespace of a node, you cannot use xsl:copy, since that would also copy the existing namespace;

您可以(并且应该)声明命名空间一次,然后使用必要时已经存在的绑定.

You can (and should) declare the namespace once, then use the already existing binding where necessary.

这篇关于XSLT 更改命名空间前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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