我已經開始使用XSLT,並且遇到了一些問題。我已經設法使用一系列值選擇指令來輸出我的XML文檔,但是我自己寫XSLT模板時真的很掙扎。編寫XSLT模板
這裏是我的XML:
<?xml version="1.0" ?>
<?xml-stylesheet type="text/xsl" href="lecturers.xsl"?>
<lecturers>
<lecturer>
<name>
<title>Professor</title>
<first>Peter </first> <last>Quirk</last>
</name>
<teaching>
<course code="CO3070">XML and the Web</course>
<course code="CO3300"> Web Server Architectures</course>
</teaching>
<research>
The application of Web protocols to Biology
</research>
</lecturer>
<lecturer>
<name>
<title>Doctor</title>
<first>Brian </first> <last>Johnson</last>
</name>
<teaching>
<course code="CO9999">Computer Hacking</course>
<course code="CO3300"> Web Server Architectures</course>
</teaching>
<research>
Investigating the various complexities of Computer Hacking
</research>
</lecturer>
然後,這是我的XSL,因爲它目前爲:
<?xml version="1.0"?>
<xsl:stylesheet version='1.0' xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" version="4.0" />
<xsl:template match="/">
<html>
<head>
<title>XML Week 7</title>
</head>
<body>
<h1>Week 7: Lecturers file turned to XSL:Template</h1>
<table border="1">
<tr>
<th><b>Title</b></th>
<th><b>Name</b></th>
<th><b>Teaching</b></th>
<th><b>Research</b></th>
</tr>
<tr>
<td><xsl:value-of select="lecturers/lecturer/name/title" /></td>
<td><xsl:value-of select="lecturers/lecturer/name/first" /><xsl:text> </xsl:text><xsl:value-of select="lecturers/lecturer/name/last" /></td>
<td><xsl:value-of select="lecturers/lecturer/teaching/course" /> and <xsl:value-of select="(lecturers/lecturer/teaching/course)[2]" /></td>
<td><xsl:value-of select="lecturers/lecturer/research" /></td>
</tr>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
這確實輸出,我需要到一個表中的信息,但我已經被指示創建新的模板來容納講師元素,然後是該元素的子課程。我可能只是過於複雜的東西在我腦海中,但我不能讓它工作,每當我嘗試將模板應用到td的一個時,我只是在瀏覽器中出現解析錯誤。那麼,有沒有人有我的任何提示?將不勝感激,甚至一些基本的例子來解釋如何讓它在我的例子中工作將是非常棒的。乾杯。
好的,所以一旦我添加了模板match =「lecturer」,我是否需要更改td中選擇的值以使其成爲模板,還是我所需要的?我對這整個事情是如何工作感到困惑,對我們來說,這很糟糕。謝謝。 – Bic1245
您可以編寫一個模板來匹配特定類型的節點,例如名稱爲'lecturer'並帶有'match =「lecturer」'的元素節點,並在模板內部寫入相對於匹配節點的表達式作爲上下文節點。當然你需要使用'apply-templates'來確保節點得到處理。 –
好吧,對不起,是一個痛苦,但作爲一個例子。如果我希望使用模板顯示標題,我該如何處理它?請問像這樣的工作: '的 \t 的 \t的 xsl:template> –
Bic1245