Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xsl template match a node with specific value

Tags:

xslt

xslt-2.0

I am trying to filter the XML by what I get in a parameter. What is the correct sintax to match on only those courses that have dep_code equal to 'something' or in this case $department???

XML STRUCTURE IS

     <course>
         <dep_code></dep_code>
         <catalog_number></catalog_number>
         <course_group></course_group>
         <term></term>
         <offered></offered>
         <course_type></course_type>
         <course_level></course_level>
         <title></title>
         <course_number></course_number>
         <meeting_text></meeting_text>
         <faculty_text></faculty_text>
         <description></description>
         <prerequisites></prerequisites>

      </course>

Parameter defined

<xsl:param name="department" select="'All'" />

Code I have tried I have tried this:

<xsl:template match="course/dep_code/{$department}">

Also this:

<xsl:template match="course/dep_code/$department">


<xsl:template match="course/dep_code eq $department">

I I keep on getting syntactical errors like the following:

  1. F [Saxon-PE 9.4.0.3] Unexpected token "{" in path expression
  2. F [Saxon-PE 9.4.0.3] A variable reference is not allowed in an XSLT pattern (except in a predicate)

Thanks in advance

like image 969
Cristy Avatar asked Jan 15 '23 22:01

Cristy


1 Answers

Sample XML

<?xml version="1.0" encoding="ISO-8859-1"?>
<courses>
      <course>
         <dep_code>not_it</dep_code>
         <catalog_number></catalog_number>
         <prerequisites></prerequisites>
      </course>
      <course>
         <dep_code>something</dep_code>
         <catalog_number></catalog_number>
         <prerequisites></prerequisites>
      </course>
</courses>

Sample XSLT

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
  <html>
  <body>
    <table border="1">
      <tr>
        <th>dep_code</th>
      </tr>
      <xsl:for-each select="courses/course">
        <xsl:if test="dep_code[text()='something']">
      <tr>
        <td><xsl:value-of select="dep_code"/></td>
      </tr>
        </xsl:if>
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

Result

<html>
<body>
<table border="1">
<tr><th>dep_code</th></tr>
<tr><td>something</td></tr>
</table>
</body>
</html>
like image 81
Tad Avatar answered Jan 21 '23 10:01

Tad