[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

conditional template matching & failure

Subject: conditional template matching & failure
From: Ryan Neil Gillespie <scoobie@xxxxxxxx>
Date: Mon, 15 Jul 2002 16:14:39 -0400 (EDT)
conditional template
[this is long, so only read if you're in a helpful mood]

I'm fairly new to XSL (yes, I've checked alternate resources before
posting), so this is probably a no-brainer for experienced XSL
programmers.

Generally, I have an XML file with student schedule information in it and
I want to print each class out when given a specific student & term. A
student may not exist in the XML file, however, for whatever reason.


> if student & term found
   > print schedule
> else
  > give a "not found" msg

---------
Problems:
---------

1. Without using xsl:if or :choose, I can't stop a failed template match
from dumping the entire data

   Example:
	<xsl:template match="//schedule[@username=$thisUser]">
        ...
        </xsl:template>

   Results: prints out entire XML if $thisUser is not found


2. Using xsl:if and :choose doesn't traverse me down the tree to the
elements I want to print, so I'm writing long (and expensive) XPaths on
the subsequent condition

   Example:
	<xsl:for-each
select="//schedule[@username=$thisUser]/semester[season=$thisSeason and
year=$thisYear]/course">
	  ...
	</xsl:for-each>

   Results: long, slow, inefficient XPath - should just be able to say
"for-each select='course'"


3. I can't use a template match with a VariableReference (student name),
but I can't create a separate template for every case either (too many
students)

   Example:
	<xsl:apply-templates
select="//schedule[@username=$thisUser]/semester"/>
	  ...
	<xsl:template match="//schedule[@username=$thisUser]/semester">
	  ...
	</xsl:template>

   Results: invalid XSL

---------------------
schedule.xml
---------------------
<schedules>

  <schedule username="tommy">

    <semester>
      <season>fall</season>
      <year>2002</year>

      <course>
        <title>Calc 1</title>
        <desc>...</desc>
      </course>

      <course>
        <title>Gym</title>
        <desc>...</desc>
      </course>

    </semester>

  </schedule>

</schedules>
--------------------
- can be 1 to many <schedule>
- can be 1 to many <semester>
- can be 1 to many <course>
--------------------

I want to template match to the level of <semester> so I'm only
processing at that level to print out the course info and if the
<schedule> username attribute is not found, it returns "not found."

I tried doing something like:

<xsl:template match="schedule[@username=$thisUser]">

  <table>

  <xs:apply-templates select="semester[season=$thisSeason and
year=$thisYear]"/>

  </table>

</xsl:template>

<xsl:template match="semester[season=$thisSeason and year=$thisYear]">
   <xsl:for-each select="course">

     <tr><td><xsl:value-of select="title"/></td></tr>

   </xsl:for-each>
</xsl:template>

------
This obviously fails because you can't have a VariableReference in the
template match, but this is the gist of what I'm trying to accomplish.

Any and all help is appreciated as I try to get my feet wet.


Regards,
_ryan


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


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.