html - 教程 5,新视角中的 cvc-elt.1

标签 html css xml xslt

对于我的 XML 在线类(class),我被分配完成本教程。

Jenna 一直致力于创建一个 DVD 库,她可以在其中列出她拥有的所有 DVD。有些是她自己买的,有些是妈妈、爸爸、姐姐、爷爷和奶奶送给她的礼物。她的库的根元素是带有一个或多个 dvd 元素的 dvdlibrary。每张 DVD 都有标题、描述、年份、公司、最喜欢的 Angular 色、最喜欢的奖励功能和购买信息。她的 Angular 色元素每个都有一个或多个 Angular 色元素。她的每个奖励元素都有一个或多个功能。她购买的每个元素都有价格、位置和日期。 dvd 元素有两个属性,bonus 元素有一个属性,purchased 元素没有属性。 Jenna 希望创建一个 XSLT 样式表,用于以有吸引力的方式显示 DVD 收藏信息。她已经为页面创建了 CSS 样式表。

完成以下内容:

  1. 使用文本编辑器打开 filedvdlibrary.txt。查看内容以熟悉 XML 文件的结构。
  2. 在序言中添加注释,包括文件说明、您的姓名、日期和文件名。
  3. 将文件另存为 dvdlibrary.xml。
  4. 在将 dvdlibrary.xsl 样式表附加到此 XML 文档的注释部分之后添加处理指令。关闭文件,保存您的更改。
  5. 使用文本编辑器打开文件 dvdlibrarytxt.xsl。在文件的评论部分输入您的姓名和日期,并将其另存为 dvdlibrary.xsl。
  6. 在文本编辑器中转到 dvdlibrary.xsl 文件,然后转到根模板。将以下命令添加到模板中:

    <html>
    <head>
    <title>Jenna's DVD Collection</title>
    <link href="dvdlibrarystyles.css"
    rel="stylesheet" type="text/css" />
    </head>
    <body>
    <header>
    <h1>Jenna's DVD Collection</h1>
    </header>
    </body>
    </html>
    
  7. </body> 正上方的根模板中标记,插入以下内容:

    <section>
    <h1>DVD List</h1>
    dvd template
    </section>
    

    其中 dvd 模板应用 dvdlibrary/dvd 路径的模板,按标题元素排序。

  8. 为 dvd 元素创建一个模板以显示每部电影的信息。将以下 HTML 代码添加到模板中:

    <article>
    </article>
    
  9. <article></article>内标签,插入以下内容以显示每张 DVD 的标题和字符列表:

    <h1>title</h1>
    <h2>Featuring: [character] [character] …</h2>
    

    其中 title 是 title 元素的值,[character] [character] … 是电影中的 Angular 色列表。 (提示:使用 for-each 指令遍历字符/字符路径中的每个字符元素。)

  10. 在 h2 标题下方,插入以下内容:

    <p>
          Bonus Features:
    <span>
    feature/feature/ …
    </span>
    </p>
    

    其中 feature/feature/… 是 bonus/feature 路径中的特征值列表,用“/”符号分隔。 (提示:将 for-each 指令与 bonus/feature 路径一起使用,并在 for-each 元素中显示上下文节点的值。)

  11. 收盘下方</p>标记,插入下表:

    <table>
    <tr>
    <th>Price</th>
    <th>Location</th>
    <th>Date</th>
    </tr>
    <tr>
    <td>price</td>
    <td>location</td>
    <td>date</td>
    </tr>
    </table>
    

    其中 price、location 和 date 是 price、location 和 date 元素的值。

  12. 保存对文件的更改。

  13. 使用 XML 编辑器或 Web 浏览器生成结果文档。验证每一项的布局和内容是否与下图所示相匹配:

  14. 将完成的文件提交给您的教师。

而且我的代码似乎仍然存在问题,我尝试添加一个 XSD 表,在其中声明所有元素,但它没有用。

这是我目前所拥有的:

这是我的 XML:

<?xml version="1.0" encoding="UTF-8"?>

<!--
This document contains data about Jenna's DVD library. 

Author: Julia Turek
Date: 6/25/2018

Filename: dvdlibrary.xml
Supporting File: dvdlibrary.xsl
-->

<?xml-stylesheet type="text/xsl" href="dvdlibrary.xsl" ?>

