[xsl] How can I improve this

Subject: [xsl] How can I improve this
From: "Walter Torres" <walter@xxxxxxxxx>
Date: Thu, 8 Nov 2001 18:16:51 -0600
I am in a bit of a quandary and I hope you learned people can help.

Earlier this year, many of you helped me (thanks again!) solve problems and
clarify many points as I tried to create a config driven XSLT to display
data in an HTML table from an XML data file.

The solution I created builds a table, a user can sort and search for an
case-insensitive (sub)string in any column.

Add items to the 'config' file and these new items show up in the table.

Remove or change item order, and the table is changed!

All is right as rain. Well, almost...

I have 2 questions on this topic and I would like to bend your (collective)
ear on this issue once more.

 1) How can I pull out the default sort item and use it in the sort
parameter
    of a Template call?
 2) How can I make the main Template more generic

Below is an overview of what I have done, how it works, and the questions,
in context.


<Sample data XML is attached>

I created a 'config' XML file...

  <titles>
     <display id="timestamp" colWidth="098" sortOrder="D"
defaultSort="T">Date</display>
     <display id="curr_dest" colWidth="060" sortOrder="A"
defaultSort="F">Agent</display>
     <display id="reason"    colWidth="*"   sortOrder="A"
defaultSort="F">Reason</display>
  </titles>


This is used to tell the XSLT...
 - which NODEs to pull from the data XML
 - what order to create columns to place which data
 - how wide to make the HTML Table column
 - what is the default sort order, Ascending or Descending
 - which of these items is to be sorted right out of the gate.
 - and what title to place in the title column of the HTML table


In order for the XSLT to process this 'config' XML, I have the XSLT open the
'config' and place the IDs into an array.

   <!-- id list from secondary -->
   <xsl:variable name="aryIDs"
                 select="document('tableData.xml')/titles/display/@id"/>


Now the IDs of the NODEs we want to deal with are in an array, and in the
order we want them as well.

But before we can display the data, we need to default sort it...

   <!-- This will loop for each INTERACTION in this data stream -->
   <!-- This calls the ROOT NODE Template below -->
   <xsl:apply-templates>
      <xsl:sort select="timestamp" order="descending" data-type="text"/>
   </xsl:apply-templates>

This works fine, almost.

What it doesn't do is use the 'config' data to determine which NODE to
default sort on.

Right now it is hard coded on 'timestamp'

So this is my first question:

  Q: How can I pull out of the 'config' which item is to be default
     sorted and insert the item in the above (or similar) code.



The above 'apply-template' tag calls this template...

   <!-- 'interaction' NODE Template -->
   <xsl:template match="interaction">

      <!-- Pull out the MEDIA Type of this record -->
      <xsl:variable name="aryMedia"
                    select="media_type[1]"/>

      <!-- Pull out the Comment Field of this record -->
      <xsl:variable name="aryDetail"
                    select="./events/call_event/detailed_comment"/>

      <!-- Record Row -->
      <tr>

         <!-- Pull out the first 'call_event' NODE into 'aryCallEvents'
array -->
         <xsl:variable name="aryCallEvent" select="events/call_event[1]"/>

         <!-- Loop through each NODE in 'tableData.xml' -->
         <!-- in the order as defined in the 'config' XML file -->
         <xsl:for-each select="$aryIDs">

            <!-- Pull out current ID attribute -->
            <xsl:variable name="thisID" select="."/>

            <!-- Match current ID to NODE in data set -->
            <!-- If found run matching template -->
            <xsl:choose>
              <xsl:when test="$aryCallEvent/*[name() = $thisID]">
<!-- This is my second question, see below -->
                 <xsl:apply-templates select="$aryCallEvent/*[name() =
