使用 xsl:attribute-set 元素的 XSLT 简化样式表

发布于 2024-10-18 22:15:19 字数 1093 浏览 3 评论 0原文

我尝试在 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 技术交流群。

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

发布评论

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

评论(3

农村范ル 2024-10-25 22:15:19

您正在使用非常罕见的“文字结果元素作为样式表”工具,也称为“简化样式表”,其中 xsl:stylesheet 元素和最外层的 xsl:template 是隐式的。你的问题说明了为什么这个设施很少被使用——它很快就会耗尽动力。由于没有 xsl:stylesheet 元素,因此 xsl:stylesheet 的常见子元素都不会出现,这包括属性集的声明。

更改代码以将其包装在显式 xsl:stylesheetxsl: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 outermost xsl:template are implicit. Your problem illustrates why this facility is so rarely used - it quickly runs out of steam. Because there is no xsl:stylesheet element, none of the usual children of xsl:stylesheet can be present, and this includes declarations of attribute sets.

Change your code to wrap it in an explicit xsl:stylesheet and xsl:template match="/". Then add an xsl:attribute-set at the same level as the xsl:template.

私藏温柔 2024-10-25 22:15:19

以下内容是在 XSLT 开始添加到问题之前编写的。它没有解决文字结果元素作为样式表问题的本质。迈克尔·凯的回答确实如此。

xsl:attribute-set 必须是 xsl:stylesheet 元素的子元素,该元素是 XSLT 的根元素。这与 xsl:outputxsl:template 相同。

标准将这些元素描述为“顶级元素”类别。

w3schools.com 从几个方面说明了这一点:

  • ELMENT 是一个顶级元素。
  • 必须是的子级或
  • ELEMENT 是顶级元素,并且必须显示为的子节点。或

例子

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output method="xml" indent="yes"/>

    <xsl:attribute-set name="body-attr">
        <xsl:attribute name="color">red</xsl:attribute>
    </xsl:attribute-set>

    <xsl:template match="/">
        <xsl:element name="result" use-attribute-sets="body-attr">
        </xsl:element>
    </xsl:template>

</xsl:stylesheet>

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 your xsl:stylesheet element, which is the root element of the XSLT. This is the same as for xsl:output or xsl:template.

The standard describes these elements as being in the "top-level-element" category.

w3schools.com says this in several ways:

  • ELMENT is a top-level element.
  • Must be child of <xsl:stylesheet> or <xsl:transform>
  • ELEMENT is a top-level element, and must appear as a child node of <xsl:stylesheet> or <xsl:transform>

example

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output method="xml" indent="yes"/>

    <xsl:attribute-set name="body-attr">
        <xsl:attribute name="color">red</xsl:attribute>
    </xsl:attribute-set>

    <xsl:template match="/">
        <xsl:element name="result" use-attribute-sets="body-attr">
        </xsl:element>
    </xsl:template>

</xsl:stylesheet>
扎心 2024-10-25 22:15:19

使用简化语法的另一种方法是将 xmlns 从 html 标记中移出,然后通过 document 函数内联源文件:

<?xml version="1.0" encoding="UTF-8"?>
 <section
  xsl:version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns=""
 >

    <blockquote>
        <xsl:for-each select="document('movies.xml')//processing-instruction()[contains(., '2014')]">
            <p><xsl:value-of select="concat( local-name(),' ', current() )"/></p>

            <a href="{substring-before(document('movies.xml')//processing-instruction(),',')}">
                <input type="button" value="Stream {local-name()}"/>
            </a>

            <a href="{substring-after(document('movies.xml')//processing-instruction(),',')}">
                <input type="button" value="{local-name()} Soundtrack"/>                    
            </a>

        </xsl:for-each>            
    </blockquote>

 </section>

然后包含它:

<stylesheet version="1.0" xmlns="http://www.w3.org/1999/XSL/Transform">
    <include href="simple.xsl"/>
    <attribute-set name="foo"></attribute-set>
    <apply-templates/>
</stylesheet>

这意味着启动转换的唯一有用方法是提供文档节点作为初始匹配选择,以便使用未命名模式通过隐式 match="/" 模板规则进行匹配。

参考

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 the document function:

<?xml version="1.0" encoding="UTF-8"?>
 <section
  xsl:version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns=""
 >

    <blockquote>
        <xsl:for-each select="document('movies.xml')//processing-instruction()[contains(., '2014')]">
            <p><xsl:value-of select="concat( local-name(),' ', current() )"/></p>

            <a href="{substring-before(document('movies.xml')//processing-instruction(),',')}">
                <input type="button" value="Stream {local-name()}"/>
            </a>

            <a href="{substring-after(document('movies.xml')//processing-instruction(),',')}">
                <input type="button" value="{local-name()} Soundtrack"/>                    
            </a>

        </xsl:for-each>            
    </blockquote>

 </section>

Then include it:

<stylesheet version="1.0" xmlns="http://www.w3.org/1999/XSL/Transform">
    <include href="simple.xsl"/>
    <attribute-set name="foo"></attribute-set>
    <apply-templates/>
</stylesheet>

In turn this means that the only useful way to initiate the transformation is to supply a document node as the initial match selection, to be matched by the implicit match="/" template rule using the unnamed mode.

References

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