您的XML文档和XSLT无法生成良好的HTML ...发生了什么?
这基本上是XML文件,我已经使用XML Schema验证了这一点,但是
当我使用XSLT文件将其转换为HTML文件时,它只生成Courses Catalog标题 用一整段文字。
我有什么问题吗?
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- Generate HTML output -->
<xsl:output method="html"/>
<!-- The root template is defined here -->
<xsl:template match="/">
<html>
<head>
<title>Courses Catalogue</title>
</head>
<body>
<h2>Courses catalogue</h2>
<xsl:apply-templates />
</body>
</html>
</xsl:template>
<xsl:template match="course">
<p>
<xsl:apply-templates select="code" />
<xsl:apply-templates select="title" />
<xsl:apply-templates select="year" />
<xsl:apply-templates select="science" />
<xsl:apply-templates select="area" />
<xsl:apply-templates select="subject" />
<xsl:apply-templates select="updated" />
<xsl:apply-templates select="unit" />
<xsl:apply-templates select="description" />
<xsl:apply-templates select="outcomes" />
<xsl:apply-templates select="incompatibility" />
</p>
</xsl:template>
<xsl:template match="code">
Course Code: <span style="color:#C66">
<xsl:value-of select="." /> </span>
<br />
</xsl:template>
<xsl:template match="title">
Course Title: <span style="color:#000">
<xsl:value-of select="." /> </span>
<br />
</xsl:template>
<xsl:template match="year">
Student Year: <span style="color:#C66">
<xsl:value-of select="." /> </span>
<br />
</xsl:template>
<xsl:template match="science">
Science Group: <span style="color:#C66">
<xsl:value-of select="." /> </span>
<br />
</xsl:template>
<xsl:template match="area">
Area: <span style="color:#C66">
<xsl:value-of select="." /> </span>
<br />
</xsl:template>
<xsl:template match="subject">
Course Subject: <span style="color:#C66">
<xsl:value-of select="." /> </span>
<br />
</xsl:template>
<xsl:template match="updated">
Page was updated in: <span style="color:#C66">
<xsl:value-of select="." /> </span>
<br />
</xsl:template>
<xsl:template match="unit">
Unit: <span style="color:#C66">
<xsl:value-of select="." /> </span>
<br />
</xsl:template>
<xsl:template match="description">
Course Description: <span style="color:#C66">
<xsl:value-of select="." /> </span>
<br />
</xsl:template>
<xsl:template match="outcomes">
Course Outcomes: <span style="color:#C66">
<xsl:value-of select="." /> </span>
<br />
</xsl:template>
< xsl:template match="incompatibility">
Incompatible courses: <span style="color:#C66">
<xsl:value-of select="." /> </span>
<br />
</xsl:template>
</xsl:stylesheet>
和我的XML文件
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl"
href="courses.xsl"?>
<!--catalogue xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="catalogue.xsd"-->
<catalogue xmlns="file://"
xmlns:xsi="http:/e"
xsi:schemaLocation="file://">
<course>
<code>COMP3410</code>
<title> Information Technology in Electronic Commerce </title>
<year>later year</year>
<science>C</science>
<area> Research School of Computer Science </area>
<subject> Computer Science </subject>
<updated>2012-03-13T13:12:00</updated>
<unit>6</unit>
由于
答案 0 :(得分:5)
您的问题是所有元素都在file://Volumes/u1234567/Assignment
命名空间中,但在您的XSLT中,您的模板与没有命名空间的元素匹配。
如果仔细查看<catalogue>
,您将看到没有前缀的名称空间声明。 <catalogue xmlns="file://Volumes/u1234567/Assignment"
所有后代元素都继承该命名空间。
在XSLT中使用前缀定义该命名空间,然后更改引用这些元素的位置以使用该命名空间前缀:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:a="file://Volumes/u1234567/Assignment">
<!-- Generate HTML output -->
<xsl:output method="html"/>
<!-- The root template is defined here -->
<xsl:template match="/">
<html>
<head>
<title>Courses Catalogue</title>
</head>
<body>
<h2>Courses catalogue</h2>
<xsl:apply-templates />
</body>
</html>
</xsl:template>
<xsl:template match="a:course">
<p>
<xsl:apply-templates select="a:code" />
<xsl:apply-templates select="a:title" />
<xsl:apply-templates select="a:year" />
<xsl:apply-templates select="a:science" />
<xsl:apply-templates select="a:area" />
<xsl:apply-templates select="a:subject" />
<xsl:apply-templates select="a:updated" />
<xsl:apply-templates select="a:unit" />
<xsl:apply-templates select="a:description" />
<xsl:apply-templates select="a:outcomes" />
<xsl:apply-templates select="a:incompatibility" />
</p>
</xsl:template>
<xsl:template match="a:code">
Course Code:
<xsl:value-of select="." />
<br />
</xsl:template>
<xsl:template match="a:title">
Course Title:
<xsl:value-of select="." />
<br />
</xsl:template>
<xsl:template match="a:year">
Student Year: <span style="color:#C66">
<xsl:value-of select="." /> </span>
<br />
</xsl:template>
<xsl:template match="a:science">
Science Group:
<xsl:value-of select="." />
<br />
</xsl:template>
<xsl:template match="a:area">
Area:
<xsl:value-of select="." />
<br />
</xsl:template>
<xsl:template match="a:subject">
Course Subject:
<xsl:value-of select="." />
<br />
</xsl:template>
<xsl:template match="a:updated">
Page was updated in:
<xsl:value-of select="." />
<br />
</xsl:template>
<xsl:template match="a:unit">
Unit:
<xsl:value-of select="." />
<br />
</xsl:template>
<xsl:template match="a:description">
Course Description:
<xsl:value-of select="." />
<br />
</xsl:template>
<xsl:template match="a:outcomes">
Course Outcomes:
<xsl:value-of select="." />
<br />
</xsl:template>
<xsl:template match="a:incompatibility">
Incompatible courses:
<xsl:value-of select="." />
<br />
</xsl:template>
</xsl:stylesheet>