QUESTION:
I am trying to create a new XML(filtered XML) depending on the parameters passed on the query string.
URL Example: search_advanced.xhtml?department=CHEM&offered=Y&level=P
For instance, if the query string above is passed, I will like the filtered XML to only show those courses that contain
- A department that are equal to CHEM (fas_courses/course/department/@code)
- An offered code equal to Y (fas_course/course/@offered)
- A Level code equal to P (fas_course/course/@offered)
the original XML file and the XSLT file I have been working on are below. Thanks for any suggestions.
THE ORIGINAL XML
<fas_courses>
<course acad_year="2012" cat_num="85749" offered="N" next_year_offered="2013">
<term term_pattern_code="4" fall_term="Y" spring_term="Y">full year</term>
<department code="VES">
<dept_long_name>Department of Visual and Environmental Studies</dept_long_name>
<dept_short_name>Visual and Environmental Studies</dept_short_name>
</department>
<course_group code="VES">Visual and Environmental Studies</course_group>
<title>Critical Media Practice: Non Fiction Filmmaking Workshop</title>
<course_type>Studio</course_type>
<course_level code="G">Graduate Course</course_level>
<description>A graduate workshop for Film Study Center non-fiction film and video projects.</description>
</course>
<course>
.....
</course>
<course>
.....
</course>
</fas_courses>
THE XSL FILE
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:param name="url"/>
<xsl:param name="querystring"/>
<xsl:param name="baselink"/>
<xsl:param name="department" select="'All'"/>
<xsl:param name="course_group" select="'All'"/>
<xsl:param name="description" select="'All'"/>
<xsl:param name="level" select="'All'"/>
<xsl:param name="term" select="'All'"/>
<xsl:param name="offered" select="'All'"/>
<xsl:template match="/">
<fas_courses>
<xsl:apply-templates />
</fas_courses>
</xsl:template>
<xsl:template match="//course
[
($department = '' or $department = 'All' or department/@code = $department)
and
($course_group = '' or $course_group = 'All' or course_group/@code = $course_group)
and
($description = '' or $description = 'All' or description = $description)
and
($level = '' or $level = 'All' or course_level/@code = $level)
and
($term = '' or $term = 'All' or term/@term_pattern_code = $term)
and
($offered = '' or $offered = 'All' or @offered = $offered)
]">
<xsl:copy-of select="."/>
</xsl:template>
</xsl:stylesheet>
If you can preprocess the query string, you can pass the parts as parameters to the transformation.
Based on the parameters you’ve set up, I’m assuming you can’t do that, so the code below parses one passed-in parameter (the URL) to derive each parameter. Note that the method I’ve used assumed one change to your query string: that it can end with an ampersand. If it cannot, you’ll need a slightly more sophisticated string parsing method. FunctX has one that should work: substring-before-if-contains. Otherwise, the last item in your query will end up as an empty string.
The following XSL provides the desired result on the following XML (returns only cat number 85753) when run with your sample URL snippet passed in as the url param (with the final ampersand added).
Note the empty course template at the end, which allows non-matching courses to be skipped. Without that, you’ll see the text nodes from those non-matching courses.
XML:
XSL:
Hope that is helpful.