<dvdlibrary>  
    <dvd isbn="1-4157-4399-0" from="mom" >
        <title>Kung Fu Lizard</title>
        <description>Jo defends his training academy against the evil Mighty Moose, Benny.</description>
        <year>2011</year>
        <company>Cloud Nine Productions</company>
        <characters>
            <character>Jo, the Kung Fu Lizard</character>
            <character>Lisa, the Fighting Kricket</character>
            <character>Benny, the Evil Mangler Moose</character>
        </characters>
        <bonus num="2">
            <feature>Kung Fu Academy</feature>
            <feature>Cast Commentary</feature>
        </bonus>
        <purchased>
            <price>19.94</price>
            <location>Best DVDs in Town</location>
            <date>2012-10-03</date>
        </purchased>
    </dvd>

    <dvd isbn="1-5158-4399-0" from="mom">
        <title>Planet of the Ogres</title>
        <description>Bilbo had always dreamt he was from another planet. Now he finds out that he is. Join him as he meets his destiny to save his homeworld.</description>
        <year>2012</year>
        <company>MGM</company>
        <characters>
            <character>Bilbo, future leader of the Ogres</character>
            <character>Kilnary, leader of the invading Tigres</character>
            <character>Wicket, leader of the Morkian army</character>
        </characters>
        <bonus num="3">
            <feature>Making Of Featurette</feature>
            <feature>Theatre Trailers</feature>
            <feature>PC downloadable version</feature>
        </bonus>
        <purchased>
            <price>17.99</price>
            <location>Mallmart</location>
            <date>2013-10-06</date>
        </purchased>
    </dvd>

    <dvd isbn="1-9955-8745-0" from="mom">
        <title>Mountain King</title>
        <description>Freda is the daughter of the Mountain King. As the only heir, will she break with tradition and be the first Mountain Queen?</description>
        <year>2014</year>
        <company>Sunny Entertainment</company>
        <characters>
            <character>Freda, daughter of the Mountain King</character>
            <character>Toru, the Mountain King</character>
            <character>Ella, the Enchanted Advisor</character>
           <character>Bopo, the Monkey Bone Wizard</character>
        </characters>
        <bonus num="2">
            <feature>Cast Commentary</feature>
            <feature>Mountain King Music Video</feature>
        </bonus>
        <purchased>
            <price>14.99</price>
            <location>Movies For Everyone</location>
            <date>2015-11-07</date>
        </purchased>
    </dvd>
</dvdlibrary>

这是我的 XSL

<?xml version="1.0" encoding="UTF-8" ?>

<!--
   Project 5
   Tutorial Project

   DVD Library XSLT Style Sheet
   Author: Julia Turek
   Date:   6/28/18

   Filename: dvdlibrary.xsl
-->

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

   <xsl:output method="html"
      doctype-system="about:legacy-compat"
      encoding="UTF-8"
      indent="yes" />

   <xsl:template match="/">
    <html>
        <head>
            <title>Jenna's DVD Collection</title>
            <link href="dvdlibrarystyles.css" rel="stylesheet" type="text/css" />
        </head>
        <body>
            <header>
                <h1>Jenna's DVD Collection</h1>
            </header>
            <section>
                <h1>DVD List</h1>
                <xsl:apply-templates select="dvdlibrary/dvd">
                <xsl:sort select="title" />
                </xsl:apply-templates>

                <xsl:apply-templates select="dvd">
                    <article>    
                        <xsl:for-each select="dvd/title">
                        <h1>
                            <xsl:value-of select="title"/>
                        </h1>
                        </xsl:for-each>                    
                        <xsl:for-each select="chracters/character">
                            <h2>
                                <xsl:value-of select="character"/>
                            </h2>
                        </xsl:for-each>
                        <p>
                            Bonus Features:
                            <span>
                                <xsl:for-each select="bonus/featuress"/>
                            </span>
                        </p>


                        <table>
                            <tr>
                                <th>Price</th>
                                <th>Location</th>
                                <th>Date</th>
                            </tr>
                            <tr>
                                <td>price</td>
                                <td>location</td>
                                <td>date</td>
                            </tr>
                        </table>


                    </article>
                </xsl:apply-templates>

            </section>
        </body>
    </html>

   </xsl:template>

</xsl:stylesheet>

这是我的 CSS:

    * {
   margin: 0px;
   list-style: none;
}

header, section, article {
   display: block;
}



/* Body styles */
body {
   background-color: white;
   font-family: Verdana, Geneva, sans-serif;
   margin: 10px auto;
   width: 920px;
}

header h1 {
   color: white;
   font-size: 2.8em;
   font-weight: normal;
   text-shadow: rgb(90, 127, 0) 0px 0px 25px;
}

