将测试插入xslt文件以确保没有值为空

时间:2011-05-25 07:09:29

标签: xslt umbraco

我有以下XSLT文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>
<xsl:stylesheet
  version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:msxml="urn:schemas-microsoft-com:xslt"
  xmlns:umbraco.library="urn:umbraco.library" xmlns:Exslt.ExsltCommon="urn:Exslt.ExsltCommon" xmlns:Exslt.ExsltDatesAndTimes="urn:Exslt.ExsltDatesAndTimes" xmlns:Exslt.ExsltMath="urn:Exslt.ExsltMath" xmlns:Exslt.ExsltRegularExpressions="urn:Exslt.ExsltRegularExpressions" xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings" xmlns:Exslt.ExsltSets="urn:Exslt.ExsltSets" xmlns:tagsLib="urn:tagsLib" xmlns:BlogLibrary="urn:BlogLibrary" xmlns:UCommentLibrary="urn:UCommentLibrary" xmlns:uForum="urn:uForum" xmlns:uForum.raw="urn:uForum.raw"
  exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets tagsLib BlogLibrary UCommentLibrary uForum uForum.raw ">
  <xsl:output method="xml" omit-xml-declaration="yes"/>
  <xsl:param name="currentPage"/>
    <xsl:variable name="blognode" select="umbraco.library:GetXmlNodeById(1187)" />
    <xsl:variable name="today" select="umbraco.library:CurrentDate()" />
    <xsl:variable name="later" select="umbraco.library:DateAdd($today, 'd', -3)" />
    <xsl:variable name="count" select="4" />


  <xsl:template match="/">
    <div id="slider" class="news_banner" >
      <xsl:for-each select="$blognode/ancestor-or-self::*//BlogPost [@isDoc and umbraco.library:DateGreaterThanOrEqual(PostDate, $later) ]">
        <xsl:sort select="pageViewCount" data-type="number" order="descending" />
        <xsl:if test="blogImage != ''">
          <xsl:variable name="crop" select="umbraco.library:GetMedia(blogImage, 0)/crops/crops/crop [@name='NewsBanner']" />
          <xsl:if test="$crop/@url != '' and position() &lt;= $count" >
            <a href="{umbraco.library:NiceUrl(@id)}"><img src="{$crop/@url}" alt="{@nodeName}" title="#htmlcaption{@id}"  /></a>
          </xsl:if>
        </xsl:if>
      </xsl:for-each>
    </div>
    <xsl:for-each select="$blognode/ancestor-or-self::*//BlogPost [@isDoc and umbraco.library:DateGreaterThanOrEqual(PostDate, $later) ]">
      <xsl:sort select="pageViewCount" data-type="number" order="descending" />
      <xsl:if test="blogImage != ''" >
        <xsl:variable name="crop" select="umbraco.library:GetMedia(blogImage, 0)/crops/crops/crop [@name='NewsBanner']" />
        <xsl:if test="$crop/@url != '' and position() &lt;= $count">
          <div id="htmlcaption{@id}" class="nivo-html-caption"><xsl:value-of select="@nodeName" /> - <a href="{umbraco.library:NiceUrl(@id)}">Full Story...</a></div>
        </xsl:if>
      </xsl:if>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

如何在此处插入测试以确保条目不为空白。

1 个答案:

答案 0 :(得分:2)

  

如何在此处插入测试   确保条目不是空白

取决于“空白”的含义。

以下是确定的一般方法,如果特定表达式的值(为方便起见,此处表示为$var)不仅包含空格:

string-length(normalize-space($var)) > 0

如果写成等效的话,可以更加通用和灵活

string-length(translate($var, ' &#xA;&#xC;&#9;')) > 0

如果我们对“空白”的含义有不同的理解,则上述内容可用于检查$var是否为空白。例如:

string-length(translate($var, ' &#xA;&#xC;&#9;&#xA0;')) > 0

测试$var是否包含空格字符(空格,换行符回车符和制表符)旁边的任何其他字符以及“不可破坏的空白字符”(&#xAO;)< / p>