2016-07-22 3 views
0

私がここで達成しようとしているのは、要素(子ノードを含む)を移動し、その要素内に子ノードを追加することです。私は一度に一つしかできないようです。同時に両方を行うことは可能ですか?あなたは要素のCD3を移動し、同様に追加されるの子ノード見ることができるようにXSLTを使用してXML要素を移動し、子要素を同時に追加する方法はありますか?

は、ここでは、この

<catalog> 
<box1> 
    <cd1> 
     <title>Title 1</title> 
     <artist>Bob Dylan</artist> 
     <year>1985</year> 
    </cd1> 
    <cd2> 
     <title>Title 2</title> 
     <artist>Bonnie Tyler</artist> 
     <year>1988</year> 
    </cd2> 
    <cd3> 
     <title>Title 3</title> 
     <artist>Metallica</artist> 
     <year>1990</year> 
    </cd3> 
</box1> 

のような出力を持っているか、私の入力XML

<?xml version="1.0" encoding="UTF-8"?> 
<catalog> 
    <box1> 
     <cd1> 
      <title>Title 1</title> 
      <artist>Bob Dylan</artist> 
      <year>1985</year> 
     </cd1> 
     <cd2> 
      <title>Title 2</title> 
      <artist>Bonnie Tyler</artist> 
      <year>1988</year> 
     </cd2> 
    </box1> 
    <box2> 
     <cd3> 
      <title>Title 3</title> 
      <artist>Metallica</artist> 
     </cd3> 
    </box2> 
</catalog> 

です。

これは私がやったことであり、コードをどのような順序で配置しても要素を動かすだけです。

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="3.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 

    <xsl:strip-space elements="*"/> 

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

    <!-- add a child element --> 
    <xsl:template match="cd3"> 
     <xsl:copy> 
      <xsl:apply-templates/> 
      <year>1990</year> 
     </xsl:copy> 
    </xsl:template> 

    <!-- move node --> 
    <xsl:template match="/catalog"> 
     <xsl:copy> 
       <xsl:apply-templates /> 
       <xsl:copy-of select="box2/cd3"/> 
     </xsl:copy> 
    </xsl:template> 
    <xsl:template match="box2"/> 

</xsl:stylesheet> 

答えて

0

変更<xsl:apply-templates select="box2/cd3"/><xsl:copy-of select="box2/cd3"/><xsl:template match="/catalog/box1">から<xsl:template match="/catalog">を変更します。

+0

マーティン、あなたが示唆したように、要素は表示されません。 – user1998820

+0

@ user1998820、はい、あなたのオリジナルのスタイルシートでは、いくつかの編集を示唆するのは少し難しかったです。 'apply-templates'を使う提案は正しいです。さらに' 'もアプローチを単純化しますが、私は、apply-templatesのパスを ''に適合させる必要があることを見落としました。 –

0

以下のコードで解決しました。助けてくれてありがとう、私はさらなる調査を始めました。

​​
関連する問題