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 6110553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:31:45+00:00 2026-05-23T14:31:45+00:00

I am trying to process an xml file which has several different state groups

  • 0

I am trying to process an xml file which has several different state groups like

<root>
<childgroup>16</childgroup>
<setstate>init</setstate>
<child1>...</child1>
<child2>...</child2>
<setstate>process<setstate>
<child2>...</child2>
<child3>...</child3>
.....
<childgroup>17</childgroup>
...

What I need is actually get something like

<childgroup no="16">
  <state statename="init">
    <child1>...</child1>
    <child2>...</child2>
  </state>
  <state statename="process">
    <child2>...</child2>
    <child3>...</child3>
  </state>
</childgroup>
<childgroup no="17">
...

I’ve done simple part which is going and adding “chgrpno” attribute and stateid attribute to all childs (it makes copy-of of all elements but childgroup and state, adding the attribute to those two.

<xsl:template match="/">
  <xsl:apply-templates mode="numb"/>
</xsl:template>

This works and in the result all childs have attribute so I could regroup them in the next pass and states have numbers so I could later make same thing. But trying to follow the example of M.Kay with “temporary documents” when I try to do

<xsl:variable name="nmb">
  <xsl:apply-templates mode="numb"/>
</xsl:variable>

<xsl:template match="/">
  <xsl:copy-of select="$nmb"/>
</xsl:template>

then it just returns the original to me, and all changes which I made in the first pass are gone. So what am I doing wrong here?

I use XSLT 1.0, not XSLT 2.0 explicitly.

(edit: surely I named the variable, forgot to copy it here).

  • 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-23T14:31:45+00:00Added an answer on May 23, 2026 at 2:31 pm

    Here is an example how to approach the grouping with XSLT 1.0 in one step; the stylesheet

    <xsl:stylesheet
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      version="1.0">
    
      <xsl:output indent="yes"/>
    
      <xsl:key name="k1" match="root/*[not(self::childgroup)]" 
        use="generate-id(preceding-sibling::childgroup[1])"/>
    
      <xsl:key name="k2" match="root/*[not(self::childgroup) and not(self::setstate)]"
        use="concat(generate-id(preceding-sibling::childgroup[1]), '|', generate-id(preceding-sibling::setstate[1]))"/>
    
      <xsl:template match="root">
        <xsl:copy>
          <xsl:apply-templates select="childgroup"/>
        </xsl:copy>
      </xsl:template>
    
      <xsl:template match="childgroup">
        <childgroup no="{.}">
          <xsl:apply-templates select="key('k1', generate-id())[self::setstate]"/>
        </childgroup>
      </xsl:template>
    
      <xsl:template match="setstate">
        <state statename="{.}">
          <xsl:copy-of select="key('k2', concat(generate-id(preceding-sibling::childgroup[1]), '|', generate-id()))"/>
        </state>
      </xsl:template>
    
    </xsl:stylesheet>
    

    transforms the input sample

    <root>
      <childgroup>16</childgroup>
      <setstate>init</setstate>
      <child1>...</child1>
      <child2>...</child2>
      <setstate>process</setstate>
      <child2>...</child2>
      <child3>...</child3>
      <childgroup>17</childgroup>
      <setstate>init</setstate>
      <child1>...</child1>
      <child2>...</child2>
      <setstate>process</setstate>
      <child2>...</child2>
      <child3>...</child3>
    </root>
    

    into

    <root>
       <childgroup no="16">
          <state statename="init">
             <child1>...</child1>
             <child2>...</child2>
          </state>
          <state statename="process">
             <child2>...</child2>
             <child3>...</child3>
          </state>
       </childgroup>
       <childgroup no="17">
          <state statename="init">
             <child1>...</child1>
             <child2>...</child2>
          </state>
          <state statename="process">
             <child2>...</child2>
             <child3>...</child3>
          </state>
       </childgroup>
    </root>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use Apache Digester (V3) to process an XML file which contains
I have an xml file which I am trying to process with Java. It
I am trying to process a CSV file in which some of the fields
I'm trying to post-process the documentation XML file as a post-build XSLT transformation (using
I am trying to figure out how to process XML in java script So
I am trying to process an uploaded file in a Perl program, using CGI::Application.
i'm trying to load some elements from a xml file. but it XDocument.Load seems
I'm trying to parse a moderately large XML file (6mb) in php using simpleXML.
This is what I am trying to achieve: I have a CSV file which
I need to create a webpage where the user submits an XML file, which

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.