如何在XSLT中对元素进行分组 [英] how to group elements in XSLT

查看:82
本文介绍了如何在XSLT中对元素进行分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

难以通过对元素XSLT进行分组来解决问题. 我必须使用xsl:键吗?如果是这样,该怎么做 或更多的xsl:每个? 这是我的问题.我的XML文件:

Faced with difficulties in solving the problem by means of grouping elements XSLT. Do I have to use the xsl: key? if so, how to do it or more xsl: for-each? Here is my problem. My XML file:

<page>
<name>test</name>
<property id="416" name="country" type="relation">
    <title>Country</title>
        <value>
            <item id="1014" name="Canada"/>
        </value>
    </property>
</page>

和这样的物品20,每个都有一个名称(可以重复使用,并且可以重复使用其自己的国家/地区) 如何得出按国家分组的此类要素? 例如:

and items such pieces 20, each has a name (non-recurring, and their own country, can be repeated) How to derive such elements grouped by country? for example:

<h1>Canada</h1>
<h2>test<h2>
<h2>test2<h2>

<h1>England</h1>
<h2>test3</h2>
<h2>test3</h2>

更新:

   <page id="423" parentId="421" link="/producers/oao_nii_elpa/" is-active="1" object-id="1020" type-id="67" type-guid="catalog-category" update-time="1350295423" alt-name="oao_nii_elpa">
<basetype id="44" module="catalog" method="category">Catalog category</basetype>
<name>Nii elpa</name>
<properties>
<group id="130" name="common">
<title>Params</title>
<property id="116" name="h1" type="string">
<title>H1 field</title>
<value>Nii elpa</value>
</property>
</group>
<group id="131" name="menu_view">
<title>Menu View</title>
<property id="123" name="header_pic" type="img_file">
<title>Header_Pic</title>
<value path="./images/cms/headers/elpa.jpg" folder="/images/cms/headers" name="elpa" ext="jpg" width="139" height="63">/images/cms/headers/elpa.jpg</value>
</property>
</group>
<group id="133" name="additional">
<title>Additional</title>
<property id="416" name="country" type="relation">
<title>Country</title>
<value>
<item id="3" guid="a1e3ae17e80ba2b4a3ddb1b855430346f74b8d48" name="England" type-id="4" type-guid="d69b923df6140a16aefc89546a384e0493641fbe" ownerId="42" xlink:href="uobject://3"/>
</value>
</property>
</group>
</properties>
</page>

推荐答案

对于XSLT 1.0,分组方法是通过muenchian方法完成的,例如:
(来源: http://www.jenitennison.com/xslt/grouping/muenchian.html )

For XSLT 1.0 the way to group is done by the muenchian method, e.g.:
(source: http://www.jenitennison.com/xslt/grouping/muenchian.html)

我们有一个XML:

<records>
    <contact id="0001">
        <title>Mr</title>
        <forename>John</forename>
        <surname>Smith</surname>
    </contact>
    <contact id="0002">
        <title>Dr</title>
        <forename>Amy</forename>
        <surname>Jones</surname>
    </contact>
    ...
</records>

我们不会按姓氏分组,并产生以下输出:

And we wan't to group by surname, and produce the following output:

Jones,<br />
    Amy (Dr)<br />
    Brian (Mr)<br />
Smith,<br />
    Fiona (Ms)<br />
    John (Mr)<br />

这是通过以下XSLT 1.0模板和密钥完成的:

This is accomplished by the following XSLT 1.0 template and key:

<xsl:key name="contacts-by-surname" match="contact" use="surname" />
<xsl:template match="records">
    <xsl:for-each select="contact[count(. | key('contacts-by-surname', surname)[1]) = 1]">
        <xsl:sort select="surname" />
        <xsl:value-of select="surname" />,<br />
        <xsl:for-each select="key('contacts-by-surname', surname)">
            <xsl:sort select="forename" />
            <xsl:value-of select="forename" /> (<xsl:value-of select="title" />)<br />
        </xsl:for-each>
    </xsl:for-each>
</xsl:template>

用于您问题的模板如下所示(未试用):

The template for your problem would look like this (untested):

<xsl:key name="page-by-country" match="page" use="property[@name='country']/value/item/@name" />
<xsl:template match="pages">
    <xsl:for-each select="page[count(. | key('page-by-country', property[@name='country']/value/item/@name)[1]) = 1]">
        <xsl:sort select="property[@name='country']/value/item/@name" />
        <h1><xsl:value-of select="property[@name='country']/value/item/@name" /></h1>
        <xsl:for-each select="key('page-by-country', property[@name='country']/value/item/@name)">
            <xsl:sort select="name" />
            <h2><xsl:value-of select="name"/></h2>
        </xsl:for-each>
    </xsl:for-each>
</xsl:template>

请参见以下工作示例以解决您的问题: xsltransform.net

See this working example for your problem: xsltransform.net

这篇关于如何在XSLT中对元素进行分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