XSLT Group和Merge

时间:2015-09-15 12:50:40

标签: xslt

尝试合并以下内容,以便:

<?xml version="1.0" encoding="utf-8"?>
<Selections repeat="yes">
    <Item>
        <Title>One</Title>
        <Details repeat="yes">
            <item>
                <Detail>First</Detail>
            </item>
            <item>
                <Detail>Second</Detail>
            </item>
        </Details>
    </Item>
    <Item>
        <Title>Two</Title>
        <Details repeat="yes">
            <Item>
                <Detail>First</Detail>
            </Item>
            <Item>
                <Detail>Second</Detail>
            </Item>
        </Details>
    </Item>
    <Item>
        <Title>Three</Title>
        <Details repeat="yes">
            <Item>
                <Detail>Third</Detail>
            </Item>
        </Details>
    </Item>
    <Item>
        <Title>Three</Title>
        <Details repeat="yes">
            <Item>
                <Detail>First</Detail>
            </Item>
            <Item>
                <Detail>Second</Detail>
            </Item>
        </Details>
    </Item>
    <Item>
        <Title>Four</Title>
        <Status></Status>
        <Details repeat="yes">
            <Item>
                <Detail>First</Detail>
            </Item>
            <Item>
                <Detail>Second</Detail>
            </Item>
            <Item>
                <Detail>Third</Detail>
            </Item>
        </Details>
    </Item>
</Selections>

变为:(任何匹配的标题中的所有项目/详细信息都已合并)

<?xml version="1.0" encoding="utf-8"?>
<Selections repeat="yes">
    <Item>
        <Title>One</Title>
        <Details repeat="yes">
            <item>
                <Detail>First</Detail>
            </item>
            <item>
                <Detail>Second</Detail>
            </item>
        </Details>
    </Item>
    <Item>
        <Title>Two</Title>
        <Details repeat="yes">
            <Item>
                <Detail>First</Detail>
            </Item>
            <Item>
                <Detail>Second</Detail>
            </Item>
        </Details>
    </Item>
    <Item>
        <Title>Three</Title>
        <Details repeat="yes">
            <Item>
                <Detail>First</Detail>
            </Item>
            <Item>
                <Detail>Second</Detail>
            </Item>
            <Item>
                <Detail>Third</Detail>
            </Item>
        </Details>
    </Item>
    <Item>
        <Title>Four</Title>
        <Status></Status>
        <Details repeat="yes">
            <Item>
                <Detail>First</Detail>
            </Item>
            <Item>
                <Detail>Second</Detail>
            </Item>
            <Item>
                <Detail>Third</Detail>
            </Item>
        </Details>
    </Item>
</Selections>

使用XSLT 2.0但并没有完全分组,因为它似乎会复制条目。

提前致谢。

抱歉忘了我的尝试:

    

<xsl:template match="/">
    <Selections repeat="yes">
        <xsl:for-each-group select="/Selections/Item" group-by="Title">
            <Item>
                <Title>
                    <xsl:value-of select="current-grouping-key()"/>
                </Title>

                <Details repeat="yes">
                    <xsl:for-each select="current-group()">
                        <xsl:copy-of select="current-group() /Details/Item"/>

                    </xsl:for-each>
                </Details>
            </Item>
        </xsl:for-each-group>
    </Selections>
</xsl:template>

给出:

<?xml version="1.0" encoding="UTF-8"?>
<Selections repeat="yes">
   <Item>
      <Title>One</Title>
      <Details repeat="yes"/>
   </Item>
   <Item>
      <Title>Two</Title>
      <Details repeat="yes">
         <Item>
                <Detail>First</Detail>
            </Item>
         <Item>
                <Detail>Second</Detail>
            </Item>
      </Details>
   </Item>
   <Item>
      <Title>Three</Title>
      <Details repeat="yes">
         <Item>
                <Detail>Third</Detail>
            </Item>
         <Item>
                <Detail>First</Detail>
            </Item>
         <Item>
                <Detail>Second</Detail>
            </Item>
         <Item>
                <Detail>Third</Detail>
            </Item>
         <Item>
                <Detail>First</Detail>
            </Item>
         <Item>
                <Detail>Second</Detail>
            </Item>
      </Details>
   </Item>
   <Item>
      <Title>Four</Title>
      <Details repeat="yes">
         <Item>
                <Detail>First</Detail>
            </Item>
         <Item>
                <Detail>Second</Detail>
            </Item>
         <Item>
                <Detail>Third</Detail>
            </Item>
      </Details>
   </Item>
</Selections>

1 个答案:

答案 0 :(得分:0)

问题在于这一行:

<xsl:for-each select="current-group()">
    <xsl:copy-of select="current-group()/Details/Item"/>
</xsl:for-each>

使用xsl:for-each,您已经在current-group()上进行迭代,因此对于组中的每个项目都有效,然后复制组中的所有项目。

要解决,只需将其更改为:

<xsl:for-each select="current-group()">
    <xsl:copy-of select="Details/Item"/>
</xsl:for-each>

或者,您根本不需要xsl:for-each。现有的生产线也需要xsl:for-each

<Details repeat="yes">
   <xsl:copy-of select="current-group()/Details/Item"/>
</Details>