2009-10-15 42 views
5

Các bạn thân mến chào buổi chiều. Vấn đề của tôi có thể là điều này rất cơ bản, tức là làm thế nào chúng ta có thể loại bỏ phần tử gốc từ một tệp xml bằng cách sử dụng xslt. Ví dụ về tệp Xml được đưa ra dưới đây.Cách xóa phần tử gốc khỏi tập tin 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> 

Vì vậy, tôi muốn đầu ra như sau

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

Vì vậy, Không còn

<Result></Result> 

thẻ trong file xml. Xin lỗi. Cứu giúp. Cảm ơn trước.

+1

Bạn có thể định dạng câu hỏi của bạn vì vậy chúng tôi có thể xem những gì nó là bạn đang yêu cầu? có vẻ như bạn cần phải phân loại < – Rippo

Trả lời

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

Mẫu nhận dạng sao chép mọi thứ như cũ, trong khi mẫu cho phần tử tài liệu chỉ chăm sóc các nút con (giao cho mẫu nhận dạng) trong khi không sao chép nút gốc.

Nếu bạn muốn giữ <summary> của bạn như CDATA đối với một số lý do, bạn sẽ cần

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

Cách sao chép các yếu tố này và chơi với chúng đã giúp tôi rất nhiều trong XSLT. Cảm ơn rất nhiều vì câu trả lời này. –

+0

@geekam Cảm ơn phản hồi của bạn! :) – Tomalak

Các vấn đề liên quan