Solved

Print elements by element_order_no tag?

Posted on 2008-06-25
5
218 Views
Last Modified: 2013-11-18
Lets say i have this xml structure:

<golfers>
   <golfer element_order_no="2" skill="excellent" handicap="10" clubs="Taylor Made" id="1111">
         <name>
             <firstName>Heedy</firstName>
             <lastName>Wahlin</lastName>
         </name>
         <favoriteCourses>
             <course city="Pinetop" state="AZ" name="Pinetop Lakes CC"/>
             <course city="Phoenix" state="AZ" name="Ocotillo"/>
             <course city="Snowflake" state="AZ" name="Silver Creek"/>
         </favoriteCourses>
      </golfer>
   <golfer element_order_no="1" skill="moderate" handicap="12" clubs="Taylor Made" id="2222">
     <name>
       <firstName>Dan</firstName>
       <lastName>Wahlin</lastName>
       </name>
       <favoriteCourses>
            <course city="Pinetop" state="AZ" name="Pinetop Lakes CC"/>
            <course city="Pinetop" state="AZ" name="White Mountain CC"/>
            <course city="Springville" state="UT" name="Hobble Creek"/>
       </favoriteCourses>
      </golfer>
        <golfer element_order_no="4" skill="moderate" handicap="12" clubs="Taylor Made" id="2222">
     <name>
       <firstName>Dan</firstName>
       <lastName>Wahlin</lastName>
       </name>
       <favoriteCourses>
            <course city="Pinetop" state="AZ" name="Pinetop Lakes CC"/>
            <course city="Pinetop" state="AZ" name="White Mountain CC"/>
            <course city="Springville" state="UT" name="Hobble Creek"/>
       </favoriteCourses>
      </golfer>
        <golfer element_order_no="3" skill="moderate" handicap="12" clubs="Taylor Made" id="2222">
     <name>
       <firstName>Dan</firstName>
       <lastName>Wahlin</lastName>
       </name>
       <favoriteCourses>
            <course city="Pinetop" state="AZ" name="Pinetop Lakes CC"/>
            <course city="Pinetop" state="AZ" name="White Mountain CC"/>
            <course city="Springville" state="UT" name="Hobble Creek"/>
       </favoriteCourses>
      </golfer>
</golfers>

Here is the xslt file:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" method="html"/>
<xsl:template match="/">
      <html>
      <head>
            <style type="text/css">
                  .blackText {
                        font-family:arial;
                        color:#000000;
                  }
                  .largeYellowText {
                        font-family:arial;
                        font-size:18pt;
                        color:#ffff00;
                  }
                  .largeBlackText {
                        font-family:arial;
                        font-size:14pt;
                        color:#000000;
                  }
                  .borders {
                        border-left:1px solid #000000;
                        border-right:1px solid #000000;
                        border-top:1px solid #000000;
                        border-bottom:1px solid #000000;
                  }
            </style>
      </head>
      <body bgcolor="#ffffff">
      <span class="largeBlackText">
            <b>Golfers: </b>
      </span>
      <xsl:apply-templates/>
      </body>
      </html>
</xsl:template>
      
<xsl:template match="golfers">
<xsl:apply-templates/></xsl:template>

      <xsl:template match="golfer">
        <table class="borders" border="0" width="640" cellpadding="4" cellspacing="0" bgcolor="#efefef">
            <xsl:apply-templates select="name"/>
            <tr class="blackText">
                  <td width="12%" align="left">
                        <b>Skill: </b>
                  </td>
                  <td width="12%" align="left">
                        <xsl:value-of select="@skill"/>
                  </td>
                  <td width="12%" align="left">
                        <b>Handicap: </b>
                  </td>
                  <td width="12%" align="left">
                        <xsl:value-of select="@handicap"/>
                  </td>
                  <td width="12%" align="left">
                        <b>Clubs: </b>
                  </td>
                  <td width="40%" align="left">
                        <xsl:value-of select="@clubs"/>
                  </td>
            </tr>
            <tr>
                  <td colspan="6">&#xa0;</td>
            </tr>
            <tr class="blackText">
                  <td colspan="6" class="largeBlackText">Favorite Courses</td>
            </tr>
            <tr>
                  <td colspan="2"><b>City: </b></td>
                  <td colspan="2"><b>State: </b></td>
                  <td colspan="2"><b>Course: </b></td>
            </tr>
            <xsl:apply-templates select="favoriteCourses"/>
        </table>
      </xsl:template>
      
<xsl:template match="name">
      <tr>
            <td colspan="6" class="largeYellowText" bgcolor="#02027a">
                  <xsl:value-of select="firstName"/>&#xa0;
                  <xsl:value-of select="lastName"/>
            </td>
      </tr>
</xsl:template>
      
<xsl:template match="favoriteCourses">
      <xsl:apply-templates/></xsl:template>
      
      <xsl:template match="course">
            <tr class="blackText">
                  <td colspan="2" align="left">
                        <xsl:value-of select="@city"/>
                  </td>
                  <td colspan="2" align="left">
                        <xsl:value-of select="@state"/>
                  </td>
                  <td colspan="2" align="left">
                        <xsl:value-of select="@name"/>
                  </td>
            </tr>
      </xsl:template>
      
</xsl:stylesheet>

How to i show the elements ascending or descending by the element_order_no tag???

Thank you.
0
Comment
Question by:drcyrus3d
  • 3
  • 2
5 Comments
 
LVL 82

Accepted Solution

by:
hielo earned 500 total points
ID: 21869242
Instead of:
<xsl:template match="golfers">
<xsl:apply-templates/></xsl:template>


use:
<xsl:template match="golfers">
 <xsl:apply-templates>
  <xsl:sort select="@element_order_no"/>
 </xsl:apply-templates>
</xsl:template>
0
 
LVL 1

Author Closing Comment

by:drcyrus3d
ID: 31470719
Thank you!
0
 
LVL 1

Author Comment

by:drcyrus3d
ID: 21869319
is there a way i can choose between asc or desc? Cause right now is in asc mode. :)

Thanks.
0
 
LVL 82

Expert Comment

by:hielo
ID: 21869340
The syntax is:
<xsl:sort
select="expression"
lang="language-code"
data-type="text|number|qname"
order="ascending|descending"
case-order="upper-first|lower-first"/>

so use:
<xsl:template match="golfers">
 <xsl:apply-templates>
  <xsl:sort select="@element_order_no" order="descending"/>
 </xsl:apply-templates>
</xsl:template>
0
 
LVL 1

Author Comment

by:drcyrus3d
ID: 21869396
Thank you!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
jQuery or Javascript animation question 6 57
Create 4 <div> below each other in php 2 47
XSLT Assistance 9 44
Adjust the position 3 16
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now