2012-12-03 133 views
2

我已經開始使用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>&#x20;</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的一個時,我只是在瀏覽器中出現解析錯誤。那麼,有沒有人有我的任何提示?將不勝感激,甚至一些基本的例子來解釋如何讓它在我的例子中工作將是非常棒的。乾杯。

回答

2

使用一個模板創建結果文檔結構例如

<xsl:template match="/"> 
<html> 
<head> 
<title>XML Week 7</title> 
</head> 

    <body> 
    <xsl:apply-templates/> 

</body> 

</html> 
</xsl:template> 

然後寫入更多模板以創建內容,例如,

<xsl:template match="lecturers"> 
    <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> 
    <xsl:apply-templates/> 
</table> 
</xsl:template> 

<xsl:template match="lecturer"> 
<tr> 

    <td><xsl:value-of select="name/title" /></td> 
    <td><xsl:value-of select="name/first" /><xsl:text>&#x20;</xsl:text><xsl:value-of select="name/last" /></td> 
    <td><xsl:value-of select="teaching/course" /> and <xsl:value-of select="(teaching/course)[2]" /></td> 
    <td><xsl:value-of select="lecturers/lecturer/research" /></td> 

</tr> 
</xsl:template> 

正如你看到的,模板做一個apply-templates跟上的處理。

[編輯] 在回答您的意見,如果你想用更多的模板對不同的孩子或lecturer元素的後代元素,你可以使用

<xsl:template match="lecturer"> 
    <tr> 
    <xsl:apply-templates/> 
    </tr> 
</xsl:template> 

,然後寫模板元素如

<xsl:template match="name/title | research"> 
    <td> 
    <xsl:value-of select="."/> 
    </td> 
</xsl:template> 

<xsl:template match="name/first"> 
    <td> 
    <xsl:value-of select="concat(., ' ', ../last)"/> 
    </td> 
</xsl:template> 

<!-- don't output name/last as the name/first template already does --> 
<xsl:template match="name/last"/> 

<xsl:template match="teaching"> 
    <xsl:apply-templates select="course"/> 
</xsl:template> 

<xsl:template match="course"> 
    <xsl:if test="position() > 1"><xsl:text> and </xsl:text></xsl:if> 
    <xsl:value-of select="."/> 
</xsl:template> 
+0

好的,所以一旦我添加了模板match =「lecturer」,我是否需要更改td中選擇的值以使其成爲模板,還是我所需要的?我對這整個事情是如何工作感到困惑,對我們來說,這很糟糕。謝謝。 – Bic1245

+1

您可以編寫一個模板來匹配特定類型的節點,例如名稱爲'lecturer'並帶有'match =「lecturer」'的元素節點,並在模板內部寫入相對於匹配節點的表達式作爲上下文節點。當然你需要使用'apply-templates'來確保節點得到處理。 –

+0

好吧,對不起,是一個痛苦,但作爲一個例子。如果我希望使用模板顯示標題,我該如何處理它?請問像這樣的工作: '的 \t ​​的 \t的 Bic1245

1

如果你currenly有:

<tr> 
    <td><xsl:value-of select="lecturers/lecturer/name/title" /></td> 
    <td><xsl:value-of select="lecturers/lecturer/name/first" /><xsl:text>&#x20;</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> 

你會希望有apply-templates一個電話,有一個lecturer模板。

<xsl:template match="lecturer"> 
    <tr> 

     <td><xsl:value-of select="name/title" /></td> 
     <td><xsl:value-of select="name/first" /><xsl:text>&#x20;</xsl:text><xsl:value-of select="name/last" /></td> 
     <td><xsl:value-of select="teaching/course" /> and <xsl:value-of select="(teaching/course)[2]" /></td> 
     <td><xsl:value-of select="research" /></td> 

    </tr> 
</xsl:template>