not sure if this is possible without having to go through several passes, but I'll ask anyway (my XSL is a little rusty)
I have an XML document, which contains nodes as follows:
<structures>
<structure id="STRUCT_A">
<field idref="STRUCT_B" name="b"/>
<field idref="STRUCT_C" name="c"/>
<field idref="FIELD_D" name="d"/>
</structure>
<structure id="STRUCT_B">
<field idref="STRUCT_C" name="c"/>
<field idref="FIELD_E" name="e"/>
</structure>
<structure id="STRUCT_C">
<field idref="FIELD_E" name="e"/>
<field idref="FIELD_F" name="f"/>
<field idref="FIELD_G" name="g"/>
</structure>
</structures>
(The real file contains lots of structure tags which interdependencies, none of which are circular!)
What I want to do is to generate some text (in this case C++ struct
s), and the obvious requirement is the order of the struct
s, so my ideal output would be
struct STRUCT_C
{
FIELD_E e;
FIELD_F f;
FIELD_G g;
};
struct STRUCT_B
{
STRUCT_C c;
FIELD_E e;
};
struct STRUCT_A
{
STRUCT_B b;
STRUCT_C c;
FIELD_D d;
};
I know I could use forward declarations and that would mean that the order doesn't matter, however the problem is that there will be "processing" code inline in the structures, and they would require the real definition to be present.
So far I can detect to see if a structure
has any dependencies, with the following bit of xsl:
<xsl:for-each select="descendant::*/@idref">
<xsl:variable name="name" select="."/>
<xsl:apply-templates select="//structure[@id = $name]" mode="struct.dep"/>
</xsl:for-each>
(this happens inside a <xsl:template match="structure">
)
Now, theoretically, I could then follow this dependency "chain" and generate the struct
s for each entry first, then the one that I am currently at, however as you can imagine, this generates lot's of copies of the same structure - which is a pain..
Is there anyway to avoid the copies? Basically, once a structure has been visited, and if we visit again, not to bother outputting the code for it... I don't need the full xslt to do this (unless it's trivial!), but just any ideas on approaches...
If there isn't, I could in theory wrap the struct
with a #ifdef
/#define
/#endif
guard so that the compiler only uses the first definition, however this is REALLY NASTY! :(
(NOTES: xslt 1.0, xsltproc on linux: Using libxml 20623, libxslt 10115 and libexslt 812)
This transformation:
when applied on the provided XML document:
produces the wanted, correct result:
Explanation:
structure
elements are processed strictly one by one. At any time we process the firststructure
element whoseid
isn't yet registered in thepVisited
parameter and that has nofield/@idref
value that isn't already in thepVisited
parameter and refers to an existingstructure
element.Ooh, this is more complicated than it looked at first. +1 for good question.
I think the best way to accomplish this in XSLT 1.0 would be to pass an accumulating parameter whenever you apply-templates to a structure. The parameter (call it "$visited-structures") is a space-delimited list of names of structures you've already processed.
Update: finally got this. :-)
In the template for processing a structure, check whether any other structures this one depends on are not already listed in $visited-structures. If not, generate the code for this structure, and recurse on the template selecting the next non-visited structure, appending the current structure name to the $visited-structures parameter. Otherwise, don't generate code for the structure but recurse on the template selecting the first dependency structure, passing the $visited-structures parameter unmodified.
Here is the code...
And the output:
Just for fun, other approach (level by level) and ussing keys:
Output: