xmlxsltaltova

Date dependant XSL transformation


Currently I output to txt files a large number of product codes and various attributes held within them. "Item End Date" is one of the attributes outputted.

Is there a way to query this attribute value to say only perform the XSL functions if Item End Date is after today's date?

I am using Altova if that helps.

XML:

 <Agility>
<group>
    <product id="295826" subtype="CMS Product" created="20/04/12" modified="03/03/15">
        <attribute definition_id="26" modified="20/04/12" is_name="true" is_identifier="true" scope="global" type="text">
            <data language="English" label="CMS Product Name">012345</data>
        </attribute>
        <attribute definition_id="278" modified="28/08/08" scope="global" type="datetime">
            <data language="English" label="Item End Date">01/01/99</data>
        </attribute>
    </product>
</group>

XSL:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" version="1.0" encoding="UTF-8" indent="yes"/>


<xsl:template match="product[@subtype = 'CMS Product']">

<xsl:for-each select="attribute">
  <xsl:if test="@definition_id = 278"> 
    <xsl:value-of select="data" />
  </xsl:if>
</xsl:for-each>

</xsl:template>
</xsl:stylesheet>

Solution

  • Try it this way?

    <xsl:template match="product">
        <xsl:variable name="end" select="attribute/data[@label='Item End Date']" />
        <xsl:variable name="end-date" select="xs:date(concat('20', substring($end, 7, 2), '-', substring($end, 4, 2), '-',substring($end, 1, 2)))" />
        <xsl:if test="$end-date gt current-date()">
            <!-- do your processing here -->
        </xsl:if>
    </xsl:template>