8

私の問題は、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>

することが...

<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>

すべての要素と属性にプレフィックス「ns0:」を追加し、ヘッダー「ProcessCreditMemo」に名前空間「xmlns:ns0="http://tempuri.org/」を追加する必要があります。

私はそれを行うために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="">
4

1 に答える 1

13

この変換

<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>

(修正された)提供された入力(ひどく奇形で不完全な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>
    </ORDER_HEADER>
  </ORDER_HEADERDetails>
</ProcessCreditMemo>

必要な正しい結果を生成します(ひどく奇形/不完全な提供された必要な結果ではありません):

<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>
于 2012-10-20T20:33:18.523 に答える