[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] In XSL how do you group common child nodes and associate duplicate parents to it?


Subject: Re: [xsl] In XSL how do you group common child nodes and associate duplicate parents to it?
From: Mike Brown <mike@xxxxxxxx>
Date: Thu, 7 Feb 2002 01:52:46 -0700 (MST)

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
Keywords