基于类似ID组合XML节点

时间:2014-08-10 00:12:54

标签: xml xslt xslt-1.0

我正在使用一种服务,该服务将数据作为多个服务请求返回。 我想使用XSLT 1.0对具有相同主要和次要ID(1,1.1,1.2.1.3,1.4 ... 2,2.1 .... N)等的请求进行分组,以便我们得到以下结果:< / p>

输入:

<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>

所需的输出

<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>

任何有关此问题的帮助都会受到很大的影响。

1 个答案:

答案 0 :(得分:1)

很难从单个示例中推断出确切的规则,但我相信这会以最小的麻烦返回正确的结果:

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>

注意:
如果在“主要服务”之后总是有4个“子服务”,那么您可以使用它来提高代码效率。