XSLT xpath problem

Discussion in 'XML' started by Tjerk Wolterink, Jul 14, 2005.

  1. I have xml like this:

    <data>
    <item><type>a</type></item>
    <item><type>a</type></item>
    <item><type>b</type></item>
    <item><type>b</type></item>
    <item><type>b</type></item>
    <item><type>c</type></item>
    </data>

    I want an xsl document that transforms this in this:

    <ul>
    <li>a</li>
    <li>b</li>
    <li>c</li>
    </ul>

    My xsl:

    <xsl:template match="data">
    <uL>
    <xsl:for-each select="item">
    <xsl:if test=" the type of the item before this item is different">
    <li><xsl:value-of select="type"/></li>
    </xsl:if>
    </xsl:for-each>
    </ul>
    </xs:template>

    pleaze help
     
    Tjerk Wolterink, Jul 14, 2005
    #1
    1. Advertising

  2. This is a grouping problem. For more information see:

    http://jenitennison.com/xslt/grouping/index.html

    Cheers,
    Dimitre Novatchev.

    "Tjerk Wolterink" <> wrote in message
    news:db5f44$jfs$...
    >I have xml like this:
    >
    > <data>
    > <item><type>a</type></item>
    > <item><type>a</type></item>
    > <item><type>b</type></item>
    > <item><type>b</type></item>
    > <item><type>b</type></item>
    > <item><type>c</type></item>
    > </data>
    >
    > I want an xsl document that transforms this in this:
    >
    > <ul>
    > <li>a</li>
    > <li>b</li>
    > <li>c</li>
    > </ul>
    >
    > My xsl:
    >
    > <xsl:template match="data">
    > <uL>
    > <xsl:for-each select="item">
    > <xsl:if test=" the type of the item before this item is different">
    > <li><xsl:value-of select="type"/></li>
    > </xsl:if> </xsl:for-each>
    > </ul>
    > </xs:template>
    >
    > pleaze help
     
    Dimitre Novatchev, Jul 14, 2005
    #2
    1. Advertising

  3. A Dimitre says this is a grouping problem but if you are in the special
    case that you know your input is already in the right order, you can
    code it more or less exactly as you sketched, without having to use
    grouping at the xslt level

    <xsl:if test=" the type of the item before this item is different">

    is

    <xsl:if test="not(type = preceding-sibling::item[1]/type)">

    David
     
    David Carlisle, Jul 14, 2005
    #3
  4. Tjerk Wolterink

    Mukul Gandhi Guest

    You may try this. This is Muenchian grouping, and is efficient. Or you
    may use David's method.

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

    <xsl:eek:utput method="html" indent="yes" />

    <xsl:key name="by-type" match="item/type" use="." />

    <xsl:template match="/data">
    <html>
    <head>
    <title/>
    </head>
    <body>
    <ul>
    <xsl:for-each select="item/type[generate-id() =
    generate-id(key('by-type', .)[1])]">
    <li><xsl:value-of select="." /></li>
    </xsl:for-each>
    </ul>
    </body>
    </html>
    </xsl:template>

    </xsl:stylesheet>

    Regards,
    Mukul
     
    Mukul Gandhi, Jul 14, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marvin_123456

    "Memory leak" in javax.xml.xpath.XPath

    Marvin_123456, Jul 29, 2005, in forum: Java
    Replies:
    4
    Views:
    1,973
    jan V
    Jul 29, 2005
  2. Alastair Cameron
    Replies:
    1
    Views:
    7,412
    SQL Server Development Team [MSFT]
    Jul 8, 2003
  3. Patrick Reilly

    XSLT and XPath lookup problem

    Patrick Reilly, Sep 27, 2003, in forum: XML
    Replies:
    1
    Views:
    628
    Dimitre Novatchev
    Sep 28, 2003
  4. Flyzone

    XSLT: problem with Xpath

    Flyzone, Jan 25, 2005, in forum: XML
    Replies:
    1
    Views:
    374
    Mike King
    Jan 25, 2005
  5. Pathik
    Replies:
    1
    Views:
    470
Loading...

Share This Page