使用XSLT删除节点

时间:2016-01-13 20:51:27

标签: xml xslt

我有2个XML样本文档,如下所示

<document>

   <content name="filetype>other</content>

  <content name="filetype>xml</content>

</document>




<document>

  <content name="filetype>other</content>

 </document>

如果内容标记的文件类型不是“其他”,我想删除“其他”文件类型。因此,XML 2将保持原样,因为它只有“其他”文件类型,而XML 1变为

<document>

   <content name="filetype>xml</content>

 </document> 

我已经尝试了一些接近处理这个但似乎没有工作。以下是我的最新方法

<xsl:template match="content[@name='filetype']" mode="copy">


    <xsl:if test=".='other'">

       <xsl:variable name="filetypes" select="../content[@name='filetype']" />



<xsl:variable name="coun" select="count($filetypes)" />


<xsl:if test="coun = 1">

  <content name="filetype">
    other
  </content>

   </xsl:if>


  </xsl:if>


  <xsl:if test=".!='other'">

    <content name="filetype">

      <xsl:value-of select="." />

  </content>

   </xsl:if>

 </xsl:template>

我如何实现这一目标?

1 个答案:

答案 0 :(得分:2)

使用

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

<xsl:template match="@* | node()">
  <xsl:copy>
    <xsl:apply-templates select="@* | node()"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="document[content[@name = 'filetype'] != 'other']/content[@name = 'filetype' and . = 'other']"/>

</xsl:stylesheet>