gpt4 book ai didi

xslt - XSLT 转换中的动态文档类型(正确使用结果文档指令)

转载 作者:行者123 更新时间:2023-12-01 00:44:48 24 4
gpt4 key购买 nike

我正在使用 XSLT,需要根据参数在转换后的输出中动态生成 doctype。我听说这不能使用 XSLT 1.0 来完成,但可以在 2.0 版中使用 result-document 标记。

到目前为止,从 this 中的答案开始问题,我有这样的事情:

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="html" indent="yes"/>
<xsl:param name="doctype.system" select="'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'" />
<xsl:param name="doctype.public" select="'-//W3C//DTD XHTML 1.0 Strict//EN'" />
<xsl:template match="/">
<xsl:result-document doctype-public="{$doctype.public}" doctype-system="{$doctype.system}" method="html">
<html>
<head>
<xsl:apply-templates select="report/head/node()"/>
</head>
<body>
<!-- ommitted for brevity -->
</body>
</html>
</xsl:result-document>
</xsl:template>
</xsl:stylesheet>

上面的问题是没有生成输出!

如果我从上面删除 results-document 标记,我的转换就会应用并输出一个文档,正如预期的那样。

有什么线索吗?我是否正确使用了结果文档标签?


更新:作为对一些评论的回应,这里有一个可以工作的小版本,一个不能工作的小版本(省略了结果文档指令的参数化)

这可行(没有结果文档):

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="html" indent="yes"/>
<xsl:template match="/">
<html>
<head>

</head>
<body>

</body>
</html>
</xsl:template>
</xsl:stylesheet>

输出:

<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body></body>
</html>

但这不会产生任何输出:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="html" indent="yes"/>
<xsl:template match="/">
<xsl:result-document doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" method="html">
<html>
<head>

</head>
<body>

</body>
</html>
</xsl:result-document>
</xsl:template>
</xsl:stylesheet>

最佳答案

您还发现,Xalan 仅支持 XSLT 1.0,但如果您已更改为 Saxon 9,则可以轻松实现您想要的。

此外,您可以使用名称定义 xsl:output,并将其用作 xsl:result-document 中的格式,而不是使用您的 doctype 设置定义参数:

<xsl:output name="my-xhtml-output" method="xml" encoding="UTF-8" indent="yes"
doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"/>

然后在您的 xsl:result-document 中使用以下输出格式:

<xsl:result-document href="{$filename}" format="my-xhtml-output">
...
</xsl:result-document>

Imo,如果你有很多不同的输出格式,这可以更容易地维护它们。

关于xslt - XSLT 转换中的动态文档类型(正确使用结果文档指令),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4275304/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com