开发者

XSL check param length and make a choice

开发者 https://www.devze.com 2022-12-27 14:37 出处:网络
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 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>


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'
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号