Hi all,

I'm having trouble coming up with the correct XSLT code and was wondering if anyone could point me in the right direction. This is what I need to do...


I've got an xml tree with pages like so:

<page>
<title>A<title>
<subtitle>I</subtitle>
<item>1</item>
</page>
<page>
<title>D<title>
<subtitle>V</subtitle>
<item>7</item>
</page>
<page>
<title>A<title>
<subtitle>II</subtitle>
<item>5</item>
</page>
<page>
<title>A<title>
<subtitle>I</subtitle>
<item>9</item>
</page>

I want to be able to write some XSL code to display the data in a nested format like so:

-A
  -I
    -1
    -9
  -II
    -5
-D
  -V
    -7

So far I'm able to select a <title> and print the <subtitle>s of all <page>s that have said <title>. However, I can't figure out how to do the two-level iteration.

Here is a snippet of my code.

<xsl:template match="/">
<xsl:apply-templates select="//page[./title[not(.=preceding::title)]]"/> 
</xsl:template>

<xsl:template match="page">
<xsl:variable name="fac"><xsl:value-of select=".//title"/></xsl:variable>
 <ul>
    <xsl:for-each select="//page[title = $fac]">
       <li>..print out sub-title...</li>
    </xsl:for-each>
 </ul>
</xsl:template>

Try the below:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:template match="pages">

<xsl:for-each-group select="page" group-by="title">
<ul>
<li><xsl:value-of select="title"/></li>
<xsl:for-each-group select="current-group()" group-by="subtitle">
<ul>
<li><xsl:value-of select="subtitle"/></li>
<ul>
<xsl:for-each select="current-group()">
<li><xsl:value-of select="item"/></li>
</xsl:for-each>
</ul>
</ul>
</xsl:for-each-group>
</ul>
</xsl:for-each-group>

</xsl:template>

</xsl:stylesheet>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.