开发者

Removing certain XML elements via XSLT

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

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>
      <Ty开发者_StackOverflow中文版pe>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?


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

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

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

精彩评论

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