2016-08-17 7 views
0

XSLTを使用してXML要素を追加しようとしています。要素を追加した後、私はいくつかの要素を新しく追加された要素の中に移動したい。既存のXMLにXML要素を追加し、その中の要素を移動する方法

これは私が

このため
 <Collection> 
<ONE/> 
<TWO/> 
<THREE/> 

     <PARTCollection> 
     <Allparts> 

      <part> 
       <number>001</number> 
       <material>Platinum</material> 
       <price>High</price> 
      </part> 

      <part> 
       <number>002</number> 
       <material>Gold</material> 
       <price>Medium</price> 
      </part> 

      <part> 
       <number>003</number> 
       <material>Silver</material> 
       <price>Low</price> 
      </part> 


     </Allparts> 
    </PARTCollection> 
     <BOMCollection> 
     <Allboms> 

      <bom> 
       <Part-number>001</Part-number> 
      </bom> 

      <bom> 
       <Part-number>002</Part-number> 
      </bom> 

      <bom> 
       <Part-number>003</Part-number> 
      </bom> 
     </Allboms> 
    <BOMCollection> 

     </Collection> 

のような出力が、私は新しい要素が適切に作成されているが、データがない

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

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

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

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



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

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

を書かれているしたい私のXML

<Collection> 
<ONE/> 
<TWO/> 
<THREE/> 

    <Allparts> 

     <part> 
      <number>001</number> 
      <material>Platinum</material> 
      <price>High</price> 
     </part> 

     <part> 
      <number>002</number> 
      <material>Gold</material> 
      <price>Medium</price> 
     </part> 

     <part> 
      <number>003</number> 
      <material>Silver</material> 
      <price>Low</price> 
     </part> 


    </Allparts> 

    <Allboms> 

     <bom> 
      <Part-number>001</Part-number> 
     </bom> 

     <bom> 
      <Part-number>002</Part-number> 
     </bom> 

     <bom> 
      <Part-number>003</Part-number> 
     </bom> 
    </Allboms> 

    </Collection> 

ですあなたがこの仕事をどうやってできるか教えてください。

答えて

0

それは

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 

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

    <xsl:template match="Collection"> 
     <xsl:copy> 
      <PARTCollection> 
       <xsl:apply-templates select="Allparts"/> 
      </PARTCollection> 
      <BOMCollection> 
       <xsl:apply-templates select="Allboms"/> 
      </BOMCollection> 
     </xsl:copy> 
    </xsl:template> 

</xsl:transform> 
+0

マーティンを使用することで十分です。別の質問のためにあなたの助けが必要です。お手伝いできますありがとう http://stackoverflow.com/questions/38993864/match-and-merge-in-xslt-with-optional-nodes – Victor

関連する問題