xslt 1.0按属性分组(muenchian)

时间:2011-06-07 15:08:48

标签: xslt group-by grouping xslt-1.0

我遇到xslt问题并将以下xml分组

<?xml version="1.0" encoding="ISO-8859-1" ?>
<PosXmlResponse ver="2.0" usr="test" typ="search" lng="de" > 
<search typ="pau" sub="" out="detail" pso="2" ka1="-1" ka2="-1" ka3="-1" ka4="">
<date von="07.06.2011" bis="16.06.2011" min="5" max="9" for="Y-m-d" />
<data id="21171889" ope="SAS" htc="AYTLIND          " htn="LINDA" hon="Side-titreyengöl " hol="Turkey" reg="Antalya" stc="4" zzm="D0LA  " rcd="D0" rnm="DZ/2 ERW/SPARZIMMER" ver="A" vnm="ALL INCLUSIVE" ppc="486" hsc="SASAYTLINDD0LA" />
<data id="21171899" ope="SAS" htc="AYTCLTI          " htn="CLUB TITAN " hon="Alanya-kargicak" hol="Turkey" reg="Antalya" stc="4" zzm="D3KA  " rcd="D3" rnm="DZ/2ERW+0-3CHD/STANDARTZI" ver="A" vnm="ALL INCLUSIVE" ppc="528" hsc="SASAYTCLTID3KA" />
<data id="21171895" ope="SAS" htc="AYTLIND          " htn="LINDA" hon="Side-titreyengöl " hol="Turkey" reg="Antalya" stc="4" zzm="D1KA  " rcd="D1" rnm="DZ/2ERW+0-1CHD/STANDARTZI" ver="A" vnm="ALL INCLUSIVE" ppc="528" hsc="SASAYTLINDD1KA" />
<data id="21171893" ope="SAS" htc="AYTLIND          " htn="LINDA" hon="Side-titreyengöl " hol="Turkey" reg="Antalya" stc="4" zzm="12KA  " rcd="12" rnm="FZ1/2ERW+0-3CHD/STANDARTZ" ver="A" vnm="ALL INCLUSIVE" ppc="528" hsc="SASAYTLIND12KA" />
<data id="21171911" ope="SAS" htc="AYTKELA          " htn="KERVANSARAY LARA " hon="Antalya-lara " hol="Turkey" reg="Antalya" stc="5" zzm="D3SU  " rcd="D3" rnm="DZ/2ERW+0-3CHD/SEITL.-MB " ver="U" vnm="ULTRA ALL INCLUSIVE" ppc="640" hsc="SASAYTKELAD3SU" />
<data id="21171928" ope="SAS" htc="AYTGRAS          " htn="GRAND ART SIDE " hon="Side-evrenseki " hol="Turkey" reg="Antalya" stc="5" zzm="D3KA  " rcd="D3" rnm="DZ/2ERW+0-3CHD/STANDARTZI" ver="A" vnm="ALL INCLUSIVE" ppc="640" hsc="SASAYTGRASD3KA" />
<data id="21171907" ope="SAS" htc="AYTKELA          " htn="KERVANSARAY LARA " hon="Antalya-lara " hol="Turkey" reg="Antalya" stc="5" zzm="D3MU  " rcd="D3" rnm="DZ/2ERW+0-3CHD/MEERBLICK " ver="U" vnm="ULTRA ALL INCLUSIVE" ppc="682" hsc="SASAYTKELAD3MU" />
<data id="21171880" ope="SAS" htc="AYTADAL          " htn="ADALYA RESORT" hon="Side-evrenseki " hol="Turkey" reg="Antalya" stc="5" zzm="D1LA  " rcd="D1" rnm="DZ/2ERW+0-1CHD/SPARZIMMER" ver="A" vnm="ALL INCLUSIVE" ppc="767" hsc="SASAYTADALD1LA" />
<data id="21171937" ope="SAS" htc="AYTSIEL          " htn="SIAM ELEGANCE" hon="Belek-bogazkent" hol="Turkey" reg="Antalya" stc="5" zzm="D3KA  " rcd="D3" rnm="DZ/2ERW+0-3CHD/STANDARTZI" ver="A" vnm="ALL INCLUSIVE" ppc="780" hsc="SASAYTSIELD3KA" />
<data id="21171878" ope="SAS" htc="AYTADAL          " htn="ADALYA RESORT" hon="Side-evrenseki " hol="Turkey" reg="Antalya" stc="5" zzm="D1KA  " rcd="D1" rnm="DZ/2ERW+0-1CHD/STANDARTZI" ver="A" vnm="ALL INCLUSIVE" ppc="786" hsc="SASAYTADALD1KA" />
<data id="21171924" ope="SAS" htc="AYTKELA          " htn="KERVANSARAY LARA " hon="Antalya-lara " hol="Turkey" reg="Antalya" stc="5" zzm="22SU  " rcd="22" rnm="FZ2/2ERW+0-4CHD/SEITL.-MB" ver="U" vnm="ULTRA ALL INCLUSIVE" ppc="801" hsc="SASAYTKELA22SU" />
<data id="21171933" ope="SAS" htc="AYTGRAS          " htn="GRAND ART SIDE " hon="Side-evrenseki " hol="Turkey" reg="Antalya" stc="5" zzm="22KA  " rcd="22" rnm="FZ2/2ERW+0-4CHD/STANDARTZ" ver="A" vnm="ALL INCLUSIVE" ppc="880" hsc="SASAYTGRAS22KA" />
<data id="21171946" ope="SAS" htc="AYTSIEL          " htn="SIAM ELEGANCE" hon="Belek-bogazkent" hol="Turkey" reg="Antalya" stc="5" zzm="22KA  " rcd="22" rnm="FZ2/2ERW+0-4CHD/STANDARTZ" ver="A" vnm="ALL INCLUSIVE" ppc="1011" hsc="SASAYTSIEL22KA" />

