XSLT 删除元素的值 [英] XSLT to remove an Element's Value

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

问题描述

我需要从元素中删除一个值,但在输出 XML 中将元素本身保留为空元素.

I need to remove a value from an element, but preserve the element itself in the output XML as an empty element.

我的输入文件:

<a>
    <b>TEXT1
        <c>123</c>
        <d>qwe</d>
        <e>rty</e>
    </b>
    <b>TEXT2
    <c>345</c>
    <d>iop</d>
    <e>jkl</e>
    </b>
</a>

输出文件应该保留元素c,但元素中的数字应该消失.

The output file should retain element c, but the numbers in the element should be gone.

<a>
<b>TEXT1
    <c></c>
    <d>qwe</d>
    <e>rty</e>
</b>
<b>TEXT2
    <c></c>
    <d>iop</d>
    <e>jkl</e>
</b>
</a>

推荐答案

XSLT 1.0

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

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

  <xsl:template match="c">
    <c/>
  </xsl:template>

</xsl:stylesheet>

XML 输出

<a>
   <b>TEXT1
    <c/>
      <d>qwe</d>
      <e>rty</e>
   </b>
   <b>TEXT2
    <c/>
      <d>iop</d>
      <e>jkl</e>
   </b>
</a>

注意: 是等价的.

Note: <c/> and <c></c> are equivalent.

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

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