开发者

Transform the Input xml into Output xml using xslt

Input XML structure:

<Customer>
   <Order>
      <item>
        <name>ID</name>
        <value>11111</value>
      </item>
   </Order>
   <Order>
      <item>
        <name>ID</name>
        <value>11111</value>
      </item>
   </Order>
    <Order>
      <item>
        <name>ID</name>
        <value>22222</value>
      </item>
   </Order>
   <Order>
      <item>
        <name>ID</name>
        <value>33333</value>
      </item>
   </Order>
</Customer>

Output should be :

<Customer>
   <Order>
      <item>
        <name>ID</name>
        <value>11111</value>
      <item>
   </Order>
   <Order>
      <item>
        <name>ID</name>
        <value>11111</value>
      </item>
   </Order>
</Customer>
<Customer>
    <Order>
      <item>
        <name>ID</name>
 开发者_StackOverflow社区       <value>22222</value>
      </item>
   </Order>
</Customer>
<Customer>
    <Order>
      <item>
        <name>ID</name>
        <value>33333</value>
      </item>
   </Order>
</Customer>

Here the /Customer/<Order/item/value will come dynamically. Please anyone give a solution for this transformation.


This stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:key name="kOrderByValue" match="Order" use="item/value"/>
    <xsl:template match="Customer">
        <xsl:for-each select="Order[count(.|key('kOrderByValue',
                                                item/value
                                            )[1]
                                    ) = 1]">
            <Customer>
                <xsl:apply-templates select="key('kOrderByValue',
                                                 item/value
                                             )"/>
            </Customer>
        </xsl:for-each>
    </xsl:template>
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

Output:

<Customer>
    <Order>
        <item>
            <name>ID</name>
            <value>11111</value>
        </item>
    </Order>
    <Order>
        <item>
            <name>ID</name>
            <value>11111</value>
        </item>
    </Order>
</Customer>
<Customer>
    <Order>
        <item>
            <name>ID</name>
            <value>22222</value>
        </item>
    </Order>
</Customer>
<Customer>
    <Order>
        <item>
            <name>ID</name>
            <value>33333</value>
        </item>
    </Order>
</Customer>

Note: grouping Customer's Order children by value.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