</search> 
<navigation ist="" lim="13" max="13" />
</PosXmlResponse>

我尝试过munchian分组但没有成功。

哪种XSLT是按属性@htn进行分组的正确方法?

1 个答案:

答案 0 :(得分:4)

定义xsl:keydata个值@htn元素进行分组:

<xsl:key name="data-by-htn" match="data" use="@htn" />

然后,您可以使用以下XPath表达式来检索@htn值的唯一列表:

data[count(. | key('data-by-htn', @htn)[1]) = 1]

这个XPath表达式也可以工作:

data[generate-id(.) = generate-id(key('data-by-htn', @htn)[1])]

应用于样式表:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes" />

    <xsl:key name="data-by-htn" match="data" use="@htn" />

    <xsl:template match="search">
        <data-grouped-by-htn>

            <xsl:for-each select="data[
                                      count(. | key('data-by-htn', @htn)[1]) 
                                       = 1]">
                <xsl:sort select="@htn" />

                <!--Construct an element using the name of the @htn,
                translate spaces into "_" in order to make it an allowable 
                            element name -->
                <xsl:element name="{translate(@htn, ' ','_')}">
                    <!--perserve the @htn for the new element -->
                    <xsl:copy-of select="@htn"/>

                    <!--Iterate over the list of rows 
                        grouped for this @htn value -->
                    <xsl:for-each select="key('data-by-htn', @htn)">
                        <xsl:sort select="@id" />
                        <!--copy the data element into the output -->
                        <xsl:copy-of select="."/>
                    </xsl:for-each>
                </xsl:element>
            </xsl:for-each>

        </data-grouped-by-htn>
    </xsl:template>
</xsl:stylesheet>

当应用于示例XML时,它会生成以下输出,其中列出了按data值分组的@htn元素:

