XSLT xpath problem

T

Tjerk Wolterink

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
 
D

David Carlisle

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
 
M

Mukul Gandhi

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top