首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >只需要计算XSLT中两天之间的工作日(不包括周末),需要引用我的XML输出。

只需要计算XSLT中两天之间的工作日(不包括周末),需要引用我的XML输出。
EN

Stack Overflow用户
提问于 2022-05-08 13:58:19
回答 1查看 104关注 0票数 1

我正在尝试创建XSLT,它将从我的XML输出中提取两个日期,并给出不包括周末的两天之间的区别。我从另一个用户那里找到了下面的代码

(How to get only business days between two dates in xslt 1.0)

但是我很难正确地调用我的xml。对于如何让下面的代码引用我的xml有什么建议吗?

编辑:经过进一步的研究,我似乎无法在函数参数上选择一个属性。任何其他想法都将不胜感激。

XML示例和XSLT如下:

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="UTF-8"?>

<Report_Data xmlns="urn:examplenamespace.com">
    <Report_Entry>
        <LOACalcStart>2022-01-09</wd:LOACalcStart>
        <LOACalcEnd>2022-04-21</wd:LOACalcEnd>
    </Report_Entry>
    <Report_Entry>
        <LOACalcStart>2022-01-21</wd:LOACalcStart>
        <LOACalcEnd>2022-04-22</wd:LOACalcEnd>
    </Report_Entry>
</Report_Data>

 <xsl:template match="/">
    <xsl:value-of select="this:WorkDayDifference(xs:date('2014-05-01'),xs:date('2014-05-12'))"/>
    

</xsl:template>

    <xsl:function name="this:WorkDayDifference">
        <xsl:param name="startDate" as="xs:date"/>
        <xsl:param name="endDate" as="xs:date"/>
        <xsl:variable name="endDateDay" select="this:day-of-week($endDate)"/>
        <xsl:variable name="days" select="days-from-duration($endDate - $startDate)"/>
        <xsl:variable name="dow1" select="this:day-of-week($startDate)"/>
        <xsl:variable name="dow2" select="this:day-of-week($endDate)"/>
        <xsl:variable name="weeks" select="xs:integer($days div 7)"/>
        <xsl:variable name="offset" select="if($dow2 ge $dow1) then if($endDateDay = 6 or $endDateDay = 0)then(-6)else(-5) else if($endDateDay = 6 or $endDateDay = 0)then(-1)else(0)"/>
        <xsl:variable name="wdays" select="sum((0,1,1,1,1,1,0,0,1,1,1,1,1,0)[position() ge $dow1 + 1 and position() le ($dow2 + 7)]) + $weeks * 5 + $offset + 1"/>
        <xsl:value-of select="number($wdays)"/>
    </xsl:function>
    
    <xsl:function name="this:day-of-week" as="xs:integer?" >
        <xsl:param name="date" as="xs:anyAtomicType?"/>
        <xsl:sequence select="if (empty($date)) then () else (xs:integer((xs:date($date) - xs:date('1901-01-06')) div xs:dayTimeDuration('P1D')) mod 7)"/>
    </xsl:function> ```
EN

回答 1

Stack Overflow用户

发布于 2022-05-08 18:57:01

只需将函数调用为

代码语言:javascript
运行
复制
<xsl:template match="/" xpath-default-namespace="urn:examplenamespace.com">
    <xsl:value-of select="this:WorkDayDifference(
            xs:date(//StartDate), xs:date(//EndDate))"/>
</xsl:template>

我不禁觉得this:workDayDifference()必须有一个更优雅的实现,但没有什么能立即想到的。

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

https://stackoverflow.com/questions/72161728

复制
相关文章

相似问题

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