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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T22:09:29+00:00 2026-06-18T22:09:29+00:00

I have an xml tree with a dynamic root element (not a static name)

  • 0

I have an xml tree with a dynamic root element (not a static name) and some children under that root element. Now I want to add to the children an element at the second position with a xslt script. How can I do it?

Example:
xml:

<root>
  <element1>
    <element1a>
      ..
    </element1a>
  </element1>
  <element2 name="exampleName">This is text.</element2>
</root>

should be converted to

<root>
  <element1>
    <element1a>
      ..
    </element1a>
  </element1>
  <someNewElement>1234</someNewElement>
  <element2 name="exampleName">This is text.</element2>
</root>

What I got so far is the following. But with that solution the node is only added at the first position. I need it at the second position.

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">

<xsl:template match="/">
    <xsl:apply-templates select="/*"/>
</xsl:template>

<xsl:template match="/*">
    <xsl:copy>
        <xsl:element name="newElement">4711</xsl:element>
        <xsl:apply-templates select="node() | @*"/>
    </xsl:copy>
</xsl:template>   

<xsl:template match="node() | @*">
    <xsl:copy>
        <xsl:apply-templates select="node() | @*"/>
    </xsl:copy>        
</xsl:template>

</xsl:stylesheet>
  • 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-18T22:09:31+00:00Added an answer on June 18, 2026 at 10:09 pm
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    
      <xsl:template match="/*/*[1]">
        <xsl:next-match />
        <xsl:element name="newElement">4711</xsl:element>
      </xsl:template>   
    
      <xsl:template match="node() | @*">
        <xsl:copy>
          <xsl:apply-templates select="node() | @*"/>
        </xsl:copy>        
      </xsl:template>
    
    </xsl:stylesheet>
    

    This defines a template that matches the first element child of the document element, does the normal identity template processing (using next-match) and then inserts your new element following it. You could alternatively do

    <xsl:template match="/*/*[2]">
      <xsl:element name="newElement">4711</xsl:element>
      <xsl:next-match />
    </xsl:template>
    

    to match the second child and insert the element before it. The difference between the two is apparent if <root> has only one child element, in which case the /*/*[1] version would insert the newElement but the /*/*[2] would not.

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

Sidebar

Related Questions

I have some XML that has an extra element and I want it gone.
I have an xml document that contains some Item elements with ids. I want
I have XML in the following form that I want to parse with PHP
I have XML that looks like this: <Parameter Name=parameter name Value=parameter value /> which
I have an XML tree like: <abc> <cd>text1</cd> <cd>text2</cd> <ef>text3</ef> <ef>text4</ef> </abc> Now I
I have the following xml and want to insert additional xml into it: <root>
I have many xml file's, I do not want to get the value, but
I have an XML file, that may or may not contain a node called
I have XElement object which is my XML tree read from XML file. Now
I have some classes that already use DOM4J to read XML files and provide

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.