使用 xsl:attribute-set 元素的 XSLT 简化样式表
我尝试在 xsl 文档中使用
但不断收到错误消息:
- compilation error: line 47 element attribute-set
- element attribute-set only allowed as child 我还检查了 W3Schools 网站对 XSLT attribute-sets 的解释
并发现that:
Must be child of <xsl:stylesheet> or <xsl:transform>.
我不明白这是什么意思,谁能解释一下吗?
如果您需要有关我的文档、WAMP 服务器设置的更多信息,请在下面评论。
我的 XSL 文档的前两行是:
<?xml version="1.0" encoding="UTF-8"?>
<html xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml">
我不知道它们做什么,只是没有它,我的 XSL 将无法工作。
我基本上是使用这个 XSL 文件将 XML 转换为 HTML。整个过程由PHP完成:
# START XSLT
$xslt = new XSLTProcessor();
$XSL = new DOMDocument();
$XSL->load('hello.xsl');
$xslt->importStylesheet($XSL);
# LOAD XML FILE
$XML = new DOMDocument();
$XML->load('hello.xml');
#PRINT
print $xslt->transformToXML($XML);
I'm trying to use the <xsl:attribute-set>
in my xsl document but I keep getting error messages:
- compilation error: line 47 element attribute-set
- element attribute-set only allowed as child of stylesheet
I've also checked the W3Schools website's explanation on XSLT attribute-sets and found out that:
Must be child of <xsl:stylesheet> or <xsl:transform>.
I don't understand what this means, can anyone explain?
If you need more information about my documents, WAMP server set up please comment below.
The first two lines of my XSL document is:
<?xml version="1.0" encoding="UTF-8"?>
<html xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml">
I have no idea what they do, just that without it, my XSL will not work.
I am basically transforming my XML into HTML using this XSL file. The whole process is done by PHP:
# START XSLT
$xslt = new XSLTProcessor();
$XSL = new DOMDocument();
$XSL->load('hello.xsl');
$xslt->importStylesheet($XSL);
# LOAD XML FILE
$XML = new DOMDocument();
$XML->load('hello.xml');
#PRINT
print $xslt->transformToXML($XML);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在使用非常罕见的“文字结果元素作为样式表”工具,也称为“简化样式表”,其中
xsl:stylesheet
元素和最外层的xsl:template
是隐式的。你的问题说明了为什么这个设施很少被使用——它很快就会耗尽动力。由于没有xsl:stylesheet
元素,因此xsl:stylesheet
的常见子元素都不会出现,这包括属性集的声明。更改代码以将其包装在显式
xsl:stylesheet
和xsl:template match="/"
中。然后在与xsl:template
相同的级别添加一个xsl:attribute-set
。You are using the very rarely-seen "literal result element as stylesheet" facility, also known as a "simplified stylesheet", in which the
xsl:stylesheet
element and the outermostxsl:template
are implicit. Your problem illustrates why this facility is so rarely used - it quickly runs out of steam. Because there is noxsl:stylesheet
element, none of the usual children ofxsl:stylesheet
can be present, and this includes declarations of attribute sets.Change your code to wrap it in an explicit
xsl:stylesheet
andxsl:template match="/"
. Then add anxsl:attribute-set
at the same level as thexsl:template
.以下内容是在 XSLT 开始添加到问题之前编写的。它没有解决文字结果元素作为样式表问题的本质。迈克尔·凯的回答确实如此。
xsl:attribute-set
必须是xsl:stylesheet
元素的子元素,该元素是 XSLT 的根元素。这与xsl:output
或xsl:template
相同。标准将这些元素描述为“顶级元素”类别。
w3schools.com 从几个方面说明了这一点:
例子
The following was written before the start of XSLT was added to the question. It does not address the literal result element as stylesheet nature of the problem. Michael Kay's answer does.
An
xsl:attribute-set
must be a child element of yourxsl:stylesheet
element, which is the root element of the XSLT. This is the same as forxsl:output
orxsl:template
.The standard describes these elements as being in the "top-level-element" category.
w3schools.com says this in several ways:
example
使用简化语法的另一种方法是将 xmlns 从
html
标记中移出,然后通过document
函数内联源文件:然后包含它:
参考
Another way this would work with the simplified syntax would be to move the xmlns off of the
html
tag, then inline the source file via thedocument
function:Then include it:
References