tags:

views:

386

answers:

2

Hi to every one. My problem is: I have an XML file where I want to remove some child elements without removing parents. Can anyone help me to get the result by using ASP.NET?

Here is my XML file:

<Jobs> 
  <Job>
    <Title></Title>
    <Summary</Summary>
    <DateActive>9/28/2009</DateActive>
   <DateExpires>10/28/2009</DateExpires>
   <DateUpdated>9/28/2009</DateUpdated>
    <Location>
      <Country>India</Country>
      <State>xxx</State>
      <City>xxx</City>
      <PostalCode>xxx</PostalCode>
    </Location>
    <CompanyName>Finance</CompanyName>
    <Salary>
      <Max>70,000.00</Max>
      <Type>Per Year</Type>
      <Currency>Dollar</Currency>
    </Salary>
    <BuilderFields />
    <DisplayOptions />
    <AddressType>6</AddressType>
    <Job_Id>123456</Job_Id>
  </Job>

From above XML I want to remove <Location> and <Salary> elements only, without deleting their child nodes. How would I use XSLT to get the desired result in XML file?

+4  A: 

You can use the pattern of applying the identity transform to copy everything, and overriding that for the Location and Salary element nodes, not copying them but just processing their children.

<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;

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

  <!-- override: for Location and Salary elements, just process the children -->
  <xsl:template match="Location|Salary">
    <xsl:apply-templates select="node()"/>
  </xsl:template>

</xsl:stylesheet>


Updated for your follow-up question. From your example, it's a bit unclear what else you actually want to do, but assuming that in addition to above, you also want to:

  1. For some elements, convert attributes to child elements. You can do this by adding an additional overriding rule which matches the attributes and outputs elements.

  2. For some other elements, remove attributes altogether. You can do this similarly to the above, but this time just use an empty template which does not output anything.

  3. Output the contents of some elements using CDATA sections. You can specify such elements with the cdata-section-elements attribute of xsl:output.

An example stylesheet demonstrating all that:

<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;

  <xsl:output method="xml" indent="yes" media-type="application/xml"
              cdata-section-elements="Summary"/>

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

  <!-- override: for Location and Salary nodes, just process the children -->
  <xsl:template match="Location|Salary">
    <xsl:apply-templates select="node()"/>
  </xsl:template>

  <!-- override: for selected elements, convert attributes to elements -->
  <xsl:template match="Jobs/@*|Job/@*">
    <xsl:element name="{name()}">
      <xsl:value-of select="."/>
    </xsl:element>
  </xsl:template>

  <!-- override: for selected elements, remove attributes -->
  <xsl:template match="DateActive/@*|DateExpires/@*|DateUpdated/@*"/>

</xsl:stylesheet>
Jukka Matilainen
A: 

Thanks Jackem for quick resposne, but still i didn't get desired output i.e. after applying i am getting following result in the browser,

Market-leading company With a newly created role High Profile Position With Responsibilty, Visibility & Opportunity Must Have Solid BA Skills Honed in a SDLC environment Market-leading company With a newly created role High Profile Position With Responsibilty, Visibility & Opportunity Must Have Solid BA Skills Honed in a SDLC environment My client is a market-leader who continue to go from strengt10/5/200911/4/200910/5/2009AustraliaNSWSydney2000Skill Quest90,000.00120,000.00Per YearAUD6

Sorry for not posting the original document, herewith i am sending it Not in xml format. Hence again i am posting the original one

- <Jobs Found="10" Returned="50">
- <Job ID="8000000" PositionID="600002">
  <Title>Development Manager</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> 
- <Location>
  <Country>xxxx</Country> 
  <State>xxx</State> 
  <City>xxx</City> 
  <PostalCode>xxx</PostalCode> 
  </Location>
  <CompanyName>abc Technology</CompanyName> 
  <BuilderFields /> 
  <DisplayOptions /> 
  <AddressType>1234</AddressType> 
  </Job>

MY DESIRED OUTPUT IS AS FOLLOWS:

<Jobs>
<Found>10</Found>
<Returned>50</Returned>
<Job_ID>8000000</Job_ID>
<PositionID>600002</PositionID>
<Title>Development Manager</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>10/6/2009</DateActive> 
  <DateExpires>11/5/2009</DateExpires> 
  <DateUpdated>10/6/2009</DateUpdated> 
  <Country>xxxx</Country> 
  <State>xxx</State> 
  <City>xxx</City> 
  <PostalCode>xxx</PostalCode> 
  </Location>
  <CompanyName>abc Technology</CompanyName> 
  <BuilderFields /> 
  <DisplayOptions /> 
  <AddressType>1234</AddressType> 
  </Job>
</Jobs>

Pls. help me to a get solution in the mentioned format,

pravakar
You've still got the close tag for Location. You don't show the children of Salary - do you want them or not? Do to want to remove the attributes on the DateActive, DateExpires, DateUpdated?
Pete Kirkham
Thanks for your response. I don't want Location and Salary elements only but need there child nodes and attributes as also elements.
pravakar
You could just edit the question instead of posting follow-up questions an an "answer".
Jukka Matilainen
If your browser is showing the XML without any tags, it might be because it is showing it as HTML (and ignoring any tags that are not HTML). If you are accessing the XML from a server, make sure the server reports the correct `Content-Type` for the file. If you are accessing it from a local file, make sure the file has an `*.xml` suffix.
Jukka Matilainen