Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

XSLT XML

Hi,

I am checking beow



https://www.tutorialspoint.com/xslt/xslt_key.htm

My XML and XSL code looks like
<?xml version = "1.0"?> 
<?xml-stylesheet type = "text/xsl" href = "students.xsl"?> 
<class> 
   <student rollno = "393">
      <firstname>Dinkar</firstname> 
      <lastname>Kad</lastname> 
      <nickname>Dinkar</nickname> 
      <marks>85</marks>
   </student> 
   <student rollno = "493"> 
      <firstname>Vaneet</firstname> 
      <lastname>Gupta</lastname> 
      <nickname>Vinni</nickname> 
      <marks>95</marks>
   </student> 
   <student rollno = "593"> 
      <firstname>Jasvir</firstname> 
      <lastname>Singh</lastname> 
      <nickname>Jazz</nickname> 
      <marks>90</marks> 
   </student> 
</class>

Open in new window


<?xml version = "1.0" encoding = "UTF-8"?>

<!-- xsl stylesheet declaration with xsl namespace: 
Namespace tells the xlst processor about which 
element is to be processed and which is used for output purpose only 
--> 

<xsl:stylesheet version = "1.0" 
xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">  
 
<!-- xsl template declaration:  
template tells the xlst processor about the section of xml 
document which is to be formatted. It takes an XPath expression. 
In our case, it is matching document root element and will 
tell processor to process the entire document with this template. 
--> 

   <xsl:template match = "/"> 
      <!-- HTML tags 
         Used for formatting purpose. Processor will skip them and browser 
            will simply render them. 
      --> 
		
      <html> 
         <body> 
            <h2>Students</h2> 
				
            <table border = "1"> 
               <tr bgcolor = "#9acd32"> 
                  <th>Roll No</th> 
                  <th>First Name</th> 
                  <th>Last Name</th> 
                  <th>Nick Name</th> 
                  <th>Marks</th> 
                  <th>Grade</th> 
               </tr> 
				
               <!-- for-each processing instruction 
               Looks for each element matching the XPath expression 
               --> 
				
               <xsl:for-each select="class/student"> 
               <xsl:if test = "marks > 90"> 
                  <tr> 
                     <td> 
                        <!-- value-of processing instruction 
                        process the value of the element matching the XPath expression 
                        --> 
                        <xsl:value-of select = "@rollno"/> 
                     </td> 
						
                     <td><xsl:value-of select = "firstname"/></td> 
                     <td><xsl:value-of select = "lastname"/></td> 
                     <td><xsl:value-of select = "nickname"/></td> 
                     <td><xsl:value-of select = "marks"/></td> 
 <td> 
                        <xsl:choose> 
                           <xsl:when test = "marks > 90"> 
                              High 
                           </xsl:when> 
									
                           <xsl:when test = "marks > 85"> 
                              Medium 
                           </xsl:when> 
									
                           <xsl:otherwise> 
                              Low 
                           </xsl:otherwise> 
                        </xsl:choose> 
                     </td> 
						
                  </tr> 
               </xsl:for-each> 
					
            </table> 
         </body> 
      </html> 
   </xsl:template>  
</xsl:stylesheet>

Open in new window


i wonder why we got only one Row with roll number 393?
please advise
ASKER CERTIFIED SOLUTION
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

my code as below not working. i want to see all three students but not working

<?xml version = "1.0"?> 
<?xml-stylesheet type = "text/xsl" href = "studentsBefore.xsl"?> 
<class> 
   <student rollno = "393">
      <firstname>Dinkar</firstname> 
      <lastname>Kad</lastname> 
      <nickname>Dinkar</nickname> 
      <marks>85</marks>
   </student> 
   <student rollno = "493"> 
      <firstname>Vaneet</firstname> 
      <lastname>Gupta</lastname> 
      <nickname>Vinni</nickname> 
      <marks>95</marks>
   </student> 
   <student rollno = "593"> 
      <firstname>Jasvir</firstname> 
      <lastname>Singh</lastname> 
      <nickname>Jazz</nickname> 
      <marks>90</marks> 
   </student> 
</class>

Open in new window


<?xml version = "1.0" encoding = "UTF-8"?>

<!-- xsl stylesheet declaration with xsl namespace: 
Namespace tells the xlst processor about which 
element is to be processed and which is used for output purpose only 
--> 

<xsl:stylesheet version = "1.0" 
xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">  
 
