开发者

How to remove root element from xml file

开发者 https://www.devze.com 2022-12-08 19:28 出处:网络
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.

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>
    &l开发者_Go百科t;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.


<!-- 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" />


Just for people like me who were looking for a complete XSLT file:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <!-- 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>   
</xsl:stylesheet>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号