首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >符合计算值的项目的XSLT计数

符合计算值的项目的XSLT计数
EN

Stack Overflow用户
提问于 2011-12-14 07:08:54
回答 4查看 2K关注 0票数 1

我有一组XML数据,我正在尝试检查并确定有多少项的日期不到一周。

数据:

代码语言:javascript
复制
<events>
    <event>
        <eventDate>20110504</date>
        <description>whatever</description>
    </event>
    <event>
        <eventDate>20110720</date>
        <description>whatever</description>
    </event>
    .
    .
    .
    <event>
        <eventDate>20111210</date>
        <description>whatever</description>
    </event>
</events>

XSLT:

代码语言:javascript
复制
<xsl:variable name="events" select="sc:item('/sitecore/content/Home/Upcoming Events',.)" />
<xsl:variable name="now" select="dateutil:get_IsoNow()" />
<xsl:variable name="counter" select="0" />
<xsl:template match="*">
    <xsl:apply-templates select="$sc_item" mode="main"/>
</xsl:template>
<xsl:template match="*" mode="main">
    <xsl:choose>
        <xsl:when test="count($events/item) &gt; 0">
            <xsl:for-each select="$events/item">
                <xsl:sort select="sc:fld('eventDate',.)" order="ascending"/>
                <xsl:variable name="date_difference">
                    <xsl:call-template name="date-difference">
                        <xsl:with-param name="item-year" select="substring(sc:fld('eventDate',.),1,4)" />
                        <xsl:with-param name="item-month" select="substring(sc:fld('eventDate',.),5,2)" />
                        <xsl:with-param name="item-day" select="substring(sc:fld('eventDate',.),7,2)" />
                        <xsl:with-param name="today-year" select="substring(substring($now,1,8),1,4)" />
                        <xsl:with-param name="today-month" select="substring(substring($now,1,8),5,2)" />
                        <xsl:with-param name="today-day" select="substring(substring($now,1,8),7,2)" />
                    </xsl:call-template>
                </xsl:variable>
                <xsl:if test="($date_difference &lt; 8)">
                    <p>
                        <sc:text field="eventDate" /><br />
                        <sc:text field="description" />
                    </p>
            ****  Increment a counter variable  ****
                    $counter++

                </xsl:if>
            </xsl:for-each>

            ****  Check the counter variable  ****
            <xsl:if test="($counter = 0)">
                <p>No upcoming events are scheduled at this time.</p>
            </xsl:if>

        </xsl:when>
        <xsl:otherwise>
            <p>No upcoming events are scheduled at this time.</p>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>


<xsl:template name="date-difference">
    <xsl:param name="item-year" />
    <xsl:param name="item-month" />
    <xsl:param name="item-day" />
    <xsl:param name="today-year" />
    <xsl:param name="today-month" />
    <xsl:param name="today-day" />
    <xsl:variable name="jul-item">
        <xsl:call-template name="calculate-julian-day">
            <xsl:with-param name="year" select="$item-year" />
            <xsl:with-param name="month" select="$item-month" />
            <xsl:with-param name="day" select="$item-day" />
        </xsl:call-template>
    </xsl:variable>
    <xsl:variable name="jul-today">
        <xsl:call-template name="calculate-julian-day">
            <xsl:with-param name="year" select="$today-year" />
            <xsl:with-param name="month" select="$today-month" />
            <xsl:with-param name="day" select="$today-day" />
        </xsl:call-template>
    </xsl:variable>
    <xsl:value-of select="$jul-today - $jul-item" />
</xsl:template>


<xsl:template name="calculate-julian-day">
    <xsl:param name="year" />
    <xsl:param name="month" />
    <xsl:param name="day" />
    <xsl:variable name="a" select="floor((14 - $month) div 12)" />
    <xsl:variable name="y" select="$year + 4800 - $a" />
    <xsl:variable name="m" select="$month + 12 * $a - 3" />
    <xsl:value-of select="$day + floor((153 * $m + 2) div 5) + $y * 365 + floor($y div 4) - floor($y div 100) + floor($y div 400) - 32045" />
</xsl:template>

我知道不能在XSLT中更改变量的值,因为它们实际上是常量。

我知道我需要使用某种递归。

我只是不知道该怎么做..。我真的被难住了。

有没有人能帮帮忙?

EN

回答 4

Stack Overflow用户

发布于 2011-12-14 07:24:26

您不能确定“一周前”日期的字符串值,然后使用XPath进行文本比较吗?

代码语言:javascript
复制
count(string(event/eventDate) > $oneweekagodatestring)
票数 0
EN

Stack Overflow用户

发布于 2011-12-14 12:21:31

在代码中替换此

代码语言:javascript
复制
                  L o n g    C o d e
        ****  Increment a counter variable  **** 
              $counter++ 

使用的

代码语言:javascript
复制
  <xsl:variable name="vHits">
    L o n g    C o d e
    <!-- If the condition is true -->
    <xsl:text>1</xsl:text>
  </xsl:variable>

和这段代码

代码语言:javascript
复制
        ****  Check the counter variable  ****                    
     <xsl:if test="($counter = 0)">                        
      <p>No upcoming events are scheduled at this time.</p>                    
     </xsl:if>  

使用

代码语言:javascript
复制
     <xsl:if test="(string-length($vHits = 0)">                        
      <p>No upcoming events are scheduled at this time.</p>                    
     </xsl:if>  

完整代码示例

XSLT这种转换演示了使用进行“无计数器”计算:)

代码语言:javascript
复制
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="/*">
  <xsl:variable name="vHits">
   <xsl:apply-templates/>
  </xsl:variable>

  <xsl:value-of select="string-length($vHits)"/>

  <xsl:text> numbers fulfil x^2 &lt;= 10</xsl:text>
 </xsl:template>

 <xsl:template match="num">
  <xsl:if test="not(. * . > 10)">1</xsl:if>
 </xsl:template>
</xsl:stylesheet>

在此XML文档上应用时的

代码语言:javascript
复制
<nums>
  <num>01</num>
  <num>02</num>
  <num>03</num>
  <num>04</num>
  <num>05</num>
  <num>06</num>
  <num>07</num>
  <num>08</num>
  <num>09</num>
  <num>10</num>
</nums>

生成所需的正确结果

代码语言:javascript
复制
3 numbers fulfil x^2 <= 10
票数 0
EN

Stack Overflow用户

发布于 2011-12-14 23:34:42

如果你正在使用Sitecore,你总是可以使用一些内置的日期函数来获取yearmonthday,而不需要求助于处理子字符串等。

代码语言:javascript
复制
<xsl:variable name="selectedDate">20111212</xsl:variable>
<xsl:variable name="isoSelectedDate" select="date:ToIsoDate(date:ParseDateTime($selectedDate,date:IsoDateToDateTime(sc:isoNow())))"/>

<xsl:variable name="day" select="sc:day($isoSelectedDate)"/>
<xsl:variable name="month" select="sc:month($isoSelectedDate)"/>
<xsl:variable name="year" select="sc:year($isoSelectedDate)"/>

我认为Dimitre的答案效果很好,但这可能会略微增加可读性:)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8497520

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档