<!-- xsl template declaration:  
template tells the xlst processor about the section of xml 
document which is to be formatted. It takes an XPath expression. 
In our case, it is matching document root element and will 
tell processor to process the entire document with this template. 
--> 

   <xsl:template match = "/"> 
      <!-- HTML tags 
         Used for formatting purpose. Processor will skip them and browser 
            will simply render them. 
      --> 
		
      <html> 
         <body> 
            <h2>Students</h2> 
				
            <table border = "1"> 
               <tr bgcolor = "#9acd32"> 
                  <th>Roll No</th> 
                  <th>First Name</th> 
                  <th>Last Name</th> 
                  <th>Nick Name</th> 
                  <th>Marks</th> 
                  <th>Grade</th> 
               </tr> 
				
               <!-- for-each processing instruction 
               Looks for each element matching the XPath expression 
               --> 
				
               <xsl:for-each select="class/student"> 
               <xsl:if test = "marks > 90"> 
                  <tr> 
                     <td> 
                        <!-- value-of processing instruction 
                        process the value of the element matching the XPath expression 
                        --> 
                        <xsl:value-of select = "@rollno"/> 
                     </td> 
						
                     <td><xsl:value-of select = "firstname"/></td> 
                     <td><xsl:value-of select = "lastname"/></td> 
                     <td><xsl:value-of select = "nickname"/></td> 
                     <td><xsl:value-of select = "marks"/></td> 
 <td> 
                        <xsl:choose> 
                           <xsl:when test = "marks > 90"> 
                              High 
                           </xsl:when> 
									
                           <xsl:when test = "marks > 85"> 
                              Medium 
                           </xsl:when> 
									
                           <xsl:otherwise> 
                              Low 
                           </xsl:otherwise> 
                        </xsl:choose> 
                     </td>						
                  </tr> 
                  
               </xsl:for-each> 
					
            </table> 
         </body> 
      </html> 
   </xsl:template>  
</xsl:stylesheet>

Open in new window


please advise
When you do XSLT development, please look for a tool that helps you to at least verify that the XSLT is wellformed
You keep sending code that has missing end tags (as the xsl:if is not closed in this example)
It will not work if the XSLT doesn't even parse

If you drop the xsl:if (as suggested way earlier and working code has been posted) it will work

<?xml version = "1.0" encoding = "UTF-8"?>

<!-- xsl stylesheet declaration with xsl namespace: 
Namespace tells the xlst processor about which 
element is to be processed and which is used for output purpose only 
--> 

<xsl:stylesheet version = "1.0" 
    xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">  
    
    <!-- xsl template declaration:  
template tells the xlst processor about the section of xml 
document which is to be formatted. It takes an XPath expression. 
In our case, it is matching document root element and will 
tell processor to process the entire document with this template. 
--> 
    
    <xsl:template match = "/"> 
        <!-- HTML tags 
         Used for formatting purpose. Processor will skip them and browser 
            will simply render them. 
      --> 
        
        <html> 
            <body> 
                <h2>Students</h2> 
                
                <table border = "1"> 
                    <tr bgcolor = "#9acd32"> 
                        <th>Roll No</th> 
                        <th>First Name</th> 
                        <th>Last Name</th> 
                        <th>Nick Name</th> 
                        <th>Marks</th> 
                        <th>Grade</th> 
                    </tr> 
                    
                    <!-- for-each processing instruction 
               Looks for each element matching the XPath expression 
               --> 
                    
                    <xsl:for-each select="class/student"> 
                            <tr> 
                                <td> 
                                    <!-- value-of processing instruction 
                        process the value of the element matching the XPath expression 
                        --> 
                                    <xsl:value-of select = "@rollno"/> 
                                </td> 
                                
                                <td><xsl:value-of select = "firstname"/></td> 
                                <td><xsl:value-of select = "lastname"/></td> 
                                <td><xsl:value-of select = "nickname"/></td> 
                                <td><xsl:value-of select = "marks"/></td> 
                                <td> 
                                    <xsl:choose> 
                                        <xsl:when test = "marks > 90"> 
                                            High 
                                        </xsl:when> 
                                        
                                        <xsl:when test = "marks > 85"> 
                                            Medium 
                                        </xsl:when> 
                                        
                                        <xsl:otherwise> 
                                            Low 
                                        </xsl:otherwise> 
                                    </xsl:choose> 
                                </td>						
                            </tr> 
                            
                    </xsl:for-each> 
                    
                </table> 
            </body> 
        </html> 
    </xsl:template>  
</xsl:stylesheet>

Open in new window

Avatar of gudii9

ASKER

When you do XSLT development, please look for a tool that helps you to at least verify that the XSLT is wellformed

any good open source free tool for this?