Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 8th, 2006, 08:08 PM
Registered User
 
Join Date: Dec 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default Selecting unique value of an attribute and render

Hello, I am new to XSLT.
I have a following xml:
<entries>
  <entry><cell row="1" col="1">Item</cell></entry>
  <entry><cell row="1" col="2" >Price</cell></entry>
  <entry><cell row="1" col="3">Total</cell></entry>
  <entry><cell row="2" col="1">3</cell></entry>
  <entry><cell row="2" col="2" >40</cell></entry>
  <entry><cell row="2" col="3">120</cell></entry>
</entries>

I want to write an xsl to output the xml like this:

<rows>
   <row id="1">
     <cell id="1">Item</cell>
     <cell id="2">Price</cell>
     <cell id="3">Total</cell>
   </row>
   <row id="2">
     <cell id="1">3</cell>
     <cell id="2">40</cell>
     <cell id="3">120</cell>
   </row>
</rows>

Please post any ideas....
 
Old December 8th, 2006, 08:19 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

If you can use XSLT 2.0 (which is much more powerful than 1.0) then there's a special construct for this:

<xsl:for-each-group select="entry" group-adjacent="cell/@row">
  <row id="{current-grouping-key()}">
    <xsl:for-each select="current-group()">
      <cell id="{cell/@col">
         <xsl:value-of select="."/>
      </
    </
  </
</

In XSLT 1.0 it's rather trickier. For the general solution to the grouping problem go to http://www.jenitennison.com/xslt/grouping.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old December 11th, 2006, 03:55 PM
Registered User
 
Join Date: Dec 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for your reply. I tried the following in XSLT 1.0. As you said it was tricky. My Chief Architect helped me to get this one working.

 <xsl:template match="entries">
        <rows>
            <xsl:for-each select="//cell[generate-id(.) = generate-id( key('rows', @row)[1])]">
                <row id="{@row}">
                    <cell><xsl:value-of select="@row"/></cell>
                    <xsl:apply-templates select="key('rows', @row)"/>
                </row>
            </xsl:for-each>
        </rows>
    </xsl:template>
 
Old December 15th, 2006, 07:48 PM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
Default

Hi vinaura,

I am trying to learn grouping by myself and would appreciate if you could send the whole source code for me to have a look. I am sure everyone from the forum would benefit as well.

Cheers

CPall

 
Old December 18th, 2006, 04:35 PM
Registered User
 
Join Date: Dec 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" version="1.0" indent="yes" standalone="yes"/>

    <xsl:key name="rows" match="cell" use="@row"/>

    <xsl:template match="/">
        <rows>
            <xsl:apply-templates select="entries/entry"/>
        </rows>
    </xsl:template>

    <xsl:template match="entries/entry">

            <xsl:for-each select="cell[generate-id(.) = generate-id( key('rows', @row)[1])]">
                <row id="{@row}">
                    <cell><xsl:number count="entry" format="1"/></cell>
                    <xsl:apply-templates select="key('rows', @row)"/>
                </row>
            </xsl:for-each>
    </xsl:template>


    <xsl:template match="cell">
        <cell>
            <xsl:attribute name="id"><xsl:value-of select="preceding-sibling::title"/></xsl:attribute>
            <xsl:value-of select="."/>
        </cell>
    </xsl:template>
</xsl:stylesheet>






Similar Threads
Thread Thread Starter Forum Replies Last Post
finding a unique attribute Navy1991_1 XSLT 1 July 6th, 2008 06:07 PM
unique attribute names for form elements fishmonkey XSLT 3 March 16th, 2008 07:46 PM
Selecting unique set of elements Chamkaur XSLT 1 March 15th, 2007 05:43 AM
Access to attribute values from class of attribute jacob C# 1 October 28th, 2005 01:11 PM
XPath - Selecting nodes based on attribute values billy_bob_the_3rd XML 4 December 1st, 2004 06:12 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.