Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3426412
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:42:46+00:00 2026-05-18T06:42:46+00:00

not sure if this is possible without having to go through several passes, but

  • 0

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++ structs), and the obvious requirement is the order of the structs, 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 structs 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)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T06:42:46+00:00Added an answer on May 18, 2026 at 6:42 am

    This transformation:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
    
     <xsl:variable name="vLeafs" select="/*/structure[not(field/@idref = /*/structure/@id)]"/>
    
     <xsl:template match="/*">
      <xsl:apply-templates select="$vLeafs[1]">
       <xsl:with-param name="pVisited" select="'|'"/>
      </xsl:apply-templates>
    
     </xsl:template>
    
     <xsl:template match="structure">
       <xsl:param name="pVisited"/>
    
    struct <xsl:value-of select="@id"/>
    {<xsl:text/>
      <xsl:apply-templates/>
    };
      <xsl:variable name="vnewVisited"
           select="concat($pVisited, @id, '|')"/>
      <xsl:apply-templates select=
      "../structure[not(contains($vnewVisited, concat('|', @id, '|')))
                  and
                    not(field/@idref
                               [not(contains($vnewVisited, concat('|', ., '|')) )
                              and
                               . = ../../../structure/@id
                               ]
                       )
                   ] [1]
      ">
       <xsl:with-param name="pVisited" select="$vnewVisited"/>
      </xsl:apply-templates>
     </xsl:template>
    
     <xsl:template match="field">
      <xsl:value-of select="concat('&#xA;   ', @idref, ' ', @name, ';')"/>
     </xsl:template>
    </xsl:stylesheet>
    

    when applied on the provided XML document:

    <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>
    

    produces the wanted, correct result:

    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;
    };
    

    Explanation:
    structure elements are processed strictly one by one. At any time we process the first structure element whose id isn’t yet registered in the pVisited parameter and that has no field/@idref value that isn’t already in the pVisited parameter and refers to an existing structure element.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several USB mass storage flash drives connected to a Ubuntu Linux computer
I make a distributed embedded application that will make use of several micro-controllers. The
I would like to get a sum from a column, with and without a
My question is about memory use and objects in actionscript 2. If I have
I am using a 3rd-party rotator object, which is providing a smooth, random rotation
I have a login.jsp page which contains a login form. Once logged in the
I am learning on my own about writing an interpreter for a programming language,
Is there a way to test if a collection is already initialized? try-catch only?
I would like to update my SQL lite database with the native update-method of
We manage a site for a medical charity. They have a number of links

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.