2012-03-19 54 views
0

我有兩個主要的XML元素:coursesCRNsXSLT鍵()基本

<courses> 
    <course credits="3" courseNum="COMP1950" name="Intermediate Web Development and Design" url="http://www.bcit.ca/study/outlines/comp1950"> 
    <prereqs> 
     <prereq courseNum="COMP1850"/> 
    </prereqs> 
    </course> 
    <course credits="1" courseNum="COMP1854" name="Content Management with Drupal" url="http://www.bcit.ca/study/outlines/comp1854"> 
    <prereqs> 
     <prereq courseNum="COMP1854"/> 
    </prereqs> 
    </course> 
    <course credits="3" courseNum="COMP1920" name="Server-side Web Scripting with PHP Level 1" url="http://www.bcit.ca/study/outlines/comp1920"> 
    <prereqs> 
     <prereq courseNum="COMP1850"/> 
    </prereqs> 
    </course> 
</courses> 

<CRNs> 
    <CRN crn="78645" courseNum="COMP4625" totalWeeks="12" startDate="Jan16" endDate="Apr02" cost="489.00" instructor="Arron Ferguson" term="201210"/> 
    <CRN crn="47515" courseNum="COMP1002" totalWeeks="12" startDate="Jan09" endDate="Mar26" cost="109.00" instructor="Michael Evans" term="201210"/> 
    <CRN crn="47515" courseNum="COMP1002" totalWeeks="12" startDate="Jan09" endDate="Mar26" cost="109.00" instructor="Michael Evans" term="201210"/> 
    <CRN crn="47514" courseNum="COMP1002" totalWeeks="12" startDate="Jan10" endDate="Mar2702" cost="109.00" instructor="Fraser Robertson" term="201210"/> 
    <CRN crn="47513" courseNum="COMP1002" totalWeeks="6" startDate="Jan11" endDate="Apr04" cost="109.00" instructor="Michael Evans" term="201210"/> 
    <CRN crn="49033" courseNum="COMP2899" totalWeeks="12" startDate="Jan10" endDate="Apr03" cost="489.00" instructor="Arron Ferguson" term="201210"/> 
    <CRN crn="49029" courseNum="COMP1920" totalWeeks="12" startDate="Jan12" endDate="Apr05" cost="510.00" instructor="Jason Harrison" term="201210"/> 
    <CRN crn="54151" courseNum="COMP1920" totalWeeks="12" startDate="Feb22" endDate="Apr02" cost="520.00" instructor="Jason Harrison" term="201210"/> 
    <CRN crn="60270" courseNum="COMP1854" totalWeeks="2" startDate="Jun10" endDate="Jun17" cost="197.00" instructor="Jason Harrison" term="201220"/> 
    <CRN crn="60271" courseNum="COMP1854" totalWeeks="2" startDate="Jun09" endDate="Jun16" cost="197.00" instructor="Jason Harrison" term="201220"/> 
</CRNs> 

我想,以獲得從課程@name屬性使用courseNum作爲重點。

到目前爲止我的代碼是:

<xsl:for-each select="CRNs/CRN[@term='201220']"> 
    <xsl:sort select="./@courseNum" order="ascending" data-type="text"/> 
    <tr> 
     <td> <xsl:value-of select="./@courseNum"/></td> 
     <td> 
     <a target="_blank" href="{./@url}"> 
      <!--How do I use the key() function here in order to obtain the @name from the course element?--> 
      <!--I am also trying to obtain the @url (also from the course element) in this case--> 
     </a> 
    </tr> 
</xsl:for-each> 

回答

1

使用

<xsl:key name="c-by-n" match="courses/course" use="@courseNum"/> 

xsl:stylesheet元素的子元素,然後裏面的for-each你可以做

<xsl:variable name="course" select="key('c-by-n', @courseNum)"/> 

<a href="{$course/@url}"> 
    <xsl:value-of select="$course/@name"/> 
</a> 

[編輯] 我認爲它工作,這裏是一個更完整的TE樣品,與所述輸入XML是

