XSL检查参数长度并做出选择

时间:2010-04-22 09:46:12

标签: xslt xls param

我需要检查一个参数是否有一个值,如果它有,那么就行,否则就行了。

我已经有了它的工作,而我没有得到错误,但它没有采取正确的分支

错误的分支在volunteer_role模板中

<?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> 

和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>

1 个答案:

答案 0 :(得分:5)

以下应该可以解决问题:

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

我用空字符串替换了默认值,因此不提供参数值与将参数值提供为“”相同。如果这不是所需的行为,则更改参数select并相应地修改test表达式,例如:

$volid != 'Not Available'