xsl-list
[Top] [All Lists]

RE: data structure in xslt?

2002-10-07 14:02:03
[ Zhiyong Hong]

I am using xslt for code generation from xml document. I need 
to maintain a data structure to remember a list of special 
nodes I have visited before. Is there any way in xslt which 
can address this issue?


The best way to address it is to re-think what you want to achieve (see
below).

For example, I have following xml document:

<method name="getPerson">
<returnType>Person</returnType>
<params/>

<method name="setPerson">
<returnType/>
<params>
<param type="Person" name="p"/>
</params>

When I first get the node of Person type, I know I need to 
include Person.h or whatever interface it is. But after that 
I don't need to do that again. So I need to remember the type 
I already include, and I think I need a datastructure(a hash, 
list or tree will do).

With xslt transformations, it is better to think out what you want to
achieve first. Your description above is not really ***what** you want
to accomplish.    What you really want to do here is something like
this:

        If my sourcedocument has at least one of 
        ( "getPerson" or  "setPerson") method, I
        want to put a Person.h statement at the top of the output.

With this this as the goal, you could write something like this:

<xsl:variable name='insert-person-header'
        select='count(//method[(_at_)getPerson]) +
count(//method[(_at_)setPerson]) > 0'/>
<xsl:if test='$insert-person-header'
#include &lt;Person.h>
</xsl:if>

You could invoke this from a template that processes the root of the xml
document, so that it would be invoked before the templates for the
methods.

In the templates that process your methods, you will not output anything
about Person.h, since that would have been output (or not) earlier in
the output.

There are many variations and possibilities for optimization, but this
may well be all you need.  

Cheers,

Tom P

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



<Prev in Thread] Current Thread [Next in Thread>