<?xml version="1.0" encoding="UTF-16"?>
<data-grouped-by-htn>
    <ADALYA_RESORT htn="ADALYA RESORT">
        <data id="21171878" ope="SAS" htc="AYTADAL          " htn="ADALYA RESORT" hon="Side-evrenseki " hol="Turkey" reg="Antalya" stc="5" zzm="D1KA  " rcd="D1" rnm="DZ/2ERW+0-1CHD/STANDARTZI" ver="A" vnm="ALL INCLUSIVE" ppc="786" hsc="SASAYTADALD1KA" />
        <data id="21171880" ope="SAS" htc="AYTADAL          " htn="ADALYA RESORT" hon="Side-evrenseki " hol="Turkey" reg="Antalya" stc="5" zzm="D1LA  " rcd="D1" rnm="DZ/2ERW+0-1CHD/SPARZIMMER" ver="A" vnm="ALL INCLUSIVE" ppc="767" hsc="SASAYTADALD1LA" />
    </ADALYA_RESORT>
    <CLUB_TITAN_ htn="CLUB TITAN ">
        <data id="21171899" ope="SAS" htc="AYTCLTI          " htn="CLUB TITAN " hon="Alanya-kargicak" hol="Turkey" reg="Antalya" stc="4" zzm="D3KA  " rcd="D3" rnm="DZ/2ERW+0-3CHD/STANDARTZI" ver="A" vnm="ALL INCLUSIVE" ppc="528" hsc="SASAYTCLTID3KA" />
    </CLUB_TITAN_>
    <GRAND_ART_SIDE_ htn="GRAND ART SIDE ">
        <data id="21171928" ope="SAS" htc="AYTGRAS          " htn="GRAND ART SIDE " hon="Side-evrenseki " hol="Turkey" reg="Antalya" stc="5" zzm="D3KA  " rcd="D3" rnm="DZ/2ERW+0-3CHD/STANDARTZI" ver="A" vnm="ALL INCLUSIVE" ppc="640" hsc="SASAYTGRASD3KA" />
        <data id="21171933" ope="SAS" htc="AYTGRAS          " htn="GRAND ART SIDE " hon="Side-evrenseki " hol="Turkey" reg="Antalya" stc="5" zzm="22KA  " rcd="22" rnm="FZ2/2ERW+0-4CHD/STANDARTZ" ver="A" vnm="ALL INCLUSIVE" ppc="880" hsc="SASAYTGRAS22KA" />
    </GRAND_ART_SIDE_>
    <KERVANSARAY_LARA_ htn="KERVANSARAY LARA ">
        <data id="21171907" ope="SAS" htc="AYTKELA          " htn="KERVANSARAY LARA " hon="Antalya-lara " hol="Turkey" reg="Antalya" stc="5" zzm="D3MU  " rcd="D3" rnm="DZ/2ERW+0-3CHD/MEERBLICK " ver="U" vnm="ULTRA ALL INCLUSIVE" ppc="682" hsc="SASAYTKELAD3MU" />
        <data id="21171911" ope="SAS" htc="AYTKELA          " htn="KERVANSARAY LARA " hon="Antalya-lara " hol="Turkey" reg="Antalya" stc="5" zzm="D3SU  " rcd="D3" rnm="DZ/2ERW+0-3CHD/SEITL.-MB " ver="U" vnm="ULTRA ALL INCLUSIVE" ppc="640" hsc="SASAYTKELAD3SU" />
        <data id="21171924" ope="SAS" htc="AYTKELA          " htn="KERVANSARAY LARA " hon="Antalya-lara " hol="Turkey" reg="Antalya" stc="5" zzm="22SU  " rcd="22" rnm="FZ2/2ERW+0-4CHD/SEITL.-MB" ver="U" vnm="ULTRA ALL INCLUSIVE" ppc="801" hsc="SASAYTKELA22SU" />
    </KERVANSARAY_LARA_>
    <LINDA htn="LINDA">
        <data id="21171889" ope="SAS" htc="AYTLIND          " htn="LINDA" hon="Side-titreyengöl " hol="Turkey" reg="Antalya" stc="4" zzm="D0LA  " rcd="D0" rnm="DZ/2 ERW/SPARZIMMER" ver="A" vnm="ALL INCLUSIVE" ppc="486" hsc="SASAYTLINDD0LA" />
        <data id="21171893" ope="SAS" htc="AYTLIND          " htn="LINDA" hon="Side-titreyengöl " hol="Turkey" reg="Antalya" stc="4" zzm="12KA  " rcd="12" rnm="FZ1/2ERW+0-3CHD/STANDARTZ" ver="A" vnm="ALL INCLUSIVE" ppc="528" hsc="SASAYTLIND12KA" />
        <data id="21171895" ope="SAS" htc="AYTLIND          " htn="LINDA" hon="Side-titreyengöl " hol="Turkey" reg="Antalya" stc="4" zzm="D1KA  " rcd="D1" rnm="DZ/2ERW+0-1CHD/STANDARTZI" ver="A" vnm="ALL INCLUSIVE" ppc="528" hsc="SASAYTLINDD1KA" />
    </LINDA>
    <SIAM_ELEGANCE htn="SIAM ELEGANCE">
        <data id="21171937" ope="SAS" htc="AYTSIEL          " htn="SIAM ELEGANCE" hon="Belek-bogazkent" hol="Turkey" reg="Antalya" stc="5" zzm="D3KA  " rcd="D3" rnm="DZ/2ERW+0-3CHD/STANDARTZI" ver="A" vnm="ALL INCLUSIVE" ppc="780" hsc="SASAYTSIELD3KA" />
        <data id="21171946" ope="SAS" htc="AYTSIEL          " htn="SIAM ELEGANCE" hon="Belek-bogazkent" hol="Turkey" reg="Antalya" stc="5" zzm="22KA  " rcd="22" rnm="FZ2/2ERW+0-4CHD/STANDARTZ" ver="A" vnm="ALL INCLUSIVE" ppc="1011" hsc="SASAYTSIEL22KA" />
    </SIAM_ELEGANCE>
</data-grouped-by-htn>