Re: [xsl] Processing Nested Lists

Subject: Re: [xsl] Processing Nested Lists
From: Adam Retter <adam.retter@xxxxxxxxxxxx>
Date: Mon, 26 Feb 2007 16:31:53 +0000
I have done something like this with the IPSV taxonomy using XQuery, I
suspect that the general premise holds though.

Basically you need a recursive function that checks for the existence of
a child node, if the child node exists then the function should call
itself passing in that child node as the argument to operate on, and so
on...

In XSLT, you would use a template that call's itself

In XQuery this looks like this (included for illustrative purposes only)
-

declare function local:processNode($node as node()) as node()*
{
	<tr>
		<td>{node-name($node)}</td>
	</tr>,

	(: process children :)
	for $child in $node/child::node()
		return local:processNode($child)
};

for $node in /TopTag/TaxonomyEntries/TaxonomyNode return
	local:processNode($node)


So XSLT would be something like this perhaps (untested, and written off
the top of my head, so the syntax may need tweaking) -

<xsl:template name="processNode">
	<xsl:parameter name="node"/>
	<tr>
		<td><xsl:value-of select="{node-name($node)}"/></td>
	</tr>
	
	<xsl:for-each select="$node/child::node">
		<xsl:call-template name="processNode">
			<xsl:with-parameter name="node" select="."/>
		</xsl:call-template>
	</xsl:for-each>
</xsl:template>

<xsl:for-each select="/TopTag/TaxonomyEntries/TaxonomyNode">
	<xsl:call-template name="processNode">
		<xsl:with-parameter name="node" select="."/>
	</xsl:call-template>
</xsl:for-each>

On Mon, 2007-02-26 at 16:13 +0000, Andy Carr1 wrote:
> Hi
> 
> Does anybody know how do you process nodes within a tag that are nested to 
> an undefined depth.
> 
> I have a tag structure that looks like this:
> 
> <TaxonomyEntries>
> - - -<TaxonomyNode> (1 to infinity)
> - - - - - -<Description />
> - - - - - -<TaxonomyNode /> (0 to infinity)
> - - -<TaxonomyNode>
> <TaxonomyEntries>
> 
> And I have some XML that looks like this:
> 
> <TopTag>
>    <TaxonomyEntries>
>       <TaxonomyNode name="Arbitrary Name">          -----Top Level Node
>          <Description>any description</Description>
>          <TaxonomyNode name="another name">
>             <TaxonomyNode name="yet another name">
>                <Description>any description</Description>
>                <TaxonomyNode name="and yet another name">
>                   <Description>any description</Description>
>                </TaxonomyNode>
>             </TaxonomyNode>
>          </TaxonomyNode>
>       </TaxonomyNode>
>       <TaxonomyNode name="Arbitrary Name">          -----Top Level Node
>          <Description>any description</Description>
>          <TaxonomyNode name="another name">
>             <Description>any description</Description>
>          </TaxonomyNode>
>       </TaxonomyNode>
>    </TaxonomyEntries>
> </TopTag>
> 
> I need to apply a stylesheet (XSL v2.0) to this XML to create a Table in 
> Word so that each Top Level TaxonomyNode is in a separate Table Row and 
> the children and childrens-children are all nested accordingly with their 
> descriptions displayed.
> 
> <w:tbl>
>    <w:tblPr>
>    <w:tblGrid>
>    <w:tr>
>    <TaxonomyEntries>
>       <w:tr>
>          <w:tc>
>             <w:p>
>                <TaxonomyNode name="Arbitrary Name">
>                   <Description>
>                      <w:r>
>                         <w:t>any description</w:t>
>                      </w:r>
>                   </Description>
>                   <TaxonomyNode name="another name">
>                      <Description>
>                         <w:r>
>                            <w:t>any description</w:t>
>                         </w:r>
>                      </Description>
>                      <TaxonomyNode name="yet another name">
>                         <Description>
>                            <w:r>
>                               <w:t>any description</w:t>
>                            </w:r>
>                         </Description>
>                         <TaxonomyNode name="and yet another name">
>                            <Description>
>                               <w:r>
>                                  <w:t>any description</w:t>
>                               </w:r>
>                            </Description>
>                         </TaxonomyNode>
>                      </TaxonomyNode>
>                   </TaxonomyNode>
>                </TaxonomyNode>
>             </w:p>
>          </w:tc>
>       </w:tr>
>       <w:tr>
>          <w:tc>
>             <w:p>
>                <TaxonomyNode name="Arbitrary Name">
>                   <Description>
>                      <w:r>
>                         <w:t>any description</w:t>
>                      </w:r>
>                   </Description>
>                   <TaxonomyNode name="another name">
>                      <Description>
>                         <w:r>
>                            <w:t>any description</w:t>
>                         </w:r>
>                      </Description>
>                   </TaxonomyNode>
>                </TaxonomyNode>
>             </w:p>
>          </w:tc>
>       </w:tr>
>    </TaxonomyEntries>
> <w:tbl>
> 
> Could any one give me some pointers on how to achieve this nesting. It 
> seems to me that some sort of recursion is in order but I'm not sure how 
> to implement it when there is no fixed depth to the nesting i.e there is 
> no break-out parameter.
> 
> Hopefully
> Andy
> 
> 
> 
> 
> 
> 
> 
> Unless stated otherwise above:
> IBM United Kingdom Limited - Registered in England and Wales with number 
> 741598. 
> Registered office: PO Box 41, North Harbour, Portsmouth, Hampshire PO6 3AU
> 
-- 
Adam Retter

Principal Developer
Devon Portal Project
Room 310
County Hall
Topsham Road
Exeter
EX2 4QD

t: 01392 38 3683
f: 01392 38 2966
e: adam.retter@xxxxxxxxxxxx
w: www.devonline.gov.uk

Current Thread