【发布时间】:2014-11-19 13:37:04
【问题描述】:
我有一些看起来像这样的 XML:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<issue xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<comment text="<div class="wiki text"><h4>Tom Fenech</h4>Here is a comment</div> "/>
</issue>
</root>
如您所见,comment 节点中的text 属性包含转义的 HTML。我想将属性的内容作为 XHTML 获取,我目前在模板中使用:
<xsl:value-of select="@text" disable-output-escaping="yes" />
这让我得到了最终输出中的 HTML:
<div class="wiki text"><h4>Tom Fenech</h4>Here is a comment</div>
但我希望能够提取<h4> 标记的内容以在其他地方使用。一般来说,一旦它被转义,能够操纵它的内容会很好。
如何将更多模板应用于<xsl:value-of /> 的输出?
我目前正在使用PHP built-in XSLT processor,它支持 XSLT 1.0 版,但如果新版本的功能使这成为可能,我愿意考虑使用替代处理器。
【问题讨论】:
-
您使用或可以使用哪种 XSLT 处理器? Saxon 9.6 HE 版本是一个 XSLT 3.0 处理器,它允许您使用
<xsl:apply-templates select="parse-xml(@text)/node()"/>或者更好的<xsl:apply-templates select="parse-xml-fragment(@text)/node()"/>。见w3.org/TR/xpath-functions-30/#func-parse-xml。如果你想使用value-of和disable-output-escaping,那么你总是需要两个样式表,第一个使用disable-output-escaping输出属性值,第二个使用第一个的结果。 -
@Martin 我正在使用 PHP 附带的 XSLT 处理器(仅支持 1.0 版)。我已经更新了这个问题。我知道可以在 PHP 中使用其他 XSLT 处理器(例如 Saxon),所以我仍然会对使用更现代版本的功能的解决方案感兴趣。