Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 5th, 2007, 10:14 AM
Registered User
 
Join Date: Jun 2007
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Grouping/Merging

Hello,

I have a problem with grouping respectively merging xml elements. I can only use XPath 1.0 and XSL 1.0.

This is the xml structure:
Code:
<Root>
  <Items>
    <Item id="A1" name="Part" version="1.0"/>
    <Item id="A2" name="Part" version="2.0"/>
    <Item id="A3" name="SubPart" version="0.5"/>
    <Item id="A4" name="SubPart" version="1.1"/>
    <Item id="A5" name="ProductA" version="1.5"/>
    <Item id="A6" name="ProductA" version="2.2"/>
    <Item id="A7" name="ProductB" version="2.1"/>
  </Items>
  <Configurations>
    <Configuration id="B7" idItem="A7" installed="false">
      <Parts>
        <Part key="[ProductB]" id="B7" idItem="A7"/>
        <Part key="[Part]" id="B1" idItem="A1"/>
        <Part key="[SubPart]" id="B9" idItem="A3"/>
      </Parts>
    </Configuration>
    <Configuration id="B3" idItem="A5" installed="false">
      <Parts>
        <Part key="[Part]" id="B1" idItem="A1"/>
        <Part key="[ProductA]" id="B3" idItem="A5"/>
        <Part key="[SubPart]" id="B9" idItem="A3"/>
      </Parts>
    </Configuration>
    <Configuration id="B1" idItem="A1" installed="false">
      <Parts>
        <Part key="[Part]" id="B1" idItem="A1"/>
        <Part key="[SubPart]" id="B9" idItem="A3"/>
      </Parts>
    </Configuration>
    <Configuration id="B2" idItem="A2" installed="true">
      <Parts>
        <Part key="[Part]" id="B2" idItem="A2"/>
        <Part key="[SubPart]" id="B10" idItem="A4"/>
      </Parts>
    </Configuration>
    <Configuration id="B4" idItem="A5" installed="true">
      <Parts>
        <Part key="[ProductA]" id="B4" idItem="A5"/>
        <Part key="[Part]" id="B2" idItem="A2"/>
        <Part key="[SubPart]" id="B10" idItem="A4"/>
      </Parts>
    </Configuration>
    <Configuration id="B5" idItem="A6" installed="false">
      <Parts>
        <Part key="[ProductA]" id="B5" idItem="A6"/>
        <Part key="[Part]" id="B1" idItem="A1"/>
        <Part key="[SubPart]" id="B9" idItem="A3"/>
      </Parts>
    </Configuration>
    <Configuration id="B6" idItem="A6" installed="true">
      <Parts>
        <Part key="[ProductA]" id="B6" idItem="A6"/>
        <Part key="[Part]" id="B2" idItem="A2"/>
        <Part key="[SubPart]" id="B10" idItem="A4"/>
      </Parts>
    </Configuration>
    <Configuration id="B8" idItem="A7" installed="true">
      <Parts>
        <Part key="[ProductB]" id="B8" idItem="A7"/>
        <Part key="[Part]" id="B2" idItem="A2"/>
        <Part key="[SubPart]" id="B10" idItem="A4"/>
      </Parts>
    </Configuration>
    <Configuration id="B9" idItem="A3" installed="true">
      <Parts>
        <Part key="[SubPart]" id="B9" idItem="A3"/>
      </Parts>
    </Configuration>
    <Configuration id="B10" idItem="A4" installed="true">
      <Parts>
        <Part key="[SubPart]" id="B10" idItem="A4"/>
      </Parts>
    </Configuration>
  </Configurations>
</Root>
There are seven items and each item has different configurations.

The Part elements in the element Parts are not always in the same order
and the element Parts doesn't contain always the same amount of Part
elements for one item name.

It could be possible that there are double versions, e.g.:
ProductA V1.5 consists of:
  • ProductA V1.5, Part V1.0 and SubPart V0.5
  • ProductA V1.5, Part V2.0 and SubPart V1.1
