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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:00:00+00:00 2026-06-09T14:00:00+00:00

I have the following XML document: <root someAttribute=someValue /> Now I want to add

  • 0

I have the following XML document:

<root someAttribute="someValue" />

Now I want to add a tag using XSLT, so that the document will look like this:

<root someAttribute="someValue">
  <item>TEXT</item>
</root>

If I repeat to use the XSLT once more it should just add another item:

<root someAttribute="someValue">
  <item>TEXT</item>
  <item>TEXT</item>
</root>

It sound’s so easy, does it not? Here is the best I got after trying a ton of things:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" >
    <xsl:param name="message" />

    <xsl:output method="xml" encoding="utf-8"/>

        <xsl:template match="/*">
        <xsl:copy>
            <xsl:copy-of select="*"/>
            <item>
                <xsl:value-of select="$message" />
            </item>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

It does /nearly/ what I have asked for, except that it “forgets” the attributes of the root element. I have found a number of other solutions here on stackoverflow and elsewhere that have in common with my solution that they loose the attributes of the root element. How can I fix that?

  • 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-09T14:00:02+00:00Added an answer on June 9, 2026 at 2:00 pm

    You’re currently transforming only child nodes, not attributes.

    <xsl:template match="root">
        <xsl:copy>
            <xsl:copy-of select="node()|@*"/> <!-- now does attrs too -->
            <item>
                <xsl:value-of select="$message" />
            </item>
        </xsl:copy>
    </xsl:template>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created an xml document and an xslt document and now i want
I have the following XML document that I have to parse using python's minidom:
I have the following XML Document (that can be redesigned if necessary) that stores
Let's say we have the following XML document: <root> <options> ... </options> <children> <child
I have the following (highly simplified) XML document that I am reading into my
I have a xml document on the following format and want to transform it
I have the following code, that inserts the processing instructions before root element: Document
Currently, I am constructing an XML document in PHP that will yield the following:
I have the following xml <root> <document> <account>12</account> <type name-a>stuff</type> <type name-b>stuff</type> </document> <document>
Consider we have the following xml document <?xml version=1.0 encoding=utf-8?> <Root> <Child /> </Root>

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.