通过去除某些XSLT的XML元素 [英] Removing certain XML elements via XSLT

查看:229
本文介绍了通过去除某些XSLT的XML元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是:我有一个XML文件,我想删除一些子元素而不删除父母。任何人都可以帮助我得到的结果通过使用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?

下面是我的XML文件:

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>

从上面的XML我想删除&LT;地点&gt; &LT;薪酬和GT; 仅元素,而不删除他们的子节点。我将如何使用XSLT来获取XML文件中所期望的结果?

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. 对于一些元素,转换属性的子元素。您可以通过添加额外的压倒一切的规则相匹配的属性和输出的元素做到这一点。

  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.

对于一些其他的元素,完全删除属性。您同样可以做到这一点上面,但这次只是用它不输出任何一个空的模板。

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.

输出一些元素的内容使用 CDATA 部分的。您可以指定这些元素具有的 CDATA节元素属性=htt​​p://www.w3.org/TR/xslt#output相对= nofollow的> XSL:输出

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>

这篇关于通过去除某些XSLT的XML元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