条件在 XSLT 1.0 中?

发布于 2024-07-14 10:28:38 字数 1520 浏览 5 评论 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不交电费瞎发啥光 2024-07-21 10:28:38

我最近不得不采用的另一个选择是:

  1. 在所有情况下省略 XML 声明。
  2. 有条件地,将声明输出为未转义文本

如果您要输出到文件,这仅适用于 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 ] ...

Another option, which I recently had to employ, is to:

  1. Omit the XML declaration in all cases.
  2. Conditionally, output the declaration as unescaped text.

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:

<!-- 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 ] ...
心如狂蝶 2024-07-21 10:28:38

听起来您需要的是两个不同的样式表。 如果可能的话,您应该创建两个单独的样式表并从代码中动态调用您需要的样式表。

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.

善良天后 2024-07-21 10:28:38

在 XSLT 中,omit-xml-declaration 的值必须是 yesno,您不能在那里使用属性值模板。 这适用于 1.0 和 2.0。

doctype 属性可以使用 AVT,但问题是您不能省略该属性,只能输出空属性,这会导致输出具有空 doctype 字符串。

抱歉,无法使用 XSLT 完成。 您可以使用两个不同的样式表,也可以在调用 XSLT 处理器的代码中设置输出参数。

In XSLT the value of omit-xml-declaration must be either yes or no, 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文