Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove root element from xml file

Tags:

xslt

Dear Friends good afternoon. My problem may be this is very basic one i.e. how can we remove root element from a xml file using xslt. Xml file example given below.

<Result>
<Jobs id="1">
  <Job ID="000000" PositionID="0000">
    <Title>Development Manager - Investment Banking - Equities Business</Title>
    <Summary><![CDATA[An experienced Development Manager with previous experience leading a small to mid-size team of developers in a Java/J2EE environment. A hands on role, you will be expected to manage and mentor a team of developers working on a mix of greenfield and maintenance projects.&#160;&#160; My client, a well known investment bank, requires an experienced Development Manager to join their core technology team. This t]]></Summary>
    <DateActive Date="2009-10-06T19:36:43-05:00">10/6/2009</DateActive>
    <DateExpires Date="2009-11-05T20:11:34-05:00">11/5/2009</DateExpires>
    <DateUpdated Date="2009-10-06 20:12:00">10/6/2009</DateUpdated>
    <CompanyName>ABC Technology</CompanyName>
  </Job>
</Jobs>
</Result>

So, I want the output as below

<Jobs>
  <Job ID="000000" PositionID="0000">
    <Title>Development Manager - Investment Banking - Equities Business</Title>
    <Summary><![CDATA[An experienced Development Manager with previous experience leading a small to mid-size team of developers in a Java/J2EE environment. A hands on role, you will be expected to manage and mentor a team of developers working on a mix of greenfield and maintenance projects.&#160;&#160; My client, a well known investment bank, requires an experienced Development Manager to join their core technology team. This t]]></Summary>
    <DateActive Date="2009-10-06T19:36:43-05:00">10/6/2009</DateActive>
    <DateExpires Date="2009-11-05T20:11:34-05:00">11/5/2009</DateExpires>
    <DateUpdated Date="2009-10-06 20:12:00">10/6/2009</DateUpdated>
    <CompanyName>ABC Technology</CompanyName>
  </Job>
</Jobs>

So, No more

<Result></Result> 

tags in the xml file. Pls. help. Thanks in advance.

like image 611
pravakar Avatar asked Oct 15 '09 08:10

pravakar


People also ask

How do you remove a specific tag in XML?

The removeChild() method removes a specified node. The removeAttribute() method removes a specified attribute.

What is a root element in XML?

The XML root element represents the XML document that is being mapped. The XML root element is a looping structure that contains elements and content particles that repeat in sequence until either the group data ends or the maximum number of times that the loop is permitted to repeat is exhausted.

Can XML documents have root element?

Each XML document has exactly one single root element. It encloses all the other elements and is, therefore, the sole parent element to all the other elements.

Does XML need root tag?

Every XML document must have a root tag which enclose the XML document.


1 Answers

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

<!-- template for the document element -->
<xsl:template match="/*">
  <xsl:apply-templates select="node()" />
</xsl:template>

The identity template copies everything as it is, while the template for the document element only takes care of the child nodes (handing them over to the identity template) while not copying the root node itself.

If you want to keep your <summary> as CDATA for some reason, you will need

<xsl:output cdata-section-elements="summary" />
like image 157
Tomalak Avatar answered Oct 14 '22 14:10

Tomalak