The desired output should be a table for each item name.
Code:
Part
----------------------
|  Part  |  SubPart  |
----------------------
|  1.0   |    0.5    |
----------------------
|  2.0   |    1.1    |
----------------------

ProductA
----------------------------------
|  Part  |  ProductA |  SubPart  |
----------------------------------
|   1.0  |     1.5   |    0.5    |
----------------------------------
|   2.0  |     1.5   |    1.1    |
----------------------------------
|   1.0  |     2.2   |    0.5    |
----------------------------------
|   2.0  |     2.2   |    1.1    |
----------------------------------

ProductB
----------------------------------
|  Part  |  ProductB |  SubPart  |
----------------------------------
|   1.0  |     2.1   |    0.5    |
----------------------------------
|   2.0  |     2.1   |    1.1    |
----------------------------------

SubPart
-------------
|  SubPart  |
-------------
|    0.5    |
-------------
|    1.1    |
-------------
I started with this xsl style sheet to get the superset of the headings (parts) from all configurations of an item name:
Code:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:key name="name" match="Item" use="@name"/>
  <xsl:key name="headings" match="Parts/*" use="@key"/>

  <xsl:variable name="configs" select="//Root/Configurations/Configuration"/>
  <xsl:variable name="items" select="//Root/Items/Item"/>

  <xsl:template match="/">
    <html>
      <body>
        <xsl:for-each select="./Root/Items/Item[count(.|key('name', @name)[1]) = 1]">
          <xsl:sort select="./@name"/>
          <xsl:value-of select="./@name"/>
          <br/>
          <table>
            <tr>
              <xsl:variable name="name" select="./@name"/>
              <xsl:variable name="idsItems" select="../Item[@name=$name]/@id"/>
              <xsl:variable name="parts" select="$configs[@idItem=$idsItems]/Parts/*"/>



              <xsl:for-each select="$parts[count(.|key('headings', @key)[1])=1]">
                <xsl:sort select="./@key"/>
                <th>
                  <xsl:variable name="selectId" select="./@idItem"/>
                  <xsl:value-of select="$items[@id=$selectId]/@name"/>
                </th>
              </xsl:for-each>

            </tr>
          </table>
          <br/>
        </xsl:for-each>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>
The output of this xsl style sheet is:
Code:
Part

ProductA
-------------
|  ProductA |
-------------

ProductB
----------------------------------
|  Part  |  ProductB |  SubPart  |
----------------------------------

SubPart
What is wrong with the loop?

Thanks and regards

Jens
Reply With Quote
  #2 (permalink)  
Old June 5th, 2007, 11:45 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Thanks for submitting such a clear problem. It's surprisingly rare on this forum.

In the line:

<xsl:for-each select="$parts[count(.|key('headings', @key)[1])=1]">

you're selecting a Part element if it's the first Part with that key in the document. So the first time round $parts consists of four Part elements, with idItem=A1 or A2, and in each case there is another Part with the same key that's earlier in the document and that isn't a member of $parts. (This tends to be where Muenchian grouping breaks down - everything is scoped to the document).

As I get no pleasure from solving grouping problems in XSLT 1.0 when there is newer technology that makes the job dead easy, I'll leave the solution to you.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #3 (permalink)  
Old June 6th, 2007, 02:50 AM
Registered User
 
Join Date: Jun 2007
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello Micheal,

thanks for your answer.
Then I will look if I could solve it in another way.

Regards

Jens
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Merging nodes trishla XSLT 11 October 18th, 2007 03:01 PM
Merging in flex rsshanmugapriya Beginning VB 6 2 December 16th, 2006 01:54 AM
Protect cells and allow grouping/un-grouping sfreuden Excel VBA 4 December 14th, 2006 08:01 AM
Merging reports siptah Access VBA 1 May 13th, 2005 08:05 AM
Merging two xslts into one...help!! nrane26 XSLT 1 January 7th, 2005 04:21 PM



All times are GMT -4. The time now is 02:36 AM.


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