0

Below is the actual xml:

<?xml version="1.0" encoding="utf-8"?>
<Procedures>
 <Title>ABC</Title>
 <P>CS</P>
 <div><P>sse</P></div>
</Procedures>

And i want the output as below:

 <?xml version="1.0" encoding="utf-8"?>
    <Procedures>
     <Title>ABC</Title>
     <body>
       <P>CS</P>
       <div><P>sse</P></div>
     </body>
    </Procedures>

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

1

2 Answers 2

2

Assuming Title is the only element that should be outside of the body you could use the following XLST 1.0 stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="Procedures">
    <xsl:copy>
      <xsl:apply-templates select="@*|Title"/>
      <body>
        <xsl:apply-templates select="*[not(self::Title)]"/>
      </body>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>
Sign up to request clarification or add additional context in comments.

Comments

1

You could do this with xmlstarlet, e.g.:

xmlstarlet ed -s /Procedures --type elem -n body \
              -m Procedures/P   Procedures/body  \
              -m Procedures/div Procedures/body  \
              infile.xml

That is, add a subnode body, move P and div to that subnode. Output:

<Procedures>
  <Title>ABC</Title>
  <body>
    <P>CS</P>
    <div>
      <P>sse</P>
    </div>
  </body>
</Procedures>

2 Comments

I didn't get your point, is this the xsl code?Please help.
@ShantoGeorge: this solution uses xmlstarlet which generates xsl code and applies it directly

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.