【问题标题】:XSLT - multiple templates within HTML transformationXSLT - HTML 转换中的多个模板
【发布时间】:2019-05-15 13:00:46
【问题描述】:

我在网上找不到任何可靠、有用的答案。对于一个类,我需要有一个 XML、XSD、XSL、HTML 和 CSS 文件才能上交。XML 和 XSD 已经完成并且它们能够被验证,但是我遇到了 XSL/HTML 的问题。基本上我需要在同一个 XSL 中有 5 个不同的模板——一个用于我的 XML 中的每个部分。这是我的 XML 的基本大纲:

<duchovny>
    <filmography>
        ...
    </filmography>
    <vg>
        ...
    </vg>
    <music>
        ...
    </music>
    <books>
        ...
    <awards>
        ...
    </awards>
</duchovny>

这就是我的 XSL 所拥有的——当我尝试在浏览器中显示我的 XML 时,它给了我一个错误(“加载样式表时出错:解析 XSLT 样式表失败。”):

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" version="4.0" indent="yes"/>
    <html>
        <head>
            <link rel="stylesheet" type="text/css" href="style.css"/>
            <title>All About David Duchovny</title>
        </head>
        <body>
            <xsl:template match="/">
                <header>
                    <h1>All About David Duchovny</h1>
                    <div id="navigation">
                        <nav>
                            <a href="#filmography">filmography</a> | 
                            <a href="#vg">video games</a> | 
                            <a href="#music">music</a> | 
                            <a href="#bookd">books</a> | 
                            <a href="#awards">awards</a>
                        </nav>
                    </div>
                </header>
            </xsl:template>
            <xsl:template match="filmography">
                <section>
                    <div id="filmography" a href="#filmography">
                        <h2>Filmography</h2>
                        <h3>Movies</h3>
                        <xsl:for-each select="//film">
                            <p><xsl:value-of select="film/info1/@title"/></p>
                        </xsl:for-each>
                    </div>
                </section>
            </xsl:template>
        </body>
    </html>
</xsl:stylesheet>

我不明白为什么这不起作用。当我在输出调用之后放置它时它可以工作,但是我尝试显示电影标题的部分没有出现(因为我需要在该部分之前关闭 HTML 元素,我想)。我不知道如何应用多个模板并让它们全部显示出来。

或者(虽然同样不起作用),我有这段代码试图使用应用模板:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" version="4.0" indent="yes"/>
    <xsl:template match="/">
        <html>
            <head>
                <link rel="stylesheet" type="text/css" href="style.css"/>
                <title>All About David Duchovny</title>
            </head>
            <body>
                <header>
                    <h1>All About David Duchovny</h1>
                    <div id="navigation">
                        <nav>
                            <a href="#filmography">filmography</a> | 
                            <a href="#vg">video games</a> | 
                            <a href="#music">music</a> | 
                            <a href="#bookd">books</a> | 
                            <a href="#awards">awards</a>
                        </nav>
                    </div>
                </header>
                <section>
                    <xsl:apply-template match="filmography" mode="filmography"/>
                </section>
            </body>
        </html>
    </xsl:template>
    <xsl:template match="filmography">
        <div id="filmography" a href="#filmography">
            <h2>Filmography</h2>
            <h3>Movies</h3>
            <xsl:for-each select="//film">
                <p><xsl:value-of select="film/info1/@title"/></p>
            </xsl:for-each>
        </div>
    </xsl:template>
</xsl:stylesheet>

非常感谢您的帮助!!

【问题讨论】:

  • 我修复了 "
    " 以删除不必要的 'a',所以请忽略它!
  • 好收获!!!!您通常可以通过在网络浏览器中打开来检查文档是否格式正确,例如错误。
  • 这里有一个元问题。您的第一次尝试完全错误(xsl:template 只能作为 xsl:stylesheet 的子项出现)。现在(a)这表明您在猜测:您编写了一些代码,而没有先进行足够的阅读以理解该语言的概念,并且(b)您没有得到任何体面的诊断,因为您试图在浏览器,而无需先创建正确的开发环境,以确保您对错误获得良好的反馈。简而言之:您在没有参加任何游泳课程或了解游泳池深度的情况下就跳入了深水区。
  • 老实说,我发现您的评论非常粗鲁。这是我第二次尝试编写 XSLT(我的第一次成功),因为项目指南是在我完成所有工作后才给我们的。我显然不明白问题出在哪里,您的评论也无济于事。我在课堂上很专心,并且学得很好;这不是我们花费大量时间的事情。感谢有人在这里分享的答案,我能够解决并完成我的项目,但我需要让你知道你的判断是粗鲁和不受欢迎的。

标签: html xml xslt


【解决方案1】:

你的问题出在这条线上……

<xsl:apply-template match="filmography" mode="filmography"/>

问题如下:

  1. 应该是xsl:apply-templates
  2. match 不是 xsl:apply-templates 的有效属性。你应该使用select
  3. 您位于与\ 匹配的模板中,该模板是文档节点。这是duchovny 节点的父节点,因此尝试选择filmography 将找不到任何东西。将模板匹配改为\duchovny(或只是\*
  4. 这里也不需要mode(虽然mode是有效的,只是在这种情况下你真的不需要使用它)

所以,应该是&lt;xsl:apply-templates select="filmography" /&gt;,但是你提到想要多个模板,大概匹配vgaward等。在这种情况下,只需这样做,因为这将选择@987654336的所有子节点@ 并为他们找到相关的匹配节点

<xsl:apply-templates />

试试这个 XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" version="4.0" indent="yes"/>
    <xsl:template match="/*">
        <html>
            <head>
                <link rel="stylesheet" type="text/css" href="style.css"/>
                <title>All About David Duchovny</title>
            </head>
            <body>
                <header>
                    <h1>All About David Duchovny</h1>
                    <div id="navigation">
                        <nav>
                            <a href="#filmography">filmography</a> | 
                            <a href="#vg">video games</a> | 
                            <a href="#music">music</a> | 
                            <a href="#bookd">books</a> | 
                            <a href="#awards">awards</a>
                        </nav>
                    </div>
                </header>
                <section>
                    <xsl:apply-templates />
                </section>
            </body>
        </html>
    </xsl:template>

    <xsl:template match="filmography">
        <a name="filmography" />
        <div id="filmography">
            <h2>Filmography</h2>
            <h3>Movies</h3>
            <xsl:for-each select="//film">
                <p><xsl:value-of select="film/info1/@title"/></p>
            </xsl:for-each>
        </div>
    </xsl:template>

    <xsl:template match="vg">
        <h2>vg goes here</h2>
    </xsl:template>
</xsl:stylesheet>

【讨论】:

  • 非常感谢!我让它工作并以我想要的方式完成了这个项目。这是一个非常基本的页面,因为 a) 没有时间和 b) 缺乏 CSS 技能,但它包含所有必要的信息。非常感谢您的回答
猜你喜欢
相关资源
最近更新 更多
热门标签