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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:22:39+00:00 2026-06-10T09:22:39+00:00

I have a XML that looks like this (arbitrary XML input) <NodeLevel1> <NodeLevel2> <NodeLevel3>

  • 0

I have a XML that looks like this (arbitrary XML input)

<NodeLevel1>
    <NodeLevel2>
        <NodeLevel3>
            Sample text 1
        </NodeLevel3>
    </NodeLevel2>

    <NodeLevel2>
        .
        .
        .
        ... <NodeLevelN>
                Some deep thought
            </NodeLevelN>
    </NodeLevel2>

    <NodeLevel2>
        Sample text 2
    </NodeLevel2>
    Sample text 3
</NodeLevel1>

What I would like to have after XSLT transformation is 4 new XMLs containing just a single text node like the following 4 XMLs:

1.

    <NodeLevel1>
        <NodeLevel2>
            <NodeLevel3>
                Sample text 1
            </NodeLevel3>
        </NodeLevel2>
    </NodeLevel1>

2.

    <NodeLevel1>    
        <NodeLevel2>
            .
            .
            .
            ... <NodeLevelN>
                    Some deep thought
                </NodeLevelN>
        </NodeLevel2>
    </NodeLevel1>

3.

    <NodeLevel1>
        <NodeLevel2>
            Sample text 2
        </NodeLevel2>
    </NodeLevel1>

4.

    <NodeLevel1>
        Sample text 3
    </NodeLevel1>

What is the best way to do that? I’m using C++ (I can use libxml2 and libxslt)
Thanks!

  • 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-06-10T09:22:41+00:00Added an answer on June 10, 2026 at 9:22 am

    I would use XSLT 2.0 with xsl:result-document or in your case with libxslt you can also use the exsl:document extension element:

    <xsl:stylesheet
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      version="1.0"
      xmlns:exsl="http://exslt.org/common"
      extension-element-prefixes="exsl"
      exclude-result-prefixes="exsl">
    
    <xsl:template match="/">
      <xsl:apply-templates select="NodeLevel1/node()[self::* or self::text()[normalize-space()]]" mode="new-doc"/>
    </xsl:template>
    
    <xsl:template match="NodeLevel1/node()" mode="new-doc">
      <exsl:document href="result{position()}.xml">
        <NodeLevel1>
          <xsl:copy-of select="."/>
        </NodeLevel1>
      </exsl:document>
    </xsl:template>
    
    </xsl:stylesheet>
    

    Thinking about the problem a bit more I have improved the stylesheet to work with any kind of root element:

    <xsl:stylesheet
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      version="1.0"
      xmlns:exsl="http://exslt.org/common"
      extension-element-prefixes="exsl"
      exclude-result-prefixes="exsl">
    
    <xsl:template match="/">
      <xsl:apply-templates select="*/node()[self::* or self::text()[normalize-space()]]" mode="new-doc"/>
    </xsl:template>
    
    <xsl:template match="/*/node()" mode="new-doc">
      <exsl:document href="result{position()}.xml">
        <xsl:element name="{name(..)}" namespace="{namespace-uri(..)}">
          <xsl:copy-of select="."/>
        </xsl:element>
      </exsl:document>
    </xsl:template>
    
    </xsl:stylesheet>
    

    [edit]
    If you want to use an XSLT 2.0 processor like Saxon or AltovaXML or XmlPrime here is an XSLT 2.0 stylesheet:

    <xsl:stylesheet
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      version="2.0">
    
    <xsl:template match="/">
      <xsl:apply-templates select="*/(* | text()[normalize-space()])" mode="new-doc"/>
    </xsl:template>
    
    <xsl:template match="/*/node()" mode="new-doc">
      <xsl:result-document href="result{position()}.xml">
        <xsl:element name="{name(..)}" namespace="{namespace-uri(..)}">
          <xsl:copy-of select="."/>
        </xsl:element>
      </xsl:result-document>
    </xsl:template>
    
    </xsl:stylesheet>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an XML file that looks like this: <SiteMenuItems> <SiteMenuItem text=Home navigateurl=/Default.aspx tooltip=Return
I have some data in an XML element that looks like this: <?xml version=1.0
I have some xml that looks like this... <tbody> <tr> <td> <h5> <a class=foo
I have some xml that looks like this: <?xml version=1.0?> <data> <items> <item><timestamp>2011-07-11T09:01:42Z</timestamp><title><![CDATA[ some
I have some xml that looks like this: <rootElement attribute=' > '/> This is
I have a xml that looks like this, <Parent> Running text with marked up
I have some xml that looks like this: <xml><name>oscar</name><race>puppet</race><class>grouch</class></xml> The tags change and are
Grettings! I have some XML that looks like this: <Root> <SectionA> <Item id=111> <Options>
I have some XML that looks like this: <Body> <RESULT> <SUCCESS>true</SUCCESS> <SESSIONID>42</SESSIONID> <SESSION_ENCODING>;jsessionid=42</SESSION_ENCODING> </RESULT>
I have some XML that looks like this: <abc x={></abc> I want to force

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.