Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7671475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:03:59+00:00 2026-05-31T16:03:59+00:00

I have two major xml elements: courses and CRNs <courses> <course credits=3 courseNum=COMP1950 name=Intermediate

  • 0

I have two major xml elements: courses and 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>

I am trying to use courseNum as a key in order to obtain the @name attribute from courses.

so far my code is:

<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 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T16:04:01+00:00Added an answer on May 31, 2026 at 4:04 pm

    Use

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

    as a child of the xsl:stylesheet element and then inside of the for-each you can do

    <xsl:variable name="course" select="key('c-by-n', @courseNum)"/>
    
    <a href="{$course/@url}">
      <xsl:value-of select="$course/@name"/>
    </a>
    

    [edit]
    I think it does work, here is a more complete sample, with the input XML being

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

    the minimal stylesheet

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

    outputs

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

    so the key works.
    If you still have problems then edit your question and show us more details.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I used pole display(E POS) in my POS c# application.I have two major problem
I have two applications written in Java that communicate with each other using XML
I have two elements: <input a> <input b onclick=...> When b is clicked, I
Are there any major differences between the two? I have yet to see a
I have two major classes in objective C using cocos2D, DebugZoneLayer and HeroClass. Using
I guess there are two major approaches to date representations in XML: <date>1984-10-27</date> and
Have two folders with approx. 150 java property files. In a shell script, how
I have two arrays of animals (for example). $array = array( array( 'id' =>
I have two arrays of System.Data.DataRow objects which I want to compare. The rows
I have two identical tables and need to copy rows from table to another.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.