30

Below is the actual xml:

<?xml version="1.0" encoding="utf-8"?>
<employee>
 <Name>ABC</Name>
 <Dept>CS</Dept>
 <Designation>sse</Designation>
</employee>

And i want the output as below:

<?xml version="1.0" encoding="utf-8"?>
<employee>
 <Name>ABC</Name>
  <Age>34</Age>
 <Dept>CS</Dept>
  <Domain>Insurance</Domain>
 <Designation>sse</Designation>
</employee>

Is this possible to add XML element in between using xslt? Please give me sample!

2 Answers 2

47

Here is an XSLT 1.0 stylesheet that will do what you asked:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   <!-- Identity transform -->
   <xsl:template match="@* | node()">
      <xsl:copy>
         <xsl:apply-templates select="@* | node()"/>
      </xsl:copy>
   </xsl:template>

   <xsl:template match="Name">
      <xsl:copy-of select="."/>
      <Age>34</Age>
   </xsl:template>

   <xsl:template match="Dept">
      <xsl:copy-of select="."/>
      <Domain>Insurance</Domain>
   </xsl:template>
</xsl:stylesheet>

Obviously the logic will vary depending on where you will be getting the new data from, and where it needs to go. The above stylesheet merely inserts an <Age> element after every <Name> element, and a <Domain> element after every <Dept> element.

(Limitation: if your document could have <Name> or <Dept> elements within other <Name> or <Dept> elements, only the outermost ones will have this special processing. I don't think you intend for your document to have this kind of recursive structure, so it wouldn't affect you, but it's worth mentioning just in case.)

Sign up to request clarification or add additional context in comments.

5 Comments

What if you only want the element to be added once, instead of once for every Name/Dept elements?
@Joe: Wow, almost 5 years later. :-) In that case you add the result element in a template that will only match once. E.g. match="/*/Name[1]".
Excellent, I didn't really expect an answer 5 years later, much less one so prompt! That helps a lot
What if I want to pass age (34) as a parameter? I don't want to hard-coded 34 in the xslt file.
@GLP: It depends on where you want to pass it from. I would recommend creating a new question.
5

I have modified few things in the existing stylesheet ,it will allow you to choose the specific element and update in your xml.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   <!-- Identity transform -->
   <xsl:template match="@* | node()">
      <xsl:copy>
         <xsl:apply-templates select="@* | node()"/>
      </xsl:copy>
   </xsl:template>

   <xsl:template match="Name[1]">
      <xsl:copy-of select="."/>
      <Age>34</Age>
   </xsl:template>

   <xsl:template match="Dept[1]">
      <xsl:copy-of select="."/>
      <Domain>Insurance</Domain>
   </xsl:template>
</xsl:stylesheet>

XML:

<?xml version="1.0" encoding="utf-8"?>
<employee>
 <Name>ABC</Name>
 <Dept>CS</Dept>
 <Designation>sse</Designation>
 <Name>CDE</Name>
 <Dept>CSE</Dept>
 <Designation>sses</Designation>
</employee>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.