开发者

xslt 1.0 grouping by attribute (muenchian)

I have problems with xslt and grouping the following 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>

I have tried munchian grouping without success.

What XSLT is the correct way to group by the attribute @htn?


Define an xsl:key to group the data elements by their @htn values:

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

Then you can use the following XPath expression to retrieve a unique listing of the @htn values:

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

This XPath expression would also work:

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

Applied in a stylesheet:

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

When applied to the sample XML, it produces the following output that lists the data elements grouped by the @htn values:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