代码之家  ›  专栏  ›  技术社区  ›  AdRock

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

  •  4
  • AdRock  · 技术社区  · 14 年前

    我需要检查一个param是否有一个值在里面,如果它有那么做这一行,否则做这一行。

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

    错误的分支位于志愿者角色模板中

    <?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 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 回复  |  直到 12 年前
        1
  •  5
  •   Justin    13 年前

    下面应该可以做到这一点:

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