XSLT 无法在 Web 浏览器中显示转换后的 XML

XSLT failing to display transformed XML in web browser

我在让我的 XSLT 文件显示其 XSLT 函数时遇到了问题,经过一些试验和 error/debugging 使用 Blueprint,看来我的问题与我的命名空间和 (可能)我的 XSD 文件。我和我的教授一起花了一个小时试图找到 bug/problem,但无济于事。

XML 文件

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

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

<movieRoot xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns="http://www.example.com/comicbooks/movies/ns"
           xsi:schemaLocation="http://www.example.com/comicbooks/movies/ns movies.xsd">    

    <movie>

        <movieTitle>Captain America: Civil War</movieTitle>
        <genre>Action, Adventure, Sci-Fi</genre>
        <rating>8.13</rating>
        <length>147 min</length>
        <releaseDate>May 6th, 2016</releaseDate>

     </movie>

</movieRoot>

XSD 文件

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

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns="http://www.example.com/comicbooks/movies/ns"
           targetNamespace="http://www.example.com/comicbooks/movies/ns">

    <xs:element name="movieRoot">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="movie" minOccurs="1" maxOccurs="unbounded" >
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="movieTitle" type="xs:string" />
                            <xs:element name="genre" type="xs:string" />
                            <xs:element name="rating" type="xs:string" />
                            <xs:element name="length" type="xs:string" />
                            <xs:element name="releaseDate" type="xs:string" />
                       </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
     </xs:element>

</xs:schema>

XSLT 文件

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

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


    <xsl:template match="/">

        <html>
        <head>
            <link rel="stylesheet" type="text/css" href="movies.css" />
        </head>

        <body>

            <h1><xsl:value-of select= "movieRoot/movie/releaseDate" /></h1>

        </body>
        </html>


     </xsl:template>

</xsl:stylesheet>

您的 XSLT 没有考虑 XML.

的默认命名空间
  1. 在您的 xsl:stylesheet 元素上声明名称空间前缀:

    xmlns:ns="http://www.example.com/comicbooks/movies/ns"
    
  2. 并更改 xsl:value-of 以使用新定义的命名空间 前缀:

    <xsl:value-of select= "ns:movieRoot/ns:movie/ns:releaseDate" />
    

然后您的 XML 将通过您的 XSLT 呈现,以将日期显示为 h1

备注:

  • XSD 无法显示从 XML 转换而来的 HTML。
  • 出于安全考虑,
  • Chrome 将不会加载本地生成的 XSLT。 Firefox 会。
  • 另见