【问题标题】:Xslt change node and add namespaceXslt 更改节点并添加命名空间
【发布时间】:2012-06-12 20:24:09
【问题描述】:

我有以下 XML

<?xml version="1.0"?>
<location>
<Destination>Des01</Destination>
<DesCode>ACD8701</DesCode>
<UniqueId>023154</UniqueId>
<Amount>26</Amount>
</location>

我想将&lt;location&gt; 更改为&lt;abc_ItemUpdate&gt; 并添加命名空间 因此使用 XSLT 后输出应如下所示

<ns0:abc_ItemUpdate xmlns:ns0="http://schemas.microsoft.com/Sql/2008/05/TypedProcedures/dbo">
    <ns0:Destination>Des01</ns0:LegalEntity>
    <ns0:DesCode>ACD8701</ns0:DesCode>
    <ns0:UniqueId>023154</ns0:UniqueId>
    <ns0:Amount>26</ns0:Amount>
</ns0:abc_ItemUpdate>

提前致谢

【问题讨论】:

    标签: xml xslt replace namespaces


    【解决方案1】:
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
        <xsl:output indent="yes"/>
    
     <xsl:template match="/location">
    
         <xsl:element name="ns0:abc_ItemUpdate" namespace="http://yournms">
             <!-- copy attributes if any -->
             <xsl:copy-of select="@*"/>
             <xsl:apply-templates/>
         </xsl:element>
    
     </xsl:template>
    
     <xsl:template match="*">
         <xsl:element name="ns0:{name()}" namespace="http://yournms">
             <!-- copy attributes if any -->
             <xsl:copy-of select="@*"/>
             <xsl:apply-templates/>
         </xsl:element>
     </xsl:template>
    
    </xsl:stylesheet>
    

    【讨论】:

    • 我会做一个小改动:使用name="ns0:{local-name()}",所以如果应用于已经使用命名空间前缀的输入,它仍然有效。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-23
    • 2017-06-13
    • 1970-01-01
    • 1970-01-01
    • 2017-07-04
    相关资源
    最近更新 更多