4

I need to check if a param has got a value in it and if it has then do this line otherwise do this line.

I've got it working whereas I don't get errors but it's not taking the right branch

The branch that is wrong is in the volunteer_role template

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:key name="volunteers-by-region" match="volunteer" use="region" />
  <xsl:template name="hoo" match="/">
    <html>
      <head>
        <title>Registered Volunteers</title>
        <link rel="stylesheet" type="text/css" href="volunteer.css" />
      </head>
      <body>
        <h1>Registered Volunteers</h1>
        <h3>Ordered by the username ascending</h3>
        <xsl:for-each select="folktask/member[user/account/userlevel='2']">
          <xsl:for-each select="volunteer[count(. | key('volunteers-by-region', region)[1]) = 1]">
            <xsl:sort select="region" />
            <xsl:for-each select="key('volunteers-by-region', region)">
              <xsl:sort select="folktask/member/user/personal/name" />
                <div class="userdiv">
                  <xsl:call-template name="volunteer_volid">
                    <xsl:with-param name="volid" select="../volunteer/@id" />
                  </xsl:call-template>
                  <xsl:call-template name="volunteer_role">
                    <xsl:with-param name="volrole" select="../volunteer/roles" />
                  </xsl:call-template>
                  <xsl:call-template name="volunteer_region">
                    <xsl:with-param name="volloc" select="../volunteer/region" />
                  </xsl:call-template>
                </div>
              </xsl:for-each>
            </xsl:for-each>
          </xsl:for-each>
          <xsl:if test="position()=last()">
            <div class="count">
              <h2>Total number of volunteers: <xsl:value-of select="count(/folktask/member/user/account/userlevel[text()=2])" />
              </h2>
            </div>
          </xsl:if>
        </body>
      </html>
    </xsl:template>

    <xsl:template name="volunteer_volid">
      <xsl:param name="volid" select="'Not Available'" />
      <div class="heading2 bold"><h2>VOLUNTEER ID: <xsl:value-of select="$volid" /></h2></div>
    </xsl:template>

    <xsl:template name="volunteer_role">
      <xsl:param name="volrole" select="'Not Available'" />
      <div class="small bold">ROLES:</div>
      <div class="large">
      <xsl:choose>
        <xsl:when test="string-length($volrole)!=0">
          <xsl:value-of select="$volrole" />
        </xsl:when>
        <xsl:otherwise>
          <xsl:text> </xsl:text>
        </xsl:otherwise>
      </xsl:choose>
    </div>
  </xsl:template>

  <xsl:template name="volunteer_region">
    <xsl:param name="volloc" select="'Not Available'" />
    <div class="small bold">REGION:</div>
    <div class="large"><xsl:value-of select="$volloc" /></div>
  </xsl:template>
</xsl:stylesheet> 

And the XML:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<?xml-stylesheet type="text/xsl" href="volunteers.xsl"?>
<folktask xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" xs:noNamespaceSchemaLocation="folktask.xsd">
  <member>
    <user id="1">
      <personal>
        <name>Abbie Hunt</name>
        <sex>Female</sex>
        <address1>108 Access Road</address1>
        <address2></address2>
        <city>Wells</city>
        <county>Somerset</county>
        <postcode>BA5 8GH</postcode>
        <telephone>01528927616</telephone>
        <mobile>07085252492</mobile>
        <email>adrock@gmail.com</email>
      </personal>
      <account>
        <username>AdRock</username>
        <password>269eb625e2f0cf6fae9a29434c12a89f</password>
        <userlevel>4</userlevel>
        <signupdate>2010-03-26T09:23:50</signupdate>
      </account>
    </user>
    <volunteer id="1">
      <roles></roles>
      <region>South West</region>
    </volunteer>
  </member>
  <member>
    <user id="2">
      <personal>
        <name>Aidan Harris</name>
        <sex>Male</sex>
        <address1>103 Aiken Street</address1>
        <address2></address2>
        <city>Chichester</city>
        <county>Sussex</county>
        <postcode>PO19 4DS</postcode>
        <telephone>01905149894</telephone>
        <mobile>07784467941</mobile>
        <email>ambientexpert@yahoo.co.uk</email>
      </personal>
      <account>
        <username>AmbientExpert</username>
        <password>8e64214160e9dd14ae2a6d9f700004a6</password>
        <userlevel>2</userlevel>
        <signupdate>2010-03-26T09:23:50</signupdate>
      </account>
    </user>
    <volunteer id="2">
      <roles>Van Driver,gas Fitter</roles>
      <region>South Central</region>
    </volunteer>
  </member>
</folktask>
Random Human
  • 946
  • 1
  • 14
  • 31
AdRock
  • 2,959
  • 10
  • 66
  • 106
  • Your choose/when/otherwise looks correct to me. what is your input? You can always test by inserting something like and then have a look at the output. – topskip Apr 22 '10 at 09:54
  • Where do i put that to? I put it straight after the param name or between the when and i get an error saying "Error loading stylesheet: Parsing an XSLT stylesheet failed." – AdRock Apr 22 '10 at 10:06
  • I've put it right after param (before the div). Are you using XSLT 1.0? I have tried with XSLT 2.0. – topskip Apr 22 '10 at 10:13
  • I have no idea what XSLT i'm using...haven't been doing it that long. That's where i put it also. The node that could be empty is It appears that it thinks it's not empty – AdRock Apr 22 '10 at 10:23
  • Could you post a complete example? Both of the source file and of the XSLT file. The version number is at the top of your XSLT file ( – topskip Apr 22 '10 at 10:30
  • Posted my example with sample xml and XSLT – AdRock Apr 22 '10 at 11:08
  • I have made a new question here where you can see the whole XSLT and XML http://stackoverflow.com/questions/2690520/how-to-group-using-xslt – AdRock Apr 22 '10 at 12:05
  • This question mus be closed then. Three more votes needed. – Dimitre Novatchev Apr 22 '10 at 13:33
  • This problem still isn't sorted. I have the problem sorted with my other question but this problem still occurs. I have reshown the code but in full – AdRock Apr 22 '10 at 23:54
  • Sorted this problem. There wasn't anything wrong with the code but it wasn't displaying the empty space properly. Added ¢ or whatever it was and it fixed it – AdRock Apr 23 '10 at 00:17

1 Answers1

5

The following should do the trick:

<xsl:template name="volunteer_volid">
  <xsl:param name="volid" />
  <xsl:choose>
    <xsl:when test="string-length($volid) > 0">
      <div class="heading2 bold"><h2>VOLUNTEER ID: <xsl:value-of select="$volid" /></h2></div>  
    </xsl:when>
    <xsl:otherwise>
      <!-- No volid -->
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

I've replaced the default value with an empty string so that not providing a parameter value is the same as providing the parameter value as "". If this isn't the desired behaviour then change the parameters select and modify the test expression accordingly, for example:

$volid != 'Not Available'
Justin
  • 84,773
  • 49
  • 224
  • 367