Removing certain XML elements via XSLT

こ雲淡風輕ζ 提交于 2020-01-16 09:55:08

问题


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?


回答1:


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>


来源:https://stackoverflow.com/questions/1524786/removing-certain-xml-elements-via-xslt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!