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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:21:16+00:00 2026-05-31T15:21:16+00:00

I am trying to remove nodes from an XML file. Using just one XSLT

  • 0

I am trying to remove nodes from an XML file. Using just one XSLT for each of my XML I need to make decisions in the XSLT based upon the number of children of the document element.

<root>
  <branch>
    <foo>bar</foo>
  </branch>
<root>

should transform into

  <branch>
  </branch>

but

<root>
  <branch>
    <foo>bar</foo>
  </branch>
  <branch>
    <foo>baz</foo>
<root>

into

<root>
  <branch>
  </branch>
  <branch>
  </branch>
<root>

That is, the root element should be removed if its (only) child can act as the new document root of the result XML after applying XSLT. The <foo> nodes have to be removed in every occurence.

Is there a way to perform this operation with a single XSL?

  • 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-31T15:21:17+00:00Added an answer on May 31, 2026 at 3:21 pm

    Try

    <xsl:template match="@* | node()">
      <xsl:copy>
        <xsl:apply-templates select="@* | node()"/>
      </xsl:copy>
    </xsl:template>
    
    <xsl:template match="root[*[2]]">
      <xsl:copy>
        <xsl:apply-templates/>
      </xsl:copy>
    </xsl:template>
    
    <xsl:template match="root[* and not(*[2])]">
      <xsl:apply-templates/>
    </xsl:template>
    
    <xsl:template match="branch/foo"/>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to remove the attribute xmlns=http://webdev2003.test.com from the following xml using xsl/xslt,
I'm trying to remove SOAP and ns2 nodes from this XML : <SOAP-ENV:Envelope xmlns:SOAP-ENV=http://schemas.xmlsoap.org/soap/envelope/
I have an XML that I need remove empty elements from. I am trying
i'm trying to remove a child node from an xml. my script is working..but
I've been trying to read a node in an xml file generated from DSLR
I'm trying to remove individual nodes from their parent, I tried the Remove method
There I am trying to load XML content from a remote host using Node.js.
I'm trying to remove a specific node from a XmlNodeList named listaWidths. This specific
I was trying to remove all the lines of a file except the last
I'm trying to remove some items of a dict based on their key, here

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.