Subject: Re: Problem with constructing a tree
From: "J.Pietschmann" <j3322ptm@xxxxxxxx>
Date: Sat, 27 Jul 2002 10:04:41 +0200
|
Yash wrote:
Hi,
I have a problem where I need to construct a tree from an xml file:
The xml file looks like this:
<feeAgreement>
<feeClauses clauseid="1" rootclause="false">
<computationUnit name="xxxx">
<complexComputationUnit clauseid="2"/>
</computationUnit>
</feeClauses>
<feeClauses clauseid="2" rootclause="false">
<computationUnit name="yyyy">
<complexComputationUnit clauseid="3"/>
</computationUnit>
</feeClauses>
<feeClauses clauseid="3" rootclause="false">
<computationUnit name="zzzz">
<complexComputationUnit clauseid="1"/>
</computationUnit>
</feeClauses>
</feeAgreement>
Now, I would like to typically use recursion to construct the tree, starting
at the first feeClauses element. When I get to the complexComputationUnit
element's clauseid attribute, I would like to go over and find the clause
with the clauseid specified here.
The problem is, I need to know if the clause is already present, as in the
case of clause 3, which refers to clause 1. Here, I need not get the first
clause.
I cannot seem to think of a way of checking while transforming whether I
have the clause already in the transformed part.
Any help would be greatly appreciated.
You have a cycle in your dependencies. You can check for this by
keeping the elements you already visited in a parameter
<xsl:template name="do-tree">
<xsl:param name="node"/>
<!-- set of visited nodes -->
<xsl:param name="visited"/>
<!-- process the currenttly visited node -->
<xsl:copy-of select="$node"/>
<!-- get referenced node -->
<xsl:variable name="referenced" select="//feeClauses[
@clauseid=$node/cumputationUnit/complexComputatioUnit/@clauseid]"/>
<!-- check whether the referenced node has been visited -->
<xsl:if test="count($referenced|$visited)!=count($visited)">
<xsl:call-template name="do-tree">
<xsl:with-param name="node" select="$referenced"/>
<!-- don't forget to add the referenced node to the visited list -->
<xsl:with-param name="visited" select="$referenced|$visited"/>
</xsl:call-template>
</xsl:if>
<xsl:template>
J.Pietschmann
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
|