2017-02-09 4 views
0

xslt 1.0を使用して、入力xmlから下の出力を取得します。あなたはそれを行う方法を提案してもらえますか?xsltを使用してインクリメンタルノードの名前を固定ノード値に変更します。

Input xml: 
    <result> 
     <item0> 
     <Name>Customer1</Name> 
     <location>1360190</location> 
     </item0> 
    <item1> 
     <Name>Customer2</Name> 
     <location>1360190</location> 
    </item1> 
    </result> 

    Output xml: 
     <result> 
     <item> 
      <Name>Customer1</Name> 
      <location>1360190</location> 
     </item> 
     <item> 
      <Name>Customer2</Name> 
      <location>1360190</location> 
     </item> 
     </result> 
+0

は、[例](http://stackoverflow.com/、テンプレートを変換するIDを使用しますa/8679861/3244046)、すべてのノードをそのままコピーする。 '/ result/* 'にマッチする別のテンプレート、つまり' result'の子要素を追加し、必要に応じてそれらを変換します。 –

答えて

0

@LingamurthyCSは述べているように、アイデンティティは、以下に示すような各種のXPath 試合参照を持つ<item>ノードの名前を変更するトランスフォームとテンプレートを検討:

結果参照し、子供に適用-テンプレート付き要素:

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
<xsl:output version="1.0" encoding="UTF-8" indent="yes" method="xml"/> 
<xsl:strip-space elements="*"/> 

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

    <xsl:template match="result/*"> 
    <item> 
     <xsl:apply-templates select="*"/> 
    </item> 
    </xsl:template>    

</xsl:transform> 

項目参照して

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
<xsl:output version="1.0" encoding="UTF-8" indent="yes" method="xml"/> 
<xsl:strip-space elements="*"/> 

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

    <xsl:template match="/*/*"> 
    <item> 
     <xsl:copy-of select="*"/> 
    </item> 
    </xsl:template>    

</xsl:transform> 

参照とコピーの子要素のSULT

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
<xsl:output version="1.0" encoding="UTF-8" indent="yes" method="xml"/> 
<xsl:strip-space elements="*"/> 

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

    <xsl:template match="*[contains(name(), 'item')]"> 
    <item> 
     <xsl:apply-templates /> 
    </item> 
    </xsl:template>    

</xsl:transform> 
+0

@Parrfait:ありがとう – user7411874

+0

素晴らしい!そして、感謝の気持ちを示す素晴らしい方法は、横の目盛りをクリックして解決策を受け入れることであり、問​​題の解決を確認することです。 – Parfait

関連する問題