XSLT 转换中的动态文档类型(正确使用结果文档指令) [英] Dynamic doctype in XSLT transform (correct use of result-document instruction)

查看:18
本文介绍了XSLT 转换中的动态文档类型(正确使用结果文档指令)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 XSLT,需要根据参数在转换后的输出中动态生成文档类型.我听说使用 XSLT 1.0 无法做到这一点,但使用 result-document 标签可以使用 2.0 版.

I'm using XSLT and need to generate the doctype dynamically in the transformed output, based on a parameter. I hear that this cann't be done using XSLT 1.0, but can with version 2.0, using the result-document tag.

到目前为止,根据这个问题中的答案,我有这样的事情:

So far, from following the answer in this question, I have something like 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>

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

The problem with the above is no output is generated!

如果我从上面删除 results-document 标签,我的转换将被应用并输出一个文档,正如预期的那样.

If I remove the results-document tags from the above, my transform is applied and a document is output, as expected.

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

Any clues? Am I using the result-document tag correctly?

更新:为了回应一些评论,这里有一个有效的小版本,一个无效(省略了结果文档指令的参数化)

UPDATE: In response to some of the comments here's a small version that works, and one that doesn't (omitting the parameterisation of the result-document instruction)

这有效(没有结果文档):

This works (no result-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>

但这不会产生任何输出:

But this produces no output:

<?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,则可以轻松实现您想要的.

As you also found out, Xalan only supports XSLT 1.0, but if you've changed to Saxon 9, you could easily achive what you want.

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

Also, instead of defining parameters with your doctype settings, you could define a xsl:output with a name and use as a format in xsl:result-document:

<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 中,您将使用以下输出格式:

In your xsl:result-document you then use this output format:

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

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

Imo, this makes it easier to maintain different output formats if you have lots of them.

这篇关于XSLT 转换中的动态文档类型(正确使用结果文档指令)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