2016-05-28 4 views
0

のコピーに繰り返しエレメントを移動すると、私は<animal>(対応<animal><xxx>を繰り返す重複カウント=カウント)を複製し、別のコピーに繰り返し<xxx>を移動する必要があります。

私のxml <xxx><animal>の最初のインスタンスに対して2回繰り返されているので、出力には2つの<animals>が必要です。最初<animal><xxx>の最初のインスタンスが含まれている必要があり、第二<animal><xxx>

入力XML

<?xml version="1.0" encoding="UTF-8"?> 
<header> 
    <animal> 
     <element1>element1</element1> 
     <element2>element2</element2> 
     <element3 lang="en">element3</element3> 
     <xxx> 
      <code>YY</code> 
      <description>code yy</description> 
     </xxx> 
     <xxx> 
      <code>ZZ</code> 
      <description>code zz</description> 
     </xxx> 
    </animal> 
    <animal> 
     <xxx> 
      <code>AA</code> 
      <description>code aa</description> 
     </xxx> 
    </animal> 
</header> 

必要な必要な変換

<?xml version="1.0" encoding="UTF-8"?> 
<header> 
    <animal> 
     <element1>element1</element1> 
     <element2>element2</element2> 
     <element3 lang="en">element3</element3> 
     <xxx> 
      <code>YY</code> 
      <description>code yy</description> 
     </xxx>   
    </animal> 
    <animal> 
     <element1>element1</element1> 
     <element2>element2</element2> 
     <element3 lang="en">element3</element3>   
     <xxx> 
      <code>ZZ</code> 
      <description>code zz</description> 
     </xxx> 
    </animal> 
    <animal> 
     <xxx> 
      <code>AA</code> 
      <description>code aa</description> 
     </xxx> 
    </animal> 
</header> 

の2番目のインスタンスを含まなければならないすべてのヘルプは非常ににappriciatedされます。事前のおかげで

+0

? –

+0

"* が2以上の場合 *"あなたは1以上を意味すると思いますか? –

+0

@ michael.hor257kあなたはxsltを学ぶのに使ったリソースを指摘できますか? – Sowmiya

答えて

2

私のソリューションは、一般的に最もエレガントではありませんが、これは所望の出力を生成します - まさにあなたはこれで立ち往生している顔をしている...

<?xml version="1.0" encoding="utf-8" standalone="yes"?> 

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


<xsl:output method="xml" indent="yes"/> 
<xsl:strip-space elements="*"/> 

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

<xsl:template match="animal"> 
    <xsl:param name="i" select="xxx[1]"/> 
    <xsl:variable name="thisanimal" select="."/> 
    <xsl:choose> 
     <xsl:when test="count(xxx) = 1"> 
      <!-- only one here --> 
      <xsl:copy-of select="."/> 
     </xsl:when> 
     <xsl:when test="$i = xxx[1]"> 
      <!-- more than one here, use the first --> 
      <xsl:copy> 
       <xsl:apply-templates select="*[name() != 'xxx']"/> 
       <xsl:apply-templates select="$i"/> 
      </xsl:copy> 
      <xsl:for-each select="xxx[position() &gt; 1]"> 
       <xsl:apply-templates select="$thisanimal"> 
        <xsl:with-param name="i" select="."/> 
       </xsl:apply-templates> 
      </xsl:for-each> 
     </xsl:when> 
     <xsl:otherwise> 
      <!-- more than one here --> 
      <xsl:copy> 
       <xsl:apply-templates select="*[name() != 'xxx']"/> 
       <xsl:apply-templates select="$i"/> 
      </xsl:copy> 
     </xsl:otherwise> 
    </xsl:choose> 
</xsl:template> 

</xsl:transform> 
+0

ありがとう@Stefan Hegny – Sowmiya

+0

喜んで助けてください... –