2016-11-08 88 views
-1

我需要映射以下,但其困难的,因为它有不同的名称:结合XML使用XSLT 1.0

<main> 
    <order> 
     <ID>123</ID> 
     <Name>ABC</Name> 
    </order> 
    <order> 
     <ID>4556</ID> 
     <Name>AAA</Name> 
     <ParentID>123</ParentID> 
    </order> 
</main> 

结果应该是:

<main> 
    <order> 
     <ID>123</ID> 
     <Name>ABC</Name> 
     <order> 
      <ID>4556</ID> 
      <Name>AAA</Name> 
      <ParentID>123</ParentID> 
     </order> 
    </order> 
</main> 
+2

我不明白这与你的其他问题有什么不同:http://stackoverflow.com/questions/40428756/combining-xml-xpath-or-xquery –

+0

在这种情况下,wee是比较订单与ID和父母的ID是不同的“使用”在钥匙@ michael.hor257k –

+0

我相信这是一回事。 –

回答

0

像@ michael.hor257k说的,概念上这个问题和你以前的问题是一样的。我想你只是不理解这个概念。希望我的例子中的评论会有所帮助。

如果我正确地理解了这个问题,您想根据ParentIDorder元素嵌套在它们的“父”order元素中。由于我们是基于此的PARENTID,这就是我们将使用我们的关键...

XML输入

<main> 
    <order> 
     <ID>123</ID> 
     <Name>ABC</Name> 
    </order> 

    <order> 
     <ID>4556</ID> 
     <Name>AAA</Name> 
     <ParentID>123</ParentID> 
    </order> 
</main> 

XSLT 1.0

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output indent="yes"/> 
    <xsl:strip-space elements="*"/> 

    <!--Create a key containing order elements that contain a non-empty ParentID.--> 
    <xsl:key name="orderByParentId" match="order[string(ParentID)]" use="ParentID"/> 

    <!--Identity transform (https://www.w3.org/TR/xslt#copying)--> 
    <xsl:template match="@*|node()"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()"/> 
    </xsl:copy> 
    </xsl:template> 

    <xsl:template match="/main"> 
    <xsl:copy> 
     <!--To get the high level order elements, only apply-templates when order 
     does not contain a non-empty ParentID child.--> 
     <xsl:apply-templates 
     select="@*|order[not(string(ParentID))]"/> 
    </xsl:copy> 
    </xsl:template> 

    <xsl:template match="order"> 
    <xsl:copy> 
     <!--Don't apply-templates to ParentID; we don't want to keep those. 
     Do apply-templates to the key 'orderByParentId' when the key matches 
     the current order's ID child.--> 
     <xsl:apply-templates 
     select="@*|*[not(self::ParentID)]|key('orderByParentId',ID)"/> 
    </xsl:copy> 
    </xsl:template> 

</xsl:stylesheet> 

输出

<main> 
    <order> 
     <ID>123</ID> 
     <Name>ABC</Name> 
     <order> 
     <ID>4556</ID> 
     <Name>AAA</Name> 
     </order> 
    </order> 
</main> 
0

一种方法给所有order节点与复制索引n>1进入第一个是以下XSLT:

<?xml version = "1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

    <xsl:template match="text()" /> <!-- suppresses copying of text() nodes --> 

    <xsl:template match="main">  <!-- replicates the "main" node --> 
     <main> 
     <xsl:apply-templates /> 
     </main> 
    </xsl:template> 

    <xsl:template match="order[1]"> <!-- matches the first "order" node and copies all following ones --> 
     <order> 
     <xsl:copy-of select="*" /> 
     <xsl:copy-of select="following-sibling::order" /> 
     </order> 
    </xsl:template>  
</xsl:stylesheet> 

输出如下:

<?xml version="1.0"?> 
<main> 
    <order> 
     <ID>123</ID> 
     <Name>ABC</Name> 
     <order> 
      <ID>4556</ID> 
      <Name>AAA</Name> 
      <ParentID>123</ParentID> 
     </order> 
    </order> 
</main> 
+0

如果有很多订单,我只是想根据他们的ID将他们分组,他们的订单具有与其他订单的ID相同的订单,必须合并。并非所有订单都存在。 @ zx485 –

+0

@Tarun:对不起,我甚至都不知道你的附加问题。上面的michael.hor257k告诉你,这个问题可能与你之前的一个问题非常相似。无论如何我都回答了​​。但是您的评论似乎表明了一组全新的参数,您最好应该用一个精确的[MCVE](http://stackoverflow.com/help/mcve)将所需的结果放入一个新问题中。 – zx485