Основы XSLT key () - PullRequest
       9

Основы XSLT key ()

0 голосов
/ 19 марта 2012

У меня есть два основных элемента xml: courses и CRNs

<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 Ответ

1 голос
/ 19 марта 2012

Используйте

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

, поэтому ключ работает.Если у вас все еще есть проблемы, отредактируйте ваш вопрос и покажите нам более подробную информацию.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...