日期测试不起作用 - 命名空间问题?

时间:2017-05-09 15:08:34

标签: xml xslt xpath

在下面的xslt中,无论我使用什么条件(大于,小于或等于),我的代码总是进入其他(ohno标签)。似乎很奇怪,当日期确实有值时,这些条件都不是真的。 xs命名空间可能缺少某些东西,因为它似乎无法识别格式日期函数,即使它是xslt 2.0。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            xmlns:xs="http://www.w3.org/2001/XMLSchema"
            version="2.0">
<xsl:output indent="yes"/>

 <xsl:template match="/"> 
    <Hi>
        <Start><xsl:value-of select="'Starting'"/></Start>

        <xsl:call-template name="DayFunction">
            <xsl:with-param name="EndDate" as="xs:date"/>
        </xsl:call-template>
        <End><xsl:value-of select="'End Here'"/></End>
    </Hi>
</xsl:template>

<xsl:template name="DayFunction" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xsl:param name="pEndDate" as="xs:date" select="'2017-05-20'"/>
    <xsl:variable name="vStartDate" as="xs:date" select="'2017-05-01'" />

    <xsl:choose>
            <xsl:when test="$pEndDate &gt;= $vStartDate">
            <ok><xsl:value-of select="$pEndDate"/></ok>
            <xsl:call-template name="DayFunction">
                <xsl:with-param name="EndDate" select="$pEndDate - xs:dayTimeDuration('P1D')"/>
            </xsl:call-template>    
        </xsl:when>
        <xsl:otherwise>
            <ohno><xsl:value-of select="$pEndDate"/></ohno>
            <ohno><xsl:value-of select="$vStartDate"/></ohno>
        </xsl:otherwise>
    </xsl:choose> 
    <after>2</after>    
</xsl:template> 

1 个答案:

答案 0 :(得分:1)

你必须纠正一些细节:

  • xmlns:xs="..."应放在stylesheet代码中,而不是template
  • 如果param的类型(as="xs:date"),则默认值应为。{li> 也可以xs:date给出,而不是字符串。将其更改为 xs:date('2017-05-20')
  • 这同样适用于vStartDate
  • DayFunction模板的调用应该具有相同的参数名称 就像在模板声明中一样。将EndDate更改为pEndDate (注意with-param中缺少 p ,在2个地方)。
  • 为模板调用提供参数时,还必须编写 它的价值。显然你忘记了select中的with-param
  • value-of在开始和结束之间使用常量字符串参数 标签(例如<Start><xsl:value-of select="'Starting'"/></Start>)是 奇怪的建筑。只写<Start>Starting</Start>。 它工作得更快。

下面有一个工作脚本(按预期结果)。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema" version="2.0">
  <xsl:output indent="yes"/>

  <xsl:template match="/"> 
    <Hi>
      <Start>Starting</Start>
      <xsl:call-template name="DayFunction">
        <xsl:with-param name="pEndDate" select="xs:date('2017-05-20')"/>
      </xsl:call-template>
      <End>End Here</End>
    </Hi>
  </xsl:template>

  <xsl:template name="DayFunction">
    <xsl:param name="pEndDate" as="xs:date" select="xs:date('2017-05-20')"/>
    <xsl:variable name="vStartDate" as="xs:date" select="xs:date('2017-05-01')" />
    <xsl:choose>
      <xsl:when test="$pEndDate &gt;= $vStartDate">
        <ok><xsl:value-of select="$pEndDate"/></ok>
        <xsl:call-template name="DayFunction">
          <xsl:with-param name="pEndDate" select="$pEndDate - xs:dayTimeDuration('P1D')"/>
        </xsl:call-template>    
      </xsl:when>
      <xsl:otherwise>
        <ohno><xsl:value-of select="$pEndDate"/></ohno>
        <ohno><xsl:value-of select="$vStartDate"/></ohno>
      </xsl:otherwise>
    </xsl:choose> 
    <after>2</after>    
  </xsl:template>

</xsl:stylesheet>
相关问题