2014-04-08 149 views
0

我有這個基於網站信息創建的xml文件。由於包含大量信息,我將發佈簡化版本。每個循環的XSLT/XML嵌套

這意味着使用xslt文件來顯示和模仿這樣的大學或大學課程的例子: 有多個課程要顯示,所以這就是爲什麼它開始於課程,然後我們有課程存儲每門課程。

這裏有像老師和其他描述的課程信息。然後我們進入年度,課程可以有多少年,這就是爲什麼我們把年度年份設置爲「1」,每年有兩個學期。在學期內有多個單位,所以我們把單位/單位存放。

這是我希望它看起來像給你一個想法的目標輸出。

我已經存儲在XML中的所有信息,這是我想通過XSLT來實現,但我不能確定如何開始它在這個層面上的輸出:http://puu.sh/80sBi/0107a6264e.png

這裏是我的代碼:

<?xml version="1.0" encoding="UTF-8"?> 
<?xml-stylesheet type="text/xsl" href="Courses.xslt"?> 

<courses> 
    <course>computing 
    <course_info></course_info> 
    <teacher etc> 
     <details></details> 
    </teacher etc> 

    <year ynum="1"> 
    <semester snum="1"> 
    <units> 
    <unit> 
     <code></code> 
     <title></title>  <!-- this is the "description" table title --> 
     <credit_points></credit_points> 
    </unit> 
    <unit> 
     <code></code> 
     <title></title> 
     <credit_points></credit_points> 
    </unit> 
    </units> 
</semester> 
<semester snum="2"> 
    <units> 
    <unit> 
     <code></code> 
     <title></title> 
     <credit_points></credit_points> 
    </unit> 
</units> 
</semester> 
</year> 
<year ynum="2"> 
    <semester snum="1"> 
    <units> 
     <unit> 
     <code></code> 
     <title></title> 
     <credit_points></credit_points> 
     </unit> 
    </units> 
    </semester> 
    <semester snum="2"> 
    <units> 
     <unit> 
     </unit> 
    </units> 
    </semester> 
    </year> 
    </course> 
</courses> 

我在xslt文件中失敗的嘗試:/任何建議或幫助將不勝感激,因爲我似乎無法包裝我的頭如何讓這些循環工作的例子。

<?xml version="1.0" encoding="ISO-8859-1"?> 
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:template match="/"> 

<html> 
<body> 
<h2>Course information</h2> 



<xsl:for-each select="courses/course"> 
<strong>Course:</strong><xsl:value-of select="course"/><br /> 
    <strong>Code:</strong><xsl:value-of select="code"/><br /> 
    <strong>Course Coordinaotr</strong><xsl:value-of select="fname"/><xsl:text> </xsl:text><xsl:value-of select="sname"/></li> 



    <table border="0"> 
    <tr> 
    <th>unit</th> 
    <th>description</th> 
    <th>Credit Points</th> 
    </tr> 

    <xsl:for-each select="year/semester"> 
    <tr> 
    <td><strong>Client ID:</strong><xsl:value-of select="unit_code"/></td> 
    <td><strong>OrderDate:</strong><xsl:value-of select="unit_title"/></td> 
    <td><strong>Quantity Ordered:</strong><xsl:value-of select="cp"/></td> 
    <td><strong>Order Status:</strong><xsl:value-of select="additional_link"/></td> 

    </xsl:for-each> 
    </tr> 


    </table> 
</xsl:for-each> 
</body> 
</html> 
</xsl:template></xsl:stylesheet> 

我該如何解決這個問題?

+0

我希望有人可以共享一些資源或處理類似上述的數據轉換成這樣的HTTP將XML XSLT的例子:// PUU .sh/80sBi/0107a6264e.png –

回答

0

你可能會發現這是更好地使用XSL:申請模板在這種情況下,而不是的xsl:for-每個。模板匹配是XSLT的主要優勢之一。它可以減少縮進,並且可以使XSLT更加清潔。所以,與其做<xsl:for-each select="courses/course">,做到這一點

<xsl:apply-templates select="courses/course" /> 

你會再有一個模板匹配當然在那裏你會輸出任何主標頭信息,然後選擇元素

<xsl:template match="course"> 
    <strong>Course:</strong> <xsl:value-of select="text()" /><br /> 
    <xsl:apply-templates select="year" /> 
</xsl:template> 

模板會類似,雖然看起來你想每學期輸出年份信息。因此,你的範本看起來像這樣

<xsl:template match="year"> 
    <xsl:apply-templates select="semester" /> 
</xsl:template> 

然後是學期模板就開始尋找這本

<xsl:template match="semester"> 
    <strong>Year:</strong><xsl:value-of select="../@ynum"/><br /> 
    <strong>Semester:</strong><xsl:value-of select="@snum"/><br /> 

(嚴格地說,你很可能跌落模板在這種情況下只需要<xsl:apply-templates select="year/semester" />)。

然後它只是一個以類似的方式爲表格行選擇單元元素的情況。

試試這個XSLT作爲首發:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="/"> 
     <html> 
     <body> 
     <h2>Course information</h2> 
     <xsl:apply-templates select="courses/course" /> 
     </body> 
     </html> 
    </xsl:template> 

    <xsl:template match="course"> 
     <strong>Course:</strong> <xsl:value-of select="text()" /><br /> 
     <xsl:apply-templates select="year" /> 
    </xsl:template> 

    <xsl:template match="year"> 
     <xsl:apply-templates select="semester" /> 
    </xsl:template> 

    <xsl:template match="semester"> 
     <strong>Year:</strong><xsl:value-of select="../@ynum"/><br /> 
     <strong>Semester:</strong><xsl:value-of select="@snum"/><br /> 
     <table border="0"> 
      <tr> 
       <th>unit</th> 
       <th>description</th> 
       <th>Credit Points</th> 
      </tr> 
      <xsl:apply-templates select="units/unit" /> 
     </table> 
    </xsl:template> 

    <xsl:template match="unit"> 
     <tr> 
      <td><xsl:value-of select="code"/></td> 
      <td><xsl:value-of select="title"/></td> 
      <td><xsl:value-of select="credit_points"/></td> 
     </tr> 
    </xsl:template> 
</xsl:stylesheet> 
+0

你是一個傳奇!非常感謝!我們最初被告知要遠離模板,因爲每個循環都是「更好」的,但從您的示例中,我已經能夠更好地理解它,併爲其添加了許多新功能。 再次非常感謝解釋和例子謝謝! –