[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: In XSL how do you group common child nodes and ass

Subject: Re: In XSL how do you group common child nodes and associate duplicateparents to it?
From: Mike Brown <mike@xxxxxxxx>
Date: Thu, 7 Feb 2002 01:52:46 -0700 (MST)
child ass
Su  Dh wrote:
> I have a xml file as shown below
> 
> <A>
>      <B>Test</B>
>      <C>
>           <CName>C1</CName>
>           <D>
>                <Dname>D1</DName>
>                <Dname>D2</DName>
>                <Dname>D3</DName>
>           </D>
>      </C>
> 
>      <B>Test</B>
>      <C>
>           <CName>C2</CName>
>           <D>
>                <Dname>D1</DName>
>                <Dname>D4</DName>
>           </D>
>      </C>
> </A>

Make sure your <DName> start tags match the </DName> end tags.
 
> Output Required:
> 
> <tr>
> <td> D1 </td>
> <td> C1 </td>
> <td> C2 </td>
> </tr>
> 
> <tr>
> <td> D2 </td>
> <td> C1 </td>
> </tr>
> <tr>
> <td> D3 </td>
> <td> C1 </td>
> </tr>
> <tr>
> <td> D4 </td>
> <td> C2 </td>
> </tr> 

The structure of your XML makes for interesting backtracking in the
XPath expressions, but this is more or less a grouping FAQ.

1. identify a node-set of all DName elements

2. iterate over that set, testing each one to see if it is the last
   with its string-value (just a way of identifying one DName for
   each unique string-value)

3. each node the passes the test causes a table row to be generated.
   the first cell contains the string-value of the current node.
   we'll call the current node $c

4. identify a node-set of all CName element preceding-siblings of
    all elements that are parents of
     all Dname elements that have the same string-value as $c.
   generate a table cell for each of these nodes.


<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="html" indent="yes"/>

  <xsl:template match="/">
    <xsl:variable name="all-DName" select="A/C/D/DName"/>
    <xsl:for-each select="$all-DName[not(. = following-sibling::DName|../../following::DName)]">
      <xsl:sort select="."/>
      <tr>
        <td><xsl:value-of select="."/></td>
        <xsl:for-each select="$all-DName[. = current()]/../preceding-sibling::CName">
          <td><xsl:value-of select="."/></td>
        </xsl:for-each>
      </tr>
    </xsl:for-each>
  </xsl:template>

</xsl:stylesheet>

Note that you are creating a table where each row might have a different
number of columns. Your browser may be able to compensate for these bad
tables, but you should think about how to fill in the correct number of cells
for each row. It's not that hard... you need to figure out, in advance, how 
many cells the row with most cells will have. I'll leave that to you :)

   - Mike
____________________________________________________________________________
  mike j. brown, fourthought.com  |  xml/xslt: http://skew.org/xml/
  denver/boulder, colorado, usa   |  personal: http://hyperreal.org/~mike/

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.