I'll assume a wrapper element x so your input is well formed
<x>
<elem id="a1" type="w">
<text>aa</text>
</elem>
<elem id="a2" type="x">
<text>bb</text>
<ref>a1</ref>
</elem>
<elem id="a3" type="y">
<text>cc</text>
<ref>a2</ref>
</elem>
<elem id="a4" type="x">
<text>dd</text>
<ref>a1</ref>
</elem>
</x>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text"/>
<xsl:key name="ref" match="elem" use="ref"/>
<xsl:template match="x">
<xsl:apply-templates select="elem[(_at_)type='w']"/>
</xsl:template>
<xsl:template match="elem[(_at_)type='w']">
process node <xsl:value-of select="@id"/> with text <xsl:value-of
select="text"/>
<xsl:apply-templates select="key('ref',@id)[(_at_)type='x']"/>
</xsl:template>
<xsl:template match="elem[(_at_)type='x']">
process node <xsl:value-of select="@id"/> with text <xsl:value-of
select="text"/>
<xsl:apply-templates select="key('ref',@id)[(_at_)type='y']"/>
</xsl:template>
<xsl:template match="elem[(_at_)type='y']">
process node <xsl:value-of select="@id"/> with text <xsl:value-of
select="text"/>
<xsl:apply-templates select="key('ref',@id)[(_at_)type='z']"/>
</xsl:template>
<xsl:template match="elem[(_at_)type='z']">
none of these should be there
</xsl:template>
</xsl:stylesheet>
$ saxon tree.xml tree.xsl
process node a1 with text aa
process node a2 with text bb
process node a3 with text cc
process node a4 with text dd
Note the above stylesheet is enforcing that it only looks for elements
of a specified type at each level. If you know that only type y eleemnts
are going to refer to type x elements then you could compress all these
templates down to just one.
David
--
http://www.dcarlisle.demon.co.uk/matthew
________________________________________________________________________
This e-mail has been scanned for all viruses by Star Internet. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
________________________________________________________________________
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list