2016-11-08 3 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

В этом случае мы сравниваем заказы с идентификатором и родительским идентификатором, который отличаются «использование» в ключе @ michael.hor257k –

+0

Я считаю, что это то же самое. –

ответ

0

Как @ michael.hor257k сказал, концептуально этот вопрос так же, как ваш предыдущий вопрос. Я думаю, вы просто не понимаете концепцию. Надеюсь, что комментарии в моем примере помогут.

Если я правильно понял вопрос, вы хотите вложить order элементов в их «родительский» элемент order на основе ParentID. Так как мы основывая его на ParentID, это то, что мы будем использовать для нашего ключа ...

XML Input

<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 другого заказа, должен быть объединен. не все присутствующие приказы. @ zx485 –

+0

@Tarun: Извините, я даже не отдаленно понимаю ваш дополнительный вопрос. michael.hor257k выше сказал вам, что этот вопрос, возможно, был очень похож на один из ваших предыдущих вопросов. Я все равно ответил. Но ваш комментарий, кажется, указывает на совершенно новый набор параметров, которые вам лучше включить в новый вопрос с точным [MCVE] (http://stackoverflow.com/help/mcve) желаемого результата. – zx485

Смежные вопросы