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

  • SEARCH
  • Home
  • 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 6600895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:40:59+00:00 2026-05-25T18:40:59+00:00

I have this problem for xslt: This is the input file: <root> <header/> <item/>

  • 0

I have this problem for xslt:

This is the input file:

  <root>
   <header/>
   <item/>
   … other n-1 <item/>
   <header/>
   <item/>
   … other m-1 <item/>
  </root>

so header and item are at the same level (/root).
it must be converted in something like:

<root2>
  <header2>
     <item2/>
     …<item2/> // the first n-items up
  </header2>
  <header2>
     <item2/>
     …<item2/> // the last m-items up
  </header2>
</root2>

so basically the first n-item must be moved in the first header while the second group of items must be moved in the second header. Any idea how to get this?

Thanks

Randomize

  • 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-25T18:41:00+00:00Added an answer on May 25, 2026 at 6:41 pm

    Sample XML:

    <root>
        <header/>
        <item>1</item>
        <item>2</item>
        <item>3</item>
        <header/>
        <item>5</item>
        <item>6</item>
        <item>7</item>
    </root>
    

    XSLT using grouping:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output omit-xml-declaration="yes" indent="yes"/>
    
        <xsl:key name="k" match="item" use="count(preceding-sibling::header)"/>
    
        <xsl:template match="/">
            <root2>
                <xsl:apply-templates select="root/item[generate-id(.) = generate-id(key('k', count(preceding-sibling::header)))]" mode="a"/>
            </root2>
        </xsl:template>
    
        <xsl:template match="item" mode="a">
            <header2>
                <xsl:apply-templates select="key('k', count(preceding-sibling::header))"/>
            </header2>
        </xsl:template>
    
        <xsl:template match="@* | node()">
            <xsl:copy>
                <xsl:apply-templates select="@* | node()"/>
            </xsl:copy>
        </xsl:template>
    
    </xsl:stylesheet>
    

    Or simple particular XSLT:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output omit-xml-declaration="yes" indent="yes"/>
    
        <xsl:template match="/">
            <root2>
                <header2>
                    <xsl:apply-templates select="root/item[count(preceding-sibling::header) = 1]"/>
                </header2>
                <header2>
                    <xsl:apply-templates select="root/item[count(preceding-sibling::header) = 2]"/>
                </header2>
            </root2>
        </xsl:template>
    
        <xsl:template match="@* | node()">
            <xsl:copy>
                <xsl:apply-templates select="@* | node()"/>
            </xsl:copy>
        </xsl:template>
    
    </xsl:stylesheet>
    

    Both produce the same output:

    <root2>
      <header2>
        <item>1</item>
        <item>2</item>
        <item>3</item>
      </header2>
      <header2>
        <item>5</item>
        <item>6</item>
        <item>7</item>
      </header2>
    </root2>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an xslt file. In this file, I have an xsl choose statement.
I'm new to xslt, and I fear that this problem hinges on me getting
I'm having a problem with including xslt templates. I have a.xslt which includes b.xslt
The problem is this: I have an XML fragment like so: <fragment>text1 <a>inner1 </a>text2
I have two XSLT file A .xslt and B .xslt, where A .xslt includes
I have an XML-like file: <p>something</p> <ac:image> <ri:attachment ri:filename=IMAGE.PNG /> </ac:image> <ac:macro ac:name=screenshot> <ac:default-parameter>IMAGE.ss</ac:default-parameter>
so I have this assignment and they are asking for data given by Xslt
I have the following problem with XSLT. In an XML document I have several
I have this xml: <Documents> <Header Document_No=Numb1 Warehouse=WH2> <Body Position=1 Quantity=11 /> <Body Position=2
I've been trying to figure out this XSLT/XML problem for the past few days

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.