我有一个 XSLT 1.0(2.0 不是一个选项)样式表,它生成
XHTML。它可以根据参数生成完整的 XHTML
有效的文件或只是<div>...</div>
片段,旨在用于
包含在网页中。
我的问题是在这两个中生成不同的 XML 声明
案例。对于独立页面,我需要:
<xsl:output doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"/>
而对于<div>
one:
<xsl:output omit-xml-declaration="yes"/>
But <xsl:output>
不能包含在<xsl:if>
。它只能是direct的孩子<xsl:stylesheet>
.
我看到的唯一解决方案是创建一个包含大多数模板的样式表,然后创建两个带有右侧的小“包装器”<xsl:output>
以及这将<xsl:import>
主要样式表。
我一直在寻找更好的主意,但显然没有。根据 Andrew Hare 和 jelovirt 的建议,我编写了两个“驱动程序”,两个简单的样式表,它们调用正确的<xsl:output>
然后是主样式表。以下是这些驱动程序之一,用于独立 HTML 的驱动程序:
<?xml version="1.0" encoding="us-ascii"?>
<!-- This file is intended to be used as the main stylesheet, it creates a
standalone Web page.
-->
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:import href="traceroute2html.xsl"/>
<xsl:param name="standalone" select="'true'"/>
<xsl:output doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"/>
</xsl:stylesheet>
我最近不得不采用的另一个选择是:
- 在所有情况下都忽略 XML 声明。
- 有条件地输出声明作为未转义的文本.
如果您要输出到文件,这仅在 XSLT 1.0 和 2.0 中有效——如果您需要在同一遍中将输出处理为 XML,例如存储在变量中时,这将不起作用。
(请注意,XSLT 2.0 扩展函数可能可以一次性获取此输出并将其视为 XML,而 XSLT 3.0 有一个内置函数可以将输入字符串解析为 XML。)
示例片段:
<!-- Omit the XML declaration as the base case:
we can conditionally output a declaration
as text, but we *cannot* apply conditions on
this `omit-xml-declaration` attribute here. -->
<xsl:output method="xml" indent="no"
omit-xml-declaration="yes"
/>
<!-- Root element match: evaluate different cases, output XML declaration,
XHTML DOCTYPE, or something else, then process the rest of the input. -->
<xsl:template match="/">
<xsl:choose>
<xsl:when test="'... some condition ...'">
<xsl:text disable-output-escaping="yes"><?xml version="1.0" encoding="UTF-8"?></xsl:text>
</xsl:when>
<xsl:when test="'... some other condition ...'">
<xsl:text disable-output-escaping="yes"><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"></xsl:text>
</xsl:when>
<xsl:otherwise>
<!-- ... some third kind of output ... -->
</xsl:otherwise>
</xsl:choose>
<!-- Just process the rest -->
<xsl:apply-templates/>
</xsl:template>
... [ other code ] ...
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)