XSL 1.0 date:difference EXSLT

此生再无相见时 提交于 2019-12-13 02:28:04

问题


I have struggled with finding (and using) the difference between two dates.

XSL 2.0 is not an option, but I have found EXSLT date:difference http://exslt.org/date/functions/difference/ that might solve my problem comparing dates in XSL 1.0 - but, I haven't found any good examples on how to use it.

I have an XML-file that looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="gall.xsl"?>
<report>
<title>Individual Item Display</title>
<dateCreated>2016-05-23T14:26:33</dateCreated>
<dateFormat>yyyy/mm/dd</dateFormat>
<catalog>
<catalogKey>100904</catalogKey>
<yearOfPublication>1982</yearOfPublication>
<marc>
<marcEntry tag="100" label="Personal Author" ind="1 ">Linderholm, Helmer, 1916-1982</marcEntry>
<marcEntry tag="245" label="Title" ind="00">Tre ogärningskvinnor / Helmer Linderholm</marcEntry>
</marc>
<call>
<callNumber>HC</callNumber>
<item>
<totalCharges>0</totalCharges>
<intervalCheckouts>0</intervalCheckouts>
<intervalRenewals>0</intervalRenewals>
<dateLastUsed>2014-06-17</dateLastUsed>
<itemID>30580000599535</itemID>
<library>VALLA</library>
<location>SKÖNLITT</location>
<type>HEMLAN</type>
<dateCreated>1996-06-20</dateCreated>
</item>
</call>
</catalog>
<catalog>
<catalogKey>100955</catalogKey>
<yearOfPublication>1983</yearOfPublication>
<marc>
<marcEntry tag="100" label="Personal Author" ind="1 ">Linderholm, Helmer, 1916-82</marcEntry>
<marcEntry tag="245" label="Title" ind="10">Fredens hemliga stig / Helmer Linderholm</marcEntry>
</marc>
<call>
<callNumber>H,U</callNumber>
<item>
<totalCharges>2</totalCharges>
<intervalCheckouts>0</intervalCheckouts>
<intervalRenewals>0</intervalRenewals>
<dateLastUsed>2006-12-18</dateLastUsed>
<itemID>30580001728125</itemID>
<library>VALLA</library>
<location>ULITT</location>
<type>HEMLAN</type>
<dateCreated>1996-06-20</dateCreated>
</item>
</call>
</catalog>
<catalog>
<catalogKey>100901</catalogKey>
<yearOfPublication>1982</yearOfPublication>
<marc>
<marcEntry tag="100" label="Personal Author" ind="1 ">Linderholm, Helmer, 1916-1982</marcEntry>
<marcEntry tag="245" label="Title" ind="00">I de rödas krig / Helmer Linderholm</marcEntry>
</marc>
<call>
<callNumber>H,U</callNumber>
<item>
<totalCharges>1</totalCharges>
<totalCheckouts>0</totalCheckouts>
<totalRenewals>0</totalRenewals>
<intervalCheckouts>0</intervalCheckouts>
<intervalRenewals>0</intervalRenewals>
<intervalStartDate>2016-01-01</intervalStartDate>
<recirculate>YES</recirculate>
<dateLastUsed>2006-12-18</dateLastUsed>
<isReserveItem>false</isReserveItem>
<copyNumber>1</copyNumber>
<itemID>30580001728158</itemID>
<library>VALLA</library>
<location>ULITT</location>
<type>HEMLAN</type>
<dateCreated>1996-06-20</dateCreated>
</item>
</call>
</catalog>
</report>

First I want to filter - so it only contains items where dateLastUsed is older that five years. I think an If-clause would do it, but any suggestions are welcome!

Secondly I want a field that says - preferably in Years-months-days - how long time has passed since dateLastUsed.

This is my tentative stylesheet - and I have no clue on how to proceeed.

<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:date="http://exslt.org/dates-and-times"
                extension-element-prefixes="date">

<xsl:import href="date.xsl" />

<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
    <catalog>
    <xsl:for-each select="report/catalog[call/item[type='FLYTANDE' or type='HEMLAN']]">
    <xsl:sort select="call/item/library"/>
    <xsl:sort select="call/callNumber"/>
    <xsl:sort select="marc/marcEntry[@tag='100']"/>
    <xsl:sort select="marc/marcEntry[@tag='245']"/>
        <xsl:if test="call/item/intervalCheckouts = 0 and call/item/intervalRenewals = 0">
        <!--Here I want a second if.
        If today minus lastUsed is greater than five years
        -->
            <itemline>
                <yearOfPublication><xsl:value-of select="yearOfPublication"/></yearOfPublication>
                <Author><xsl:value-of select="marc/marcEntry[@tag='100']"/></Author>
                <Title><xsl:value-of select="substring(marc/marcEntry[@tag='245'],1,30)"/></Title>
                <callNumber><xsl:text>"</xsl:text><xsl:value-of select="string(call/callNumber)"/><xsl:text>"</xsl:text></callNumber>
                <library><xsl:value-of select="call/item/library"/></library>
                <itemID><xsl:value-of select="call/item/itemID"/></itemID>
                <type><xsl:value-of select="call/item/type"/></type>
                <location><xsl:value-of select="call/item/location"/></location>
                <totalCharges><xsl:value-of select="call/item/totalCharges"/></totalCharges>
                <itemCreated><xsl:value-of select="call/item/dateCreated"/></itemCreated>
                <dateLastUsed><xsl:value-of select="call/item/dateLastUsed"/></dateLastUsed>
                <today><xsl:value-of select="substring(//report/dateCreated,1,10)"/></today>
                <!--
                Here I want a duration: today minus lastUsed as Years - months - days
                -->
            </itemline>
        </xsl:if> 
    </xsl:for-each>
    </catalog>
</xsl:template>
</xsl:stylesheet>

来源:https://stackoverflow.com/questions/37392402/xsl-1-0-datedifference-exslt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!