This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
conditional template matching & failure
- From: Ryan Neil Gillespie <scoobie at UDel dot Edu>
- To: XSL-List at lists dot mulberrytech dot com
- Date: Mon, 15 Jul 2002 16:14:39 -0400 (EDT)
- Subject: [xsl] conditional template matching & failure
- Reply-to: xsl-list at lists dot mulberrytech dot com
[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