Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > XSLT xpath problem

Reply
Thread Tools

XSLT xpath problem

 
 
Tjerk Wolterink
Guest
Posts: n/a
 
      07-14-2005
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
 
Reply With Quote
 
 
 
 
Dimitre Novatchev
Guest
Posts: n/a
 
      07-14-2005
This is a grouping problem. For more information see:

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

Cheers,
Dimitre Novatchev.

"Tjerk Wolterink" <(E-Mail Removed)> wrote in message
news:db5f44$jfs$(E-Mail Removed)...
>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



 
Reply With Quote
 
 
 
 
David Carlisle
Guest
Posts: n/a
 
      07-14-2005

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
 
Reply With Quote
 
Mukul Gandhi
Guest
Posts: n/a
 
      07-14-2005
You may try this. This is Muenchian grouping, and is efficient. Or you
may use David's method.

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

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Xpath and XSLT-transformation problem Pathik XML 1 03-30-2007 07:42 AM
"Memory leak" in javax.xml.xpath.XPath Marvin_123456 Java 4 07-29-2005 03:49 PM
XSLT: problem with Xpath Flyzone XML 1 01-25-2005 03:25 AM
XSLT and XPath lookup problem Patrick Reilly XML 1 09-28-2003 07:31 AM
Problem selecting a node with XPATH if attribute value contains backslashes - how to force XPATH string to be treated as literal? Alastair Cameron XML 1 07-08-2003 07:24 PM



Advertisments