RE: [xsl] equivalent for 'while'

Subject: RE: [xsl] equivalent for 'while'
From: "Ross, Douglas" <DRoss@xxxxxxxxxx>
Date: Wed, 16 Feb 2005 14:24:11 -0500
How do I find the grandchild'th element?

The original XML is like:

<dc id="parent">
	<child ref="child"/>
</dc>
<dc id="child">
	<child ref="grandchild"/>
</dc>
<dc id="grandchild"/>

Could be expressed as:

<dc id="parent">
	<dc id="child">
		<dc id="grandchild"/>
	</dc>
</dc>

In the later XML, I would match /dc/dc/dc to get the grand child. But
how would I match in the former XML?

I wonder if the following psudo-xslt works?

1) Create an index of all the child elements on the ref attribute (using
keys), for example, <xsl:key name="t" match="child" use="@ref" />

2) Create a template to match each dc element. Inside the match
template, use the child node's ref attribute to find the child dc
element in the index. Then get it's child ref id to find the grandchild
dc element in the index. For example,
<xsl:template match="dc">
	<xsl:variable name="child" select="key('t', child/@ref)"/>
	<xsl:variable name="grandchild" select="key('t',
$child/child/@ref)"/>
	<!-- do something with $grandchild -->
</xsl:template>

You could even use a recursive template call to do this to a specific
level of children.

Douglas Ross
Developer, HTML UI Framework
Kronos
E-mail: dross@xxxxxxxxxx
Voice: (978) 947-4305
Fax: (978) 256-2474
www.kronos.com
Smaller, Faster, Sharper, Easier(tm)

-----Original Message-----
From: Richard Lewis [mailto:richardlewis@xxxxxxxxxxxxxx]
Sent: Wednesday, February 16, 2005 12:30 PM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] equivalent for 'while'


On Wed, 16 Feb 2005 17:24:16 +0000, "RQamar" <qamar_rahil@xxxxxxxxxxx>
said:
> David Carlisle wrote:
>
>
> >>>>Is there an equivalent of 'while' loop in XSLT 2.0?
> >>>>
> >>>>
> >>
> >>
> >>
> >>it depends what you mean by equivalence.
> >>
> >>
> >
> >
>
>
> Well I have an XML document which has several <DEFCONCEPT> elements.
> Each of these elements contain certain <CHILD> sub-elements of the
form
> below:
>
> <DEFCONCEPT id="123" name="abc">
>     <CHILD ref="567">abcChild</CHILD>
> </DEFCONCEPT>
>
> <DEFCONCEPT id="567" name="abcChild">
>     <CHILD ref="890">abcGrandChild</CHILD>
> </DEFCONCEPT>
>
> <DEFCONCEPT id="890" name="abcGrandChild"/>
>
> How could I then place conditions when processing this XML doc such
that
> it continues searching for a <CHILD> element until it finds
> 'abcGrandChild' ?
>
> I thought perhaps a single statement could loop until the condition
> becomes 'true' which is obviously not in lines with declarative
> programming. How else is it then possible?
>
Does
<xsl:template match="child[text()='abcGrandChild']">
work?
Richard

Current Thread