如何使用XSLT为所有元素和属性添加名称空间和前缀?

如何使用XSLT为所有元素和属性添加名称空间和前缀?,xslt,namespaces,Xslt,Namespaces,我的问题是如何使用XSLT为所有元素和属性添加名称空间和前缀? 我的输入xml原样 <ProcessCreditMemo xmlns='CreditMemo' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:soa

我的问题是如何使用XSLT为所有元素和属性添加名称空间和前缀? 我的输入xml原样

<ProcessCreditMemo xmlns='CreditMemo' 
                   xmlns:xsd='http://www.w3.org/2001/XMLSchema' 
                   xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
                   xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>
<ORDER_HEADERDetails>
    <ORDER_HEADER>
    <NAME>0010185214</NAME>

0010185214

<ns0:ProcessCreditMemo xmlns='CreditMemo' 
                       xmlns:xsd='http://www.w3.org/2001/XMLSchema' 
                       xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
                       xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/' 
                       xmlns:ns0="http://tempuri.org/">
<ns0:ORDER_HEADERDetails>
    <ns0:ORDER_HEADER>
   <ns0:NAME>0010185214</NAME>

0010185214
我需要为所有元素和属性添加前缀“ns0:”,并添加名称空间“xmlns:ns0=”http://tempuri.org/在标题“ProcessCreditMemo”中

我正在尝试构建一个XSLT来完成它

<xsl:stylesheet version="1.0" 
          xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:template match="node()|text()|@*">
    <xsl:copy>
        <xsl:if test="local-name()='ProcessCreditMemo'">
            <xsl:attribute name="xmlns" namespace="http://tempuri.org/" />
        </xsl:if>

但是结果XML使用空值复制前缀

<ProcessCreditMemo xmlns="CreditMemo" 
                   xmlns:ns0="http://tempuri.org/" 
                   xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
                   xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                   ns0:xmlns="">

此转换:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:ns0="http://tempuri.org/">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

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

 <xsl:template match="*">
  <xsl:element name="ns0:{name()}" namespace="http://tempuri.org/">
   <xsl:copy-of select="namespace::*"/>
       <xsl:apply-templates select="node()|@*"/>
  </xsl:element>
 </xsl:template>
</xsl:stylesheet>
<ProcessCreditMemo xmlns='CreditMemo'
  xmlns:xsd='http://www.w3.org/2001/XMLSchema'
  xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
  xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>
  <ORDER_HEADERDetails>
    <ORDER_HEADER>
      <NAME>0010185214</NAME>
    </ORDER_HEADER>
  </ORDER_HEADERDetails>
</ProcessCreditMemo>

应用于(已更正)提供的输入时(格式严重错误、不完整的XML)

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:ns0="http://tempuri.org/">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

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

 <xsl:template match="*">
  <xsl:element name="ns0:{name()}" namespace="http://tempuri.org/">
   <xsl:copy-of select="namespace::*"/>
       <xsl:apply-templates select="node()|@*"/>
  </xsl:element>
 </xsl:template>
</xsl:stylesheet>
<ProcessCreditMemo xmlns='CreditMemo'
  xmlns:xsd='http://www.w3.org/2001/XMLSchema'
  xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
  xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>
  <ORDER_HEADERDetails>
    <ORDER_HEADER>
      <NAME>0010185214</NAME>
    </ORDER_HEADER>
  </ORDER_HEADERDetails>
</ProcessCreditMemo>

0010185214
生成所需的正确结果(而不是提供的格式严重错误/不完整的所需结果):

<ns0:ProcessCreditMemo xmlns:ns0="http://tempuri.org/" xmlns="CreditMemo" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <ns0:ORDER_HEADERDetails>
      <ns0:ORDER_HEADER>
         <ns0:NAME>0010185214</ns0:NAME>
      </ns0:ORDER_HEADER>
   </ns0:ORDER_HEADERDetails>
</ns0:ProcessCreditMemo>

0010185214

我在xslt中使用了一个类似的代码片段,但我的eclipse一直将错误消息显示为无效的xpath……然而,xml转换工作得非常好。