2012-09-16 56 views
3

嗨,我的XML文檔和XSLT無法生成良好的HTML ...發生了什麼?XSLT不能正常工作

這基本上是XML文件,我已經驗證這與XML模式,但

當我使用XSLT文件,將其轉換成HTML文件,它只是生成課程目錄有一大堆文本的一個段落標題 。

什麼樣的問題我在這裏?

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

你爲什麼要調整XML命名空間的問題?對我的回答所做的更改與對示例XML所做的更改不匹配。 –

回答

4

你的問題是,所有的元素都在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> 
+0

也許有點晚了,但非常感謝! –