Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > How do I extract a repeating attribute value out to display it only once (using XSLT 1.0).

Reply
Thread Tools

How do I extract a repeating attribute value out to display it only once (using XSLT 1.0).

 
 
mark4asp
Guest
Posts: n/a
 
      03-04-2008
Apologies, I just can't get my head around xslt but I need to do this.

I have an xml file with two attributes per product. One of the
attributes repeats to produce several groups (3 in the example, grouped
by region). How can I select this repeating attribute out as a header
column in a table with the other attribute as in a data column. (see
example html). XSLT 1.0 solutions only please (or whatever is
compatible with current major browser parsers [FF, IE6, IE7, Opera,
Safari]).

source xml:

<sales>
<product region="NW" item="Drill" />
<product region="NW" item="Fork" />
<product region="NW" item="Spade" />
<product region="SE" item="Brick" />
<product region="SE" item="Hammer" />
<product region="SE" item="Nail" />
<product region="SW" item="Chisel" />
<product region="SW" item="Screw" />
</sales>

target html:

<html>
<body>
<table>
<tr><th>NW</th></tr>
<tr><td>Drill</td></tr>
<tr><td>Fork</td></tr>
<tr><td>Spade</td></tr>
<tr><th>SE</th></tr>
<tr><td>Brick</td></tr>
<tr><td>Hammer</td></tr>
<tr><td>Nail</td></tr>
<tr><th>SW</th></tr>
<tr><td>Chisel</td></tr>
<tr><td>Screw</td></tr>
</table>
</body>
</html>

 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      03-04-2008
mark4asp wrote:
> Apologies, I just can't get my head around xslt but I need to do this.
>
> I have an xml file with two attributes per product. One of the
> attributes repeats to produce several groups (3 in the example, grouped
> by region). How can I select this repeating attribute out as a header
> column in a table with the other attribute as in a data column. (see
> example html). XSLT 1.0 solutions only please (or whatever is
> compatible with current major browser parsers [FF, IE6, IE7, Opera,
> Safari]).


It is a grouping problem, solved in XSLT 1.0 using Muenchian grouping:

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

<xslutput method="html" indent="yes"/>

<xsl:key name="by-region" match="product" use="@region"/>

<xsl:template match="sales">
<html lang="en">
<head>
<title>Example</title>
</head>
<body>
<table>
<tbody>
<xsl:apply-templates
select="product[generate-id() =
generate-id(key('by-region', @region)[1])]" mode="th"/>
</tbody>
</table>
</body>
</html>
</xsl:template>

<xsl:template match="product" mode="th">
<tr>
<th><xsl:value-of select="@region"/></th>
</tr>
<xsl:apply-templates select="key('by-region', @region)" mode="td"/>
</xsl:template>

<xsl:template match="product" mode="td">
<tr>
<td><xsl:value-of select="@item"/></td>
</tr>
</xsl:template>

</xsl:stylesheet>

--

Martin Honnen
http://JavaScript.FAQTs.com/
 
Reply With Quote
 
 
 
 
Richard Tobin
Guest
Posts: n/a
 
      03-04-2008
In article <023e2a0a$0$20024$(E-Mail Removed)>,
mark4asp <(E-Mail Removed)> wrote:

>I have an xml file with two attributes per product. One of the
>attributes repeats to produce several groups (3 in the example, grouped
>by region). How can I select this repeating attribute out as a header
>column in a table with the other attribute as in a data column.


You may find this helpful:

http://www.jenitennison.com/xslt/grouping/muenchian.xml

-- Richard
--
:wq
 
Reply With Quote
 
mark4asp
Guest
Posts: n/a
 
      03-04-2008
Martin Honnen wrote:

> mark4asp wrote:
> > Apologies, I just can't get my head around xslt but I need to do
> > this.
> >
> > I have an xml file with two attributes per product. One of the
> > attributes repeats to produce several groups (3 in the example,
> > grouped by region). How can I select this repeating attribute out
> > as a header column in a table with the other attribute as in a data
> > column. (see example html). XSLT 1.0 solutions only please (or
> > whatever is compatible with current major browser parsers [FF, IE6,
> > IE7, Opera, Safari]).

>
> It is a grouping problem, solved in XSLT 1.0 using Muenchian grouping:
>
> <xsl:stylesheet
> xmlnssl="http://www.w3.org/1999/XSL/Transform"
> version="1.0">
>
> <xslutput method="html" indent="yes"/>
>
> <xsl:key name="by-region" match="product" use="@region"/>
>
> <xsl:template match="sales">
> <html lang="en">
> <head>
> <title>Example</title>
> </head>
> <body>
> <table>
> <tbody>
> <xsl:apply-templates
> select="product[generate-id() =
> generate-id(key('by-region', @region)[1])]" mode="th"/>
> </tbody> </table>
> </body>
> </html>
> </xsl:template>
>
> <xsl:template match="product" mode="th">
> <tr>
> <th><xsl:value-of select="@region"/></th>
> </tr>
> <xsl:apply-templates select="key('by-region', @region)"
> mode="td"/> </xsl:template>
>
> <xsl:template match="product" mode="td">
> <tr>
> <td><xsl:value-of select="@item"/></td>
> </tr>
> </xsl:template>
>
> </xsl:stylesheet>



Thanks Richard and Martin. For some reason the example code in O'Reilly
XSLT Cookbook 2e, for Muenchian grouping did not work (no details only
a summary) and poking about with it didn't make it work. In fact, only
the first grouping example in chapter 10 (Recipe 10.3. Creating HTML
Tables) would work for me in FF.

I will def. read the article Richard posted.
 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
[XSLT] Select node by attribute with value from an attribute of its self Stefan_W XML 1 11-05-2008 10:50 AM
XSLT Newbie: Attribute Value transformed to Attribute Name bprimuslevy XML 1 08-23-2006 09:56 PM
Loop Form - extract display 'value' as well as form value? ASP General 2 02-20-2006 09:36 PM
XPath / XSLT to extract 'overloaded' attribute Iain XML 9 12-03-2003 10:04 AM



Advertisments