2016-10-05 5 views
3

何らかの理由で、既存のxmlファイルを変更する必要があり、xsltを使用してそのようにしたいと考えています。名前空間宣言をルートノードから子ノードに移動する必要があります。 XSLT - 一部の子供に名前空間宣言を追加する

は基本的に私は、ファイルはこのように見ていると始めています:

<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:ns1="http://ns1" xmlns:ns2="http://ns2" xmlns:ns3="http://ns3"> 
<env:Header> 
    <ns1:parent11> 

     <ns1:child11>value</ns1:child11> 
     <ns1:child11>value</ns1:child11> 
     <ns1:child11>value</ns1:child11> 

     <ns1:child12> 
      <ns2:child21>value</ns2:child21> 
      <ns2:child22>value</ns2:child22> 
     </ns1:child12> 

    </ns1:parent11> 
</env:Header> 


<env:Body> 
    <ns3:parent31> 
     <ns3:child31>value</ns3:child31> 
     <ns3:child32>value</ns3:child32> 

     <ns3:child33> 
      <ns2:parent2> 
       <ns2:child23>value</ns2:child23> 
       <ns2:child23>value</ns2:child23> 
       <ns2:child23>value</ns2:child23> 
      </ns2:parent2> 
     </ns3:child33> 

    </ns3:parent31> 
</env:Body> 

そして、私はこのような何かを終了する必要があります。

<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:ns1="http://ns1" xmlns:ns2="http://ns2" xmlns:ns3="http://ns3"> 
<env:Header> 
    <parent11 xmlns="http://ns1"> 

     <child11>value</child11> 
     <child11>value</child11> 
     <child11>value</child11> 

     <child12> 
      <ns2:child21>value</ns2:child21> 
      <ns2:child22>value</ns2:child22> 
     </child12> 

    </parent11> 
</env:Header> 


<env:Body> 

    <parent31 xmlns="http://ns3"> 
     <child31>value</child31> 
     <child32>value</child32> 

     <child33> 
      <parent2 xmlns="http://ns2"> 
       <child23>value</child23> 
       <child23>value</child23> 
       <child23>value</child23> 
      </parent2> 
     </child33> 

    </parent31> 
</env:Body> 

私はxsltの完全な初心者(私はこれが正しい方法だと思うのでそれを選ぶ)だから私はスチュです非常に初めにはckと私は開始する方法を知らない。

+0

この練習の目的は何ですか? AFAICTでは、入力と出力は意味的に同じです。 –

答えて

0

ソリューションXSLT 1.0/XSLT 2.0:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:ns1="http://ns1" 
    xmlns:ns2="http://ns2" 
    xmlns:ns3="http://ns3" 
    version="1.0"> 

    <xsl:template match="ns1:* | ns2:* | ns3:*"> 
     <xsl:element name="{local-name()}" namespace="{namespace-uri()}"> 
      <xsl:apply-templates/> 
     </xsl:element> 
    </xsl:template> 

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

</xsl:stylesheet> 

出力:

<?xml version="1.0" encoding="UTF-8"?> 
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://ns1" 
    xmlns:ns2="http://ns2" xmlns:ns3="http://ns3"> 
    <env:Header> 
     <parent11 xmlns="http://ns1"> 

      <child11>value</child11> 
      <child11>value</child11> 
      <child11>value</child11> 

      <child12> 
       <child21 xmlns="http://ns2">value</child21> 
       <child22 xmlns="http://ns2">value</child22> 
      </child12> 

     </parent11> 
    </env:Header> 
    <env:Body> 
     <parent31 xmlns="http://ns3"> 
      <child31>value</child31> 
      <child32>value</child32> 

      <child33> 
       <parent2 xmlns="http://ns2"> 
        <child23>value</child23> 
        <child23>value</child23> 
        <child23>value</child23> 
       </parent2> 
      </child33> 

     </parent31> 
    </env:Body> 
</env:Envelope> 

説明:

アッパーテンプレートは、名前空間接頭辞を削除します - 例えばns1 - 一致する要素の場合。

第2のテンプレートは、他のすべての要素、属性およびテキストについてのID-copy です。

XSLTプロセッサは、同じ名前空間内の子の名前空間を単独で削除します。

関連する問題