<root> 
<courses> 
    <course credits="3" courseNum="COMP1950" name="Intermediate Web Development and Design" url="http://www.bcit.ca/study/outlines/comp1950"> 
    <prereqs> 
     <prereq courseNum="COMP1850"/> 
    </prereqs> 
    </course> 
    <course credits="1" courseNum="COMP1854" name="Content Management with Drupal" url="http://www.bcit.ca/study/outlines/comp1854"> 
    <prereqs> 
     <prereq courseNum="COMP1854"/> 
    </prereqs> 
    </course> 
    <course credits="3" courseNum="COMP1920" name="Server-side Web Scripting with PHP Level 1" url="http://www.bcit.ca/study/outlines/comp1920"> 
    <prereqs> 
     <prereq courseNum="COMP1850"/> 
    </prereqs> 
    </course> 
</courses> 

<CRNs> 
    <CRN crn="78645" courseNum="COMP4625" totalWeeks="12" startDate="Jan16" endDate="Apr02" cost="489.00" instructor="Arron Ferguson" term="201210"/> 
    <CRN crn="47515" courseNum="COMP1002" totalWeeks="12" startDate="Jan09" endDate="Mar26" cost="109.00" instructor="Michael Evans" term="201210"/> 
    <CRN crn="47515" courseNum="COMP1002" totalWeeks="12" startDate="Jan09" endDate="Mar26" cost="109.00" instructor="Michael Evans" term="201210"/> 
    <CRN crn="47514" courseNum="COMP1002" totalWeeks="12" startDate="Jan10" endDate="Mar2702" cost="109.00" instructor="Fraser Robertson" term="201210"/> 
    <CRN crn="47513" courseNum="COMP1002" totalWeeks="6" startDate="Jan11" endDate="Apr04" cost="109.00" instructor="Michael Evans" term="201210"/> 
    <CRN crn="49033" courseNum="COMP2899" totalWeeks="12" startDate="Jan10" endDate="Apr03" cost="489.00" instructor="Arron Ferguson" term="201210"/> 
    <CRN crn="49029" courseNum="COMP1920" totalWeeks="12" startDate="Jan12" endDate="Apr05" cost="510.00" instructor="Jason Harrison" term="201210"/> 
    <CRN crn="54151" courseNum="COMP1920" totalWeeks="12" startDate="Feb22" endDate="Apr02" cost="520.00" instructor="Jason Harrison" term="201210"/> 
    <CRN crn="60270" courseNum="COMP1854" totalWeeks="2" startDate="Jun10" endDate="Jun17" cost="197.00" instructor="Jason Harrison" term="201220"/> 
    <CRN crn="60271" courseNum="COMP1854" totalWeeks="2" startDate="Jun09" endDate="Jun16" cost="197.00" instructor="Jason Harrison" term="201220"/> 
</CRNs> 

</root> 

最小樣式表

<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 

    version="1.0"> 

    <xsl:output method="html" indent="yes"/> 

    <xsl:key name="c-by-n" match="courses/course" use="@courseNum"/> 

    <xsl:template match="root"> 
    <xsl:for-each select="CRNs/CRN[@term='201220']"> 
     <xsl:sort select="./@courseNum" order="ascending" data-type="text"/> 
     <tr> 
      <td> <xsl:value-of select="./@courseNum"/></td> 
      <td> 
      <xsl:variable name="course" select="key('c-by-n', @courseNum)"/> 

      <a href="{$course/@url}"> 
       <xsl:value-of select="$course/@name"/> 
      </a> 
      </td> 
     </tr> 
    </xsl:for-each> 
    </xsl:template> 

</xsl:stylesheet> 

輸出

<tr> 
    <td>COMP1854</td> 
    <td><a href="http://www.bcit.ca/study/outlines/comp1854">Content Management with Drupal</a></td> 
</tr> 
<tr> 
    <td>COMP1854</td> 
    <td><a href="http://www.bcit.ca/study/outlines/comp1854">Content Management with Drupal</a></td> 
</tr> 

所以關鍵作品。 如果您仍有問題,請編輯您的問題並向我們展示更多詳細信息。

+0

似乎沒有工作... – iggy2012 2012-03-19 18:04:08

+0

啊是的。我試圖運行它沒有變量「課程」,出於某種原因,它的工作。 – iggy2012 2012-03-19 18:30:19

+0

非常感謝馬丁。這非常有教育意義並且有幫助。你是一個紳士和學者。有一張支票。 – iggy2012 2012-03-19 18:30:57