如何删除文本但保留HTML元素,属性和属性值?

时间:2012-11-14 02:00:24

标签: html xml xslt xpath

使用XSLT,如何从HTML中删除所有文本节点,但保留元素标签,属性名称和属性值?

<table id="preserve-this-value">
  <caption>Lose this text node</caption>

转型:

<table id="preserve-this-value">
  <caption></caption>

谢谢:)

1 个答案:

答案 0 :(得分:2)

使用此模板:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>

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

  <xsl:template match="text()"/>

</xsl:stylesheet>

它复制除文本节点之外的所有节点(元素,属性)。

相关问题