#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    1
    Rep Power
    0

    Key function with two nodes and attributes


    I am trying to display the professor names with the classes they teach. At the moment, I just have the information displaying without it. I'm trying to use a key function.

    In my xsl file I've got


    Code:
    <?xml version="1.0"?>
    <!-- Brock Shelton
    	 Assignment 4
    	 inmajor.xsl 
         implements selection of all course grades of in major courses  
      -->
    	 
     <xsl:stylesheet version="1.0"
    		xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output method="html"/>
      <xsl:key name="profbyclass" match="prof" use="@instid"/>
     <xsl:template match="/" >
     <!-- starts processing from the root node -->
         <html>
           <body>    
                
                  <h1>In Major Courses Completed</h1>
              <xsl:for-each select="transcript/course[@maj='y']">
                      
                  <xsl:value-of select="dept"/> 
    			  <xsl:text> </xsl:text>
                      <xsl:value-of select="num"/>
    				  <xsl:text> - </xsl:text>
                  <xsl:value-of select="grade" /> 
    			  
    			  <xsl:for-each select="key('profbyclass', 'it1')">
    				<xsl:value-of select="prof/name" />
    			  </xsl:for-each>
    			  
    				<xsl:choose>
    					<xsl:when test="position() != last()">, </xsl:when>
    				</xsl:choose>
                
    				<xsl:choose>
    					<xsl:when test="position() mod 3 = 0"> <br/> </xsl:when>
    				</xsl:choose>
    			</xsl:for-each>
              
    		  
              <h1> Non-Major Courses Completed</h1>
    		  
              <xsl:for-each select="transcript/course[@maj = 'n' ]">
                      
                  <xsl:value-of select="dept"/>   
                      <xsl:value-of select="num"/> 
    				   <xsl:text> - </xsl:text>
                  <xsl:value-of select="grade" /> 
    			  <xsl:choose>
    				<xsl:when test="position() != last()">, </xsl:when>
    				</xsl:choose>
                
    			<xsl:choose>
    			<xsl:when test="position() mod 3 = 0">
    			<br/>
    			</xsl:when>
    			</xsl:choose>
                
              </xsl:for-each>
                
    		
           </body>
         </html>		  
      </xsl:template>
    </xsl:stylesheet>
    and my XML is:

    Code:
    <?xml version="1.0" ?>
    <!-- 
       inmajor.xml
       to be used unchanged (except for the XSLT file specified)
       in assignment 5
    -->
    <?xml-stylesheet type="text/xsl" href="inmajor.xsl"?> 
    <transcript>
       <course maj="y">
           <sem>Fall</sem>
           <yr>2008</yr>
          <dept>ITEC</dept>
          <num refinstid="it1">225</num>
          <grade>D </grade>
          <hrs >3</hrs>
       </course>
       <course maj="y">
          <sem>Fall</sem>
           <yr>2008</yr>
          <dept>ITEC</dept>
          <num refinstid="it2">220</num>
          <grade>C</grade>
          <hrs>4</hrs>
       </course>
       <course maj="y">
          <sem>Fall</sem>
           <yr>2008</yr>
          <dept>MATH</dept>
          <num>151</num>
          <grade>B</grade>
          <hrs>3</hrs>
       </course> 
        <course maj="n">
          <sem>Spring</sem>
           <yr>2008</yr>
          <dept>MGNT</dept>
          <num>351</num>
          <grade>C</grade>
          <hrs>3</hrs>
       </course>
       
       <course maj="n">
          <sem>Fall</sem>
           <yr>2008</yr>
          <dept>SOCY</dept>
          <num refinstid="so4">102</num>
          <grade>C</grade>
          <hrs>3</hrs>
       </course>  
       <course maj="y">
           <sem>Spring</sem>
           <yr>2009</yr>
          <dept>ITEC</dept>
          <num refinstid="it1">325</num>
          <grade>A</grade>
          <hrs>3</hrs>
       </course>
       <course maj="y">
          <sem>Spring</sem>
           <yr>2009</yr>
          <dept>ITEC</dept>
          <num refinstid="it4">224</num>
          <grade>C</grade>
          <hrs>3</hrs>
       </course>
       <course maj="y">
          <sem>Spring</sem>
           <yr>2009</yr>
          <dept>MATH</dept>
          <num refinstid="ma3">152</num>
          <grade>B</grade>
          <hrs>3</hrs>
       </course> 
        <course maj="n">
          <sem>Spring</sem>
           <yr>2009</yr>
          <dept>MKTG</dept>
          <num>361</num>
          <grade>B</grade>
          <hrs>3</hrs>
       </course>
       <course maj="n">
          <sem>Spring</sem>
           <yr>2009</yr>
          <dept>SOCY</dept>
          <num refinstid="so5">301</num>
          <grade>A</grade>
          <hrs>3</hrs>
       </course>
       <course maj="y">
          <sem>Spring</sem>
          <yr>2009</yr>
          <dept>ITEC</dept>
          <num refinstid="it3">340</num>
          <grade>B</grade>
          <hrs>3</hrs>
       </course>
      
       <prof instid="it1"> <name>Davis</name></prof>
       <prof instid="it2"><name>Chase</name></prof>
       <prof instid="ma3"><name>Warner</name></prof>
       <prof instid="it4"><name>Uppuluri</name></prof>
       <prof instid="so4"><name>Brooks</name></prof>
       <prof instid="it3"><name>Phillips</name></prof>
       <prof instid="so5"><name>Krutchner</name></prof>
    </transcript>
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2006
    Posts
    24
    Rep Power
    0
    xslt 2.0 for-each-group to group the maj=y|n then key the prof using instid

    did not work below into your html

    input.xml

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <transcript>
      <course maj="y">
        <sem>Fall</sem>
        <yr>2008</yr>
        <dept>ITEC</dept>
        <num refinstid="it1">225</num>
        <grade>D </grade>
        <hrs>3</hrs>
      </course>
      <course maj="y">
        <sem>Fall</sem>
        <yr>2008</yr>
        <dept>ITEC</dept>
        <num refinstid="it2">220</num>
        <grade>C</grade>
        <hrs>4</hrs>
      </course>
      <course maj="y">
        <sem>Fall</sem>
        <yr>2008</yr>
        <dept>MATH</dept>
        <num>151</num>
        <grade>B</grade>
        <hrs>3</hrs>
      </course>
      <course maj="n">
        <sem>Spring</sem>
        <yr>2008</yr>
        <dept>MGNT</dept>
        <num>351</num>
        <grade>C</grade>
        <hrs>3</hrs>
      </course>
      <course maj="n">
        <sem>Fall</sem>
        <yr>2008</yr>
        <dept>SOCY</dept>
        <num refinstid="so4">102</num>
        <grade>C</grade>
        <hrs>3</hrs>
      </course>
      <course maj="y">
        <sem>Spring</sem>
        <yr>2009</yr>
        <dept>ITEC</dept>
        <num refinstid="it1">325</num>
        <grade>A</grade>
        <hrs>3</hrs>
      </course>
      <course maj="y">
        <sem>Spring</sem>
        <yr>2009</yr>
        <dept>ITEC</dept>
        <num refinstid="it4">224</num>
        <grade>C</grade>
        <hrs>3</hrs>
      </course>
      <course maj="y">
        <sem>Spring</sem>
        <yr>2009</yr>
        <dept>MATH</dept>
        <num refinstid="ma3">152</num>
        <grade>B</grade>
        <hrs>3</hrs>
      </course>
      <course maj="n">
        <sem>Spring</sem>
        <yr>2009</yr>
        <dept>MKTG</dept>
        <num>361</num>
        <grade>B</grade>
        <hrs>3</hrs>
      </course>
      <course maj="n">
        <sem>Spring</sem>
        <yr>2009</yr>
        <dept>SOCY</dept>
        <num refinstid="so5">301</num>
        <grade>A</grade>
        <hrs>3</hrs>
      </course>
      <course maj="y">
        <sem>Spring</sem>
        <yr>2009</yr>
        <dept>ITEC</dept>
        <num refinstid="it3">340</num>
        <grade>B</grade>
        <hrs>3</hrs>
      </course>
      <prof instid="it1">
        <name>Davis</name>
      </prof>
      <prof instid="it2">
        <name>Chase</name>
      </prof>
      <prof instid="ma3">
        <name>Warner</name>
      </prof>
      <prof instid="it4">
        <name>Uppuluri</name>
      </prof>
      <prof instid="so4">
        <name>Brooks</name>
      </prof>
      <prof instid="it3">
        <name>Phillips</name>
      </prof>
      <prof instid="so5">
        <name>Krutchner</name>
      </prof>
    </transcript>
    xsl

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
    
    	<xsl:output method="xml" indent="yes"/>
    
    	<xsl:template match="/">
        <xsl:element name="root">
          <xsl:apply-templates select="node()|@*"/>
        </xsl:element>
    	</xsl:template>
    	
    	<xsl:template match="text()|@*"/>
    
    	<xsl:template match="transcript">
          <xsl:for-each-group select="course" group-by="@maj">      
            <xsl:element name="major">
              <xsl:attribute name="yn" select="@maj"/>
              <xsl:for-each select="current-group()">
                <xsl:element name="course">
                  <xsl:element name="prof">
                    <xsl:variable name="_prof-id_" select="num/@refinstid"/>
                    <xsl:value-of select="//prof[@instid=$_prof-id_]"/>
                  </xsl:element>
                  <xsl:apply-templates select="node()|@*"/>
                </xsl:element>
              </xsl:for-each>
            </xsl:element>
          </xsl:for-each-group>
    	</xsl:template>
    
      <xsl:template match="*">
        <xsl:copy-of select="."/>
      </xsl:template>
      
      <xsl:template match="prof"/>
      
    </xsl:stylesheet>
    result (note your xml does not have instid for courses 151, 351, 361)

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <root>
    	<major yn="y">
    		<course>
    			<prof>Davis</prof>
    			<sem>Fall</sem>
    			<yr>2008</yr>
    			<dept>ITEC</dept>
    			<num refinstid="it1">225</num>
    			<grade>D </grade>
    			<hrs>3</hrs>
    		</course>
    		<course>
    			<prof>Chase</prof>
    			<sem>Fall</sem>
    			<yr>2008</yr>
    			<dept>ITEC</dept>
    			<num refinstid="it2">220</num>
    			<grade>C</grade>
    			<hrs>4</hrs>
    		</course>
    		<course>
    			<prof></prof>
    			<sem>Fall</sem>
    			<yr>2008</yr>
    			<dept>MATH</dept>
    			<num>151</num>
    			<grade>B</grade>
    			<hrs>3</hrs>
    		</course>
    		<course>
    			<prof>Davis</prof>
    			<sem>Spring</sem>
    			<yr>2009</yr>
    			<dept>ITEC</dept>
    			<num refinstid="it1">325</num>
    			<grade>A</grade>
    			<hrs>3</hrs>
    		</course>
    		<course>
    			<prof>Uppuluri</prof>
    			<sem>Spring</sem>
    			<yr>2009</yr>
    			<dept>ITEC</dept>
    			<num refinstid="it4">224</num>
    			<grade>C</grade>
    			<hrs>3</hrs>
    		</course>
    		<course>
    			<prof>Warner</prof>
    			<sem>Spring</sem>
    			<yr>2009</yr>
    			<dept>MATH</dept>
    			<num refinstid="ma3">152</num>
    			<grade>B</grade>
    			<hrs>3</hrs>
    		</course>
    		<course>
    			<prof>Phillips</prof>
    			<sem>Spring</sem>
    			<yr>2009</yr>
    			<dept>ITEC</dept>
    			<num refinstid="it3">340</num>
    			<grade>B</grade>
    			<hrs>3</hrs>
    		</course>
    	</major>
    	<major yn="n">
    		<course>
    			<prof></prof>
    			<sem>Spring</sem>
    			<yr>2008</yr>
    			<dept>MGNT</dept>
    			<num>351</num>
    			<grade>C</grade>
    			<hrs>3</hrs>
    		</course>
    		<course>
    			<prof>Brooks</prof>
    			<sem>Fall</sem>
    			<yr>2008</yr>
    			<dept>SOCY</dept>
    			<num refinstid="so4">102</num>
    			<grade>C</grade>
    			<hrs>3</hrs>
    		</course>
    		<course>
    			<prof></prof>
    			<sem>Spring</sem>
    			<yr>2009</yr>
    			<dept>MKTG</dept>
    			<num>361</num>
    			<grade>B</grade>
    			<hrs>3</hrs>
    		</course>
    		<course>
    			<prof>Krutchner</prof>
    			<sem>Spring</sem>
    			<yr>2009</yr>
    			<dept>SOCY</dept>
    			<num refinstid="so5">301</num>
    			<grade>A</grade>
    			<hrs>3</hrs>
    		</course>
    	</major>
    </root>

IMN logo majestic logo threadwatch logo seochat tools logo