【问题标题】:XSLT select all text and specific nodeXSLT 选择所有文本和特定节点
【发布时间】:2015-11-23 10:38:49
【问题描述】:

我想从 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>

对此的任何帮助将不胜感激。

【问题讨论】:

    标签: xml xslt xhtml


    【解决方案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

    【讨论】:

    • 感谢您的回答,但两者都不适合我。
    • @BernatL “不起作用”到底是什么意思? --附言在这里看到它的工作:xsltransform.net/94rmq68
    • 我得到 null 结果,但可能是处理器错误。我正在检查您在不同 XSLT 处理器中的解决方案,现在看起来是这样。
    • 好的,确实是处理器问题。再次感谢,您的解决方案工作正常。 @michael.hor257k
    猜你喜欢
    • 1970-01-01
    • 2013-08-02
    • 2018-07-20
    • 2011-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多