XSLT - 基于属性值的递归分组/计数

时间:2017-10-03 15:48:07

标签: xslt

我正在努力解决基于不同值的简单递归计数问题。从列表如:

<List>
    <Place continent="Asia" country="India" region="UP">Lucknow</Place>
    <Place continent="Europe" country="France" region="Provence">Marseille</Place>
    <Place continent="Europe" country="Greece" region="Arcadia">Dimitsana</Place>
    <Place continent="Europe" country="Italy" region="Sicily">Sicily</Place>
    <Place continent="Asia" country="India" region="Maharastra">Pune</Place>
    <Place continent="Asia" country="China">China</Place>
    <Place continent="Europe" country="France" region="Provence">Marseille</Place>
    <Place continent="America" country="Brasil" region="Pará">Belém</Place>
</List>

我希望根据大陆,国家和地区属性的不同值(在某些情况下是可选的)检索输出,这将按递归方式计算每个标记,如下所示:

<Places>
<continent name="America" count="1">
    <country name="Brasil" count="1">
        <region name="Pará" count="1">
            <city name="Belém" count="1"/>
        </region>
    </country>
</continent>
<continent name="Asia" count="3">
    <country name="India" count="2">
        <region name="Maharastra" count="1">
            <city name="Pune" count="1"/>
        </region>
        <region name="UP" count="1">
            <city name="Lucknow" count="1"/>
        </region>
    </country>
    <country name="China" count="1"/>
</continent>
<continent name="Europe" count="4">
    <country name="France" count="2">
        <region name="Provence" count="2">
            <city name="Marseille" count="2"/>
        </region>
    </country>
    <country name="Greece" count="1">
        <region name="Arcadia" count="1">
            <city name="Dimitsana" count="1"/>
        </region>
    </country>
    <country name="Italy" count="1">
        <region name="Sicily" count="1"/>
    </country>
</continent>

我可以设法独立检索每组不同的值(大陆,国家,地区),例如:

<xsl:template match="List">
    <Places> 
        <xsl:for-each-group select="Place" group-by="@continent"> 
            <continent>
                <xsl:attribute name="name">
                    <xsl:value-of select="@continent"/>
                </xsl:attribute>
                <xsl:attribute name="count">
                    <xsl:value-of select="count(current-group())"/>
                </xsl:attribute>
            </continent> 
        </xsl:for-each-group>
    </Places>

- 但是,虽然我认为我遗漏了一些非常基本的东西,但我无法获得嵌套分组并计算大陆内的国家等等。非常感谢提前。

0 个答案:

没有答案