XSLT key()基础知识

时间:2012-03-19 17:38:16

标签: xml xslt

我有两个主要的xml元素:coursesCRNs

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

我正在尝试使用courseNum作为密钥,以便从课程中获取@name属性。

到目前为止我的代码是:

<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 个答案:

答案 0 :(得分: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>

[编辑] 我认为它确实有效,这里有一个更完整的示例,输入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>

所以关键是有效的。 如果您仍有问题,请编辑您的问题并向我们展示更多详细信息。