XSLT选择所有文本和特定节点

时间:2015-11-23 10:38:50

标签: xml xslt xhtml

我想从XHTML文件中选择所有文本,除了需要完全打印的所有超链接元素:

XHTML

<?xml version="1.0" encoding="UTF-8" ?>
<html>
   <body>
      <p>This is a paragraph.</p>
      <p>This is another paragraph.</p>
      <a href="www.link.com">This is a link.</a>
   </body>
</html>

期望的输出:

This is a paragraph. This is another paragraph. <a href="www.link.com">This is a link.</a>

我使用的是以下XSLT,但没有结果:

XSLT

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

   <xsl:output method="text"/>

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

   <xsl:template match="ancestor-or-self::text()">
      <xsl:value-of select="text()"/>
   </xsl:template>

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

</xsl:stylesheet>

对此的任何帮助都将非常感激。

1 个答案:

答案 0 :(得分:1)

以这种方式尝试:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="a">
    <xsl:copy-of select="."/>
</xsl:template>

</xsl:stylesheet>

请注意输出方法。另见:http://www.w3.org/TR/xslt/#built-in-rule

相关问题