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

The Archive Base Latest Questions

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

I have an input xml as follows, <?xml version=1.0 encoding=utf-8 ?> <?xml-stylesheet type=text/xsl href=cdcatalog.xsl?>

  • 0

I have an input xml as follows,

<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?>
<root>
  <employee>
    <firstname>Kaushal</firstname>
    <lastname>Parik</lastname>
  </employee>
  <employee>
    <firstname>Abhishek</firstname>
    <lastname>Swarnkar</lastname>
  </employee>
</root>

and I need output xml as

<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?>
<root>
  <employee>
    <firstname>Kaushal</firstname>
    <lastname>Parik</lastname>
    <status>Single</status>
  </employee>
  <employee>
    <firstname>Abhishek</firstname>
    <lastname>Swarnkar</lastname>
    <status>Single</status>
  </employee>
</root>

The value of “status” is “Single” in all the nodes…. I know how to add this static text “Single” through c# code…. But, I don’t know how to add the node “status” in xml through xslt…. When I try, it gets added below the node “firstname” and not in the expected place as shown…. Please help me how can i achieve this…. The xslt and C# code used by me are,

XSLT:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
            xmlns:myUtils="pda:MyUtils">
  <xsl:output method="xml" indent="yes"/>
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="employee/firstname">
    <xsl:element name="firstname">
      <xsl:value-of select="myUtils:FormatName(.)" />
    </xsl:element>
    <xsl:element name ="status">
      <xsl:value-of select ="Single"/>
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>

aspx.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Xml.Xsl;
using System.Xml.XPath;
using System.IO;

public partial class nirav : System.Web.UI.Page
{
    public class MyXslExtension
    {
        public string FormatName(string name)
        {
            return "Mr. " + name;
        }
        public int GetAge(string name)
        {
            int age = name.Count();
            return age;
        }
    }  
    protected void Page_Load(object sender, EventArgs e)
    {
        string outputpath = "nirav.xml";
        XsltArgumentList arguments = new XsltArgumentList();
        arguments.AddExtensionObject("pda:MyUtils", new MyXslExtension());
        using (StreamWriter writer = new StreamWriter(outputpath))
        {
            XslCompiledTransform transform = new XslCompiledTransform();
            transform.Load("http://localhost:4329/XsltTransform/nirav.xslt");
            transform.Transform("http://localhost:4329/XsltTransform/nirav.xml", arguments, writer);
        }

    }
}

Your help is greatly appreciated….

  • 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-09T04:35:04+00:00Added an answer on June 9, 2026 at 4:35 am

    There are a couple of issues with your XSLT. Firstly, this expression is incorrect

    <xsl:value-of select="Single"/>
    

    This will select the value of the element Single, which does not exist in your input XML. You actually want to output the literal value ‘Single’

    <xsl:value-of select="'Single'"/>
    

    Or rather, you could just output the whole element ‘as-is’

    <status>Single</status>
    

    Secondly, it looks like you want to want to add the status as the last element of the employee element. In which case, you need a template to match the employee element, which copies all existing elements, and then just adds the new status element

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

    Here is the full XSLT

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
       <xsl:output method="xml" indent="yes"/>
    
       <xsl:template match="@*|node()">
          <xsl:copy>
             <xsl:apply-templates select="@*|node()"/>
          </xsl:copy>
       </xsl:template>
    
       <xsl:template match="employee">
          <xsl:copy>
             <xsl:apply-templates select="@*|node()"/>
             <status>Single</status>
          </xsl:copy>
       </xsl:template>
    </xsl:stylesheet>
    

    When applied to your XML, the following is output

    <root>
       <employee>
          <firstname>Kaushal</firstname>
          <lastname>Parik</lastname>
          <status>Single</status>
       </employee>
       <employee>
          <firstname>Abhishek</firstname>
          <lastname>Swarnkar</lastname>
          <status>Single</status>
       </employee>
    </root>
    

    (Note, I’ve removed the reference to the extension functions, because I don’t have those myself on my PC).

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

Sidebar

Related Questions

I have a input file Main.xml <?xml version=1.0 encoding=utf-8 ?> <Employees> <Employee> <id name=id>1</id>
I have the following input XML: <root age=1> <description>some text</description> <section> <item name=a> <uuid>1</uuid>
I have input text: <div><label wicket:for=name>Name</label><input type=text wicket:id=name /></div> Now I need to add
I have xml input which looks like (simplified version used for example): <Student> <Subject>
in input XML I have a tag <name>Sample '</name> in XSL I transform this
My input.xml is as follows: <root> <Property> <info> <Name>A</Name> <Value>1000</Value> </info> <info> <Name>B</Name> <Value>2000</Value>
My input XML consists of the following, <root> <entry> <type>U</type> <value>111</value> </entry> <entry> <type>X</type>
I have an input xml as below <maindocument> <first> <testing>random text</testing> <checking>random test</checking> </first>
i have input xml as <content> <date> <day>14</day> <month>06</month> <year>2012</year> </date> </content> want it
This is my input xml. The xml can have maximunm three nodes and minimum

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.