如何从xml文件中删除根元素

时间:2009-10-15 08:39:53

标签: xslt

亲爱的朋友们,下午好。我的问题可能是这是非常基本的问题,即我们如何使用xslt从xml文件中删除根元素。下面给出了Xml文件示例。

<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>

所以,我希望输出如下

<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>

所以,不再

<Result></Result> 
xml文件中的

标签。 PLS。救命。提前谢谢。

2 个答案:

答案 0 :(得分:11)

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

身份模板按原样复制所有内容,而文档元素的模板只处理子节点(将它们交给身份模板),而不复制根节点本身。

如果您出于某种原因想要将<summary>作为CDATA保留,则需要

<xsl:output cdata-section-elements="summary" />

答案 1 :(得分:0)

仅适用于像我这样的人,他们正在寻找完整的XSLT文件:

<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>