header h2 {
   font-size: 1.5em;
   font-weight: normal;
   color: rgb(90, 127, 0);
}

section {
   margin-top: 25px;
}

section > h1 {
   font-weight: normal;
   font-size: 1.9em;
}

article {
   border: 1px solid black;
   margin: 10px 10px;
   padding: 10px;
   width: 400px;
   float: left;
   box-shadow: rgb(151, 151, 151) 8px 8px 15px;
}

article > h1, article > h2 {
   background-color: rgb(190, 235, 110);
}

article > h1 {
   font-size: 1em;
   font-weight: normal;
   padding: 10px 0px 0px 10px;
}

article > h2 {
   font-size: 0.7em;
   font-weight: normal;
   padding: 0px 0px 10px 10px;
   border-bottom: 1px solid black;
}

article > p {
   font-size: 0.7em;
   margin: 4px 0px 0px 0px;
}

article span {
   color: rgb(151,151, 151);
}

table {
   border-collapse: collapse;
   margin: 10px 0px 5px 0px;
}

td, th {
   border: 1px solid rgb(191, 191, 191);
   font-weight: normal;
   font-size: 0.7em;
   padding: 3px;
   text-align: center;
}

th {
   background-color: rgb(231, 231, 231);
}

但我不断收到一条错误消息: cvc-elt.1: 找不到元素“dvdlibrary”的声明。

最佳答案

http://xsltfiddle.liberty-development.net/ 测试 XSLT 时(这是测试 XSLT 的好地方),给出的错误消息与您给出的错误消息并不完全匹配。

Error 1 at line 25:51 : Invalid element <article> within xsl:apply-templates

错误涉及这些行..

<xsl:apply-templates select="dvd">
    <article>    

因为确实不能在 xsl:apply-templates 中嵌入内容.你可能需要做 <xsl:template match="dvd">在这里,尽管需要将其移动为 xsl:stylesheet 的直接子级

此外,您对 xsl:for-each 的使用存在一些问题,尽管您当前的代码不会导致任何错误。但在 xsl:for-each 内您所做的任何 xpath 表达式都将与您选择的当前元素相关。所以,与其这样做……

<xsl:for-each select="chracters/character">
    <h2>
        <xsl:value-of select="character"/>
    </h2>
</xsl:for-each>

您需要这样做(还要更正拼写...)

<xsl:for-each select="characters/character">
    <h2>
        <xsl:value-of select="."/>
    </h2>
</xsl:for-each>

无论如何,先试试这个 XSLT,您可以在 http://xsltfiddle.liberty-development.net/eiZQaFj 上看到它的实际应用。

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

   <xsl:output method="html"
      doctype-system="about:legacy-compat"
      encoding="UTF-8"
      indent="yes" />

   <xsl:template match="/">
    <html>
        <head>
            <title>Jenna's DVD Collection</title>
            <link href="dvdlibrarystyles.css" rel="stylesheet" type="text/css" />
        </head>
        <body>
            <header>
                <h1>Jenna's DVD Collection</h1>
            </header>
            <section>
                <h1>DVD List</h1>
                <xsl:apply-templates select="dvdlibrary/dvd">
                <xsl:sort select="title" />
                </xsl:apply-templates>
            </section>
        </body>
    </html>
   </xsl:template>

    <xsl:template match="dvd">
        <article>    
            <h1>
                <xsl:value-of select="title"/>
            </h1>
            <xsl:for-each select="characters/character">
                <h2>
                    <xsl:value-of select="."/>
                </h2>
            </xsl:for-each>
            <p>
                Bonus Features:
                <xsl:for-each select="bonus/feature">
                    <span>
                        <xsl:value-of select="." />
                    </span>
                </xsl:for-each>
            </p>
        </article>
    </xsl:template>   
</xsl:stylesheet>

关于html - 教程 5,新视角中的 cvc-elt.1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51118218/

相关文章:

html - 将 div 容器居中

javascript - 我不能在 html5/css3 中制作 div 或任何图像 100%

jquery - 仅将函数应用于一个 div 类,而不是全部

javascript - 为什么 CSS 过渡需要触发重排?

xml - XLS 到 CSV 转义

c# - 将 xmlWriter.WriteElementString 值转换为日期和时间

css - Div 包装器未将所有页面内容居中

javascript - if/else 语句显示/隐藏图像

css - 未定义方法 `attr' 为 nil :NilClass (NoMethodError)

java - STaX 解析器表现不同