条件在 XSLT 1.0 中?
我有一个 XSLT 1.0(2.0 不是一个选项)样式表,它生成 XHTML。 它可以根据参数生成完整的 XHTML 有效的文档或只是一个
片段,用于 包含在网页中。
我的问题是在这两个中生成不同的 XML 声明 案例。 对于独立页面,我需要:
<xsl:output doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"/>
对于
一个:<xsl:output omit-xml-declaration="yes"/>
但是
不能包含在
的直接子级。
我看到的唯一解决方案是创建一个包含大多数模板的样式表,然后创建两个带有正确的
的小“包装器”,这将
主样式表。
我一直在寻找更好的主意,但显然没有。 根据 Andrew Hare 和 jelovirt 的建议,我编写了两个“驱动程序”,两个简单的样式表,它们调用正确的
,然后编写主样式表。 以下是这些驱动程序之一,用于独立 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>
I have an XSLT 1.0 (2.0 is not an option) stylesheet which produces
XHTML. It can, depending on a parameter, produce a full XHTML
validable document or just a <div>...</div>
snippet, intended for
inclusion in a Web page.
My problem is to produce different XML declarations in these two
cases. For the standalone page, I need:
<xsl:output doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"/>
And for the <div>
one:
<xsl:output omit-xml-declaration="yes"/>
But <xsl:output>
cannot be included in an <xsl:if>
. It can only be the direct child of <xsl:stylesheet>
.
The only solution I see is to create a stylesheet with most of the templates and then two small "wrappers" with the right <xsl:output>
and which will <xsl:import>
the main stylesheet.
I was looking for a better idea but apparently there is none. Following advice from Andrew Hare and jelovirt, I wrote two "drivers", two simple stylesheets which call the proper <xsl:output>
and then the main stylesheet. Here is one of these drivers, the one for standalone 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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我最近不得不采用的另一个选择是:
如果您要输出到文件,这仅适用于 XSLT 1.0 和 2.0——如果您需要在同一遍中将输出处理为 XML,例如存储在变量中时,这将不起作用。
(请注意,XSLT 2.0 扩展函数可能可以一次性获取此输出并将其视为 XML,而 XSLT 3.0 有一个内置函数可将输入字符串解析为 XML。)
示例片段:
Another option, which I recently had to employ, is to:
This only works in XSLT 1.0 and 2.0 if you're outputting to a file -- this won't work if you need to process the output as XML in the same pass, such as when storing in a variable.
(Note that XSLT 2.0 extension functions might make it possible to take this output and treat it as XML in one go, and XSLT 3.0 has a built-in function to parse an input string as XML.)
Example snippet:
听起来您需要的是两个不同的样式表。 如果可能的话,您应该创建两个单独的样式表并从代码中动态调用您需要的样式表。
It sounds like what you need is two different stylesheets. If at all possible you should create two separate stylesheets and dynamically call the one you need from code.
在 XSLT 中,
omit-xml-declaration
的值必须是yes
或no
,您不能在那里使用属性值模板。 这适用于 1.0 和 2.0。doctype 属性可以使用 AVT,但问题是您不能省略该属性,只能输出空属性,这会导致输出具有空 doctype 字符串。
抱歉,无法使用 XSLT 完成。 您可以使用两个不同的样式表,也可以在调用 XSLT 处理器的代码中设置输出参数。
In XSLT the value of
omit-xml-declaration
must be eitheryes
orno
, you can't use Attribute Value Templates there. This applies to both 1.0 and 2.0.The doctype attributes can use AVTs, but the problem is that you cannot omit the attribute, you can only output an empty attribute and this leads to output that has empty doctype strings.
Sorry, can't be done with XSLT. You can either use two different stylesheets, or set the output parameters in the code that calls the XSLT processor.