简体   繁体   English

根据相似的ID组合XML节点

[英]Combining XML nodes based on similar IDs

I am working with a service that returns data as multiple service requests. 我正在使用将数据作为多个服务请求返回的服务。 I would like to group requests with the same major and minor IDs ( 1,1.1,1.2.1.3,1.4 ...2,2.1....N) and so on using XSLT 1.0 so that we get the following results: 我想使用相同的主要和次要ID(1,1.1,1.2.1.3,1.4 ... 2,2.1 .... N)对请求进行分组,以此类推,使用XSLT 1.0,以便获得以下结果:

Input: 输入:

<response>
<Service id="1">
    <order>
    <parentNode>
        <firstname>Kevin</firstname>
        <moddlename>A</moddlename>
        <lastname>Blue</lastname>
    </parentNode>
    </order>   
</Service>
<Service id="1.1">
    <subnode>
        <node4>takeout</node4>     
    </subnode>      
</Service>
<Service id="1.2" >
    <description>
        <item1>takeoutItem1</item1>
        <item2>takeoutItem2</item2>
    </description>      
</Service>
<Service id="1.3" >
   <information>
       <node7>Information Goes here</node7>
   </information>     
</Service>
<Service id="1.4">
    <homeAddress>
        <node8>home address</node8>
        <node9>city</node9>
    </homeAddress>
    <officeAddress>
        <node10>office address</node10>
        <node11>city</node11>
    </officeAddress> 
</Service>
<Service id="2">
    <order>
        <parentNode>
            <firstname>Tony</firstname>
            <moddlename>A</moddlename>
            <lastname>Pink</lastname>
        </parentNode>
    </order>   
</Service>
<Service id="2.1">
    <subnode>
        <node4>dineIn</node4>     
    </subnode>      
</Service>
<Service id="2.2">
    <description>
        <item1>takeoutItem1</item1>
        <item2>takeoutItem2</item2>
    </description>      
</Service>
<Service id="2.3">
    <information>
        <node7>Other information</node7>
    </information>     
</Service>
<Service id="2.4">
    <homeAddress>
        <node8>home address</node8>
        <node9>city</node9>
    </homeAddress>
    <officeAddress>
        <node10>office address</node10>
        <node11>city</node11>
    </officeAddress> 
</Service>

Desired Output 期望的输出

<output>
    <order>
        <parentNode>
            <firstname>Kevin</firstname>
            <moddlename>A</moddlename>
            <lastname>Blue</lastname>
        </parentNode>
        <subnode>
            <node4>takeout</node4>     
        </subnode>      
        <description>
            <item1>takeoutItem1</item1>
            <item2>takeoutItem2</item2>
        </description>      
        <information>
            <node7>Information Goes here</node7>
        </information>     
        <homeAddress>
            <node8>home address</node8>
            <node9>city</node9>
        </homeAddress>
        <officeAddress>
            <node10>office address</node10>
            <node11>city</node11>
        </officeAddress> 
    </order>   
<order>
    <parentNode>
        <firstname>Tony</firstname>
        <moddlename>A</moddlename>
        <lastname>Pink</lastname>
    </parentNode>     
    <subnode>
        <node4>dineIn</node4>     
    </subnode>      
    <description>
        <item1>takeoutItem1</item1>
        <item2>takeoutItem2</item2>
    </description>      
    <information>
        <node7>Other information</node7>
    </information>     
    <homeAddress>
        <node8>home address</node8>
        <node9>city</node9>
    </homeAddress>
    <officeAddress>
        <node10>office address</node10>
        <node11>city</node11>
    </officeAddress>     
</order></output>

Any help with this will be greatly appreaciated. 任何帮助将不胜感激。

It's difficult to deduce the exact rules from a single example, but I believe this returns the correct result with minimum fuss: 很难从一个示例中得出确切的规则,但是我相信这会以最小的麻烦返回正确的结果:

XSLT 1.0 XSLT 1.0

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

<xsl:template match="/">
    <output>
        <xsl:for-each select="response/Service[not(contains(@id, '.'))]">
            <order>
                <xsl:copy-of select="order/parentNode"/>
                <xsl:copy-of select="../Service[starts-with(@id, concat(current()/@id, '.'))]/*"/>
            </order>
        </xsl:for-each> 
    </output>
</xsl:template>

</xsl:stylesheet>

Note: 注意:
If there are always exactly 4 "sub-services" following the "main service", then you can use that to make your code more efficient. 如果在“主服务”之后始终总是有4个“子服务”,则可以使用它来提高代码效率。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM