gpt4 book ai didi

XSLT 不工作

转载 作者:行者123 更新时间:2023-12-05 01:21:17 26 4
gpt4 key购买 nike

嗨,我的 XML 文档和 XSLT 无法生成良好的 HTML……这是怎么回事?

这基本上是 XML 文件,我已经使用 XML 架构验证了这一点,但是

当我使用 XSLT 文件将其转换为 HTML 文件时,它只会生成类(class)目录标题
用一整段文字。

我这里有什么问题?

<?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>

谢谢

最佳答案

您的问题是所有元素都在 file://Volumes/u1234567/Assignment namespace 中,但在您的 XSLT 中,您的模板与没有 namespace 的元素匹配。

如果您仔细查看 <catalogue>,您会看到一个没有前缀的命名空间声明。 <catalogue xmlns="file://Volumes/u1234567/Assignment" 所有后代元素都继承该命名空间。

在 XSLT 中使用前缀定义该 namespace ,然后更改引用这些元素的位置以使用该 namespace 前缀:

<?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>

关于XSLT 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12444530/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com