$thisID]"/>

              <!-- else, special case for media_type NODE !! :( -->
              <!-- MUST find a way to do this without special cases! -->
              <xsl:otherwise>
                 <xsl:call-template name="media">
                    <xsl:with-param name="media_value" select="$aryMedia"/>
                 </xsl:call-template>
              </xsl:otherwise>

            </xsl:choose>

         </xsl:for-each>

      </tr>

   </xsl:template>


The 'apply-templates' defined here calls this template...

   <!-- general NODE Template -->
   <xsl:template match="curr_dest | media_type | detailed_comment | contact
| resolution | disposition">
      <td valign="top" class='data'>
         <xsl:value-of select="."/>
         <xsl:text>&#160;</xsl:text>
      </td>
   </xsl:template>


This leads me to my second question:

  Q: I would like to make this last Template a generic one.
     Right now, it is explicitly defined to handle these NODEs.

     This means that when someone adds to the config XML, this NODE
     definition needs to change.

     Not really what I am trying to achieve.

     I am trying to have a generic Template called from the main
     'apply-template', but...

     There is always a but!

     I have 2 other templates that are used in special cases.


This one tears apart the ISO time/date string and creates a human readable
format...
Well, it would be nice if this dealt with the Time as an AM/PM format. This
ignores this issue.
Also, this Template deals with the conversion and the display all
internally. It would be nice if this could be pulled out as a
subroutine/function, outside the Template, and then the Template can
concentrate on displaying the data, once converted.

   <!-- timestamp NODE Template -->
   <xsl:template match="timestamp">

      <!-- Pull the pieces apart -->
      <xsl:variable name="datetime" select="."/>
      <xsl:variable name="year" select="substring( $datetime, 0 , 5 )"/>
      <xsl:variable name="month" select="substring( $datetime, 6 , 2 )"/>
      <xsl:variable name="day" select="substring( $datetime, 9 , 2 )"/>

      <td valign="top" class='data'>
         <!-- put the date together in American order -->
         <xsl:value-of select="concat($month, '/', $day, '/', $year )"/>
         <!-- Seperate the date and Time -->
         -
         <!-- Display the Time -->
         <xsl:value-of select="substring( $datetime, 12 , 5 )"/>
      </td>

   </xsl:template>

I also have a Template (not given here) that creates a special cell that
contains hidden data, until clicked on.

So I guess I am asking...

  Q: How can I create a Template that will grab all 'apply-template' calls
     that are not explicitly defined (as in the date template)?


I hope that I am clear enough on this.

If not, please reply and and I will try to expand on what Iam trying to ask.

Walter


Sample XML Data
=============================================================

<?xml version="1.0" encoding="iso-8859-1"?>
<!--define the location of the external DTD using a relative URL address-->
<?xml-stylesheet type='text/xsl' href='./historyDisplay-01.xslt'?>
<callEvent>
	<request>
		<command name="GetCustomerInteractionList_custom_code" success="false"
username=""/>
		<param name="customer_id" value="126"/>
		<param name="call_event_type_id" value="29"/>
		<param name="time_zone" value="CST"/>
	</request>
	<response>
		<interaction_list>
			<interaction id="19766">
				<timestamp julian="-1229877939000">1931-01-11T01:14:21</timestamp>
				<media_type id="103">Phone</media_type>
				<channel id="1"></channel>
				<ani/>
				<dnis/>
				<route_type>I</route_type>
				<events>
					<call_event id="34757">
						<timestamp julian="-1229877939000">1931-01-11T01:14:26</timestamp>
						<call_event_type id="98">eMail</call_event_type>
						<customer id="2124">Harris</customer>
						<curr_dest id="103">Vincent</curr_dest>
						<to_dest id="1"></to_dest>
						<nav_code id="1"></nav_code>
						<rule id="1"></rule>
						<greeting id="1"></greeting>
						<call_type id="1"></call_type>
						<ext_call id="1"></ext_call>
						<old_call_record id="1"></old_call_record>
						<product id="1"></product>
						<reason id="198">Case Resolution</reason>
						<detailed_comment></detailed_comment>
					</call_event>
				</events>
			</interaction>
			<interaction id="19767">
				<timestamp julian="-2141394912000">1902-02-22T02:04:48</timestamp>
				<media_type id="103">inquiry</media_type>
				<channel id="1"></channel>
				<ani/>
				<dnis/>
				<route_type>I</route_type>
				<events>
					<call_event id="34759">
						<timestamp julian="-2141394912000">1902-02-28T02:04:48</timestamp>
						<call_event_type id="27">Phone</call_event_type>
						<customer id="8392">Lewis</customer>
						<curr_dest id="101">Harold</curr_dest>
						<to_dest id="1"></to_dest>
						<nav_code id="1"></nav_code>
						<rule id="1"></rule>
						<greeting id="1"></greeting>
						<call_type id="1"></call_type>
						<ext_call id="1"></ext_call>
						<old_call_record id="1"></old_call_record>
						<product id="1"></product>
						<reason id="98">Balance Inquiry</reason>
						<detailed_comment>FAX-back would not send balance on Checking
Account</detailed_comment>
					</call_event>
					<call_event id="34759">
						<timestamp julian="-2141394912000">1902-02-28T02:04:48</timestamp>
						<call_event_type id="27">eMail</call_event_type>
						<customer id="8392">Lewis</customer>
						<curr_dest id="101">Sue</curr_dest>
						<to_dest id="1"></to_dest>
						<nav_code id="1"></nav_code>
						<rule id="1"></rule>
						<greeting id="1"></greeting>
						<call_type id="1"></call_type>
						<ext_call id="1"></ext_call>
						<old_call_record id="1"></old_call_record>
						<product id="1"></product>
						<reason id="98">Funds Transfer</reason>
						<detailed_comment>wanted to move money into
Brokerage</detailed_comment>
					</call_event>
				</events>
			</interaction>
			<interaction id="19768">
				<timestamp julian="-1507127384000">1922-03-30T03:30:16</timestamp>
				<media_type id="103">inquiry</media_type>
				<channel id="1"></channel>
				<ani/>
				<dnis/>
				<route_type>I</route_type>
				<events>
					<call_event id="34768">
						<timestamp julian="-1507127384000">1922-03-30T03:30:21</timestamp>
						<call_event_type id="39">FAX</call_event_type>
						<customer id="3432">Williamson</customer>
						<curr_dest id="121">Sharon</curr_dest>
						<to_dest id="1"></to_dest>
						<nav_code id="1"></nav_code>
						<rule id="1"></rule>
						<greeting id="1"></greeting>
						<call_type id="1"></call_type>
						<ext_call id="1"></ext_call>
						<old_call_record id="1"></old_call_record>
						<product id="1"></product>
						<reason id="98">Account Opening</reason>
						<detailed_comment>This person does not understand finacial
transactions</detailed_comment>
					</call_event>
				</events>
			</interaction>
			<interaction id="19768">
				<timestamp julian="987413416000">2001-04-16T04:30:16</timestamp>
				<media_type id="103">inquiry</media_type>
				<channel id="1"></channel>
				<ani/>
				<dnis/>
				<route_type>I</route_type>
				<events>
					<call_event id="34768">
						<timestamp julian="987413416000">2001-04-16T04:30:32</timestamp>
						<call_event_type id="34">Telepathy</call_event_type>
						<customer id="2824">Simpson</customer>
						<curr_dest id="129">Cindi</curr_dest>
						<to_dest id="1"></to_dest>
						<nav_code id="1"></nav_code>
						<rule id="1"></rule>
						<greeting id="1"></greeting>
						<call_type id="1"></call_type>
						<ext_call id="1"></ext_call>
						<old_call_record id="1"></old_call_record>
						<product id="1"></product>
						<reason id="98">Transfer Funds</reason>
						<detailed_comment>The Web system would not allow Him to move money to
his Money Market and he was very upset about this.</detailed_comment>
					</call_event>
				</events>
			</interaction>
		</interaction_list>
		<event_count>317</event_count>
		<stats>
			<time_for_query>580-milliseconds</time_for_query>
			<result_set_fetch_and_processing_time>1-second
723-milliseconds</result_set_fetch_and_processing_time>
			<total_time>2-seconds 374-milliseconds</total_time>
		</stats>
	</response>
</callEvent>

=============================================================


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread