Hi All

Ive got an XSL for some XML data. The XML data contains a date field called OrderNum_OrderDate.

How can I filter the data so it only displays data from the XML that has a date of TODAY + 7 days.

I only want to see information for the next seven days.

It needs to be based on a dynamic date so it doesnt have to be changed every day.

Any help would be appreciated.

Thanks.

Dani AI

Generated

There are two common meanings in the thread so this clarifies both and gives ready-to-drop patterns for each approach (XSLT 2.0 first, then a pragmatic XSLT 1.0 option). As prompted, pick 1.0 vs 2.0 up front — date arithmetic is trivial in 2.0 and awkward in 1.0. Also note the sample dates in the thread are in DD/MM/YY (e.g. the post on 2011-02-28 => today+7 = 2011-03-07), so normalize to a sortable form before comparing.

XSLT 2.0 (recommended when available)

  • cast the normalized string to xs:date and use current-date() + xs:dayTimeDuration:
    
    <!-- inside the element processing -->
    <xsl:variable name="rdate" select="xs:date(concat('20', substring(OrderHed_RequestDate,7,2), '-', substring(OrderHed_RequestDate,4,2), '-', substring(OrderHed_RequestDate,1,2)))"/>

<!-- next 7 days, inclusive (today .. today+7) -->
<xsl:if test="$rdate >= current-date() and $rdate <= current-date() + xs:dayTimeDuration('P7D')">
<!-- emit row -->
</xsl:if>

<!-- 7..14 days ahead -->
<xsl:if test="$rdate > current-date() + xs:dayTimeDuration('P7D') and $rdate <= current-date() + xs:dayTimeDuration('P14D')">
<!-- emit row for second file -->
</xsl:if>

Use inclusive/exclusive boundaries deliberately (above uses first range inclusive up to +7 and second strictly greater than +7 to avoid double counting).

XSLT 1.0 (when 2.0 not available)
- XSLT 1.0 has no current-date() or date arithmetic. Best practice is to compute today and the wanted offsets in the host (script/.NET/Java) and pass them as parameters in YYYYMMDD numeric form. Normalize the element date to YYYYMMDD and compare as numbers:

<xsl:param name="today" /> <!-- e.g. 20110228 -->
<xsl:param name="todayPlus7" /> <!-- e.g. 20110307 -->

<xsl:variable name="d" select="normalize-space(OrderHed_RequestDate)"/>
<xsl:variable name="norm" select="concat('20', substring($d,7,2), substring($d,4,2), substring($d,1,2))"/>

<xsl:if test="number($norm) >= number($today) and number($norm) <= number($todayPlus7)">
<!-- emit row -->
</xsl:if>



Practical notes and gotchas
- If input can be DD/MM/YYYY adapt substring offsets accordingly; verify two- vs four-digit years.  
- Validate blank/malformed dates before casting (skip or treat as non-matching).  
- Compute the normalized date once per node (store in a variable) for performance.  
- If possible, filter at the data source (SQL/report generator) to avoid transmitting extra rows.  
- For @SBA-CDeCinko (past 7 days) use the same patterns but compare against current-date() - xs:dayTimeDuration('P7D') (XSLT 2.0) or pass a $todayMinus7 param (XSLT 1.0).

These patterns let @chriselectrix keep the existing element traversal and simply add a date predicate (or an enclosing xsl:if) — same idea applies to creating two separate XSL files (adjust the range constants).

Recommended Answers

All 4 Replies

XSLT 1.0 or 2.0 ? Please post a small sample of your XML input document and how you want the data to look like on the output. USE CODEBOXES.

Thanks for the prompt reply.

Im totally new to all of this so what may be easy for you is not for me :-)

This is the first entry in my XML for the 'PARENT'.

<?xml version="1.0" encoding="utf-8"?>
<ExportQuery>
  <ELEC01_SalesOrdersWaitingParts>
    <Customer_CustID><![CDATA[V027]]></Customer_CustID>
    <OrderHed_RequestDate><![CDATA[01/02/11]]></OrderHed_RequestDate>
    <OrderHed_OrderDate><![CDATA[24/02/11]]></OrderHed_OrderDate>
    <OrderHed_NeedByDate><![CDATA[07/03/11]]></OrderHed_NeedByDate>
    <OrderHed_OrderNum><![CDATA[19122]]></OrderHed_OrderNum>
    <OrderHed_OpenOrder><![CDATA[Open]]></OrderHed_OpenOrder>
    <OrderDtl_OrderNum><![CDATA[19122]]></OrderDtl_OrderNum>
    <OrderDtl_OrderLine><![CDATA[1]]></OrderDtl_OrderLine>
    <OrderDtl_OrderQty><![CDATA[10.00]]></OrderDtl_OrderQty>
    <OrderDtl_PartNum><![CDATA[S17 150-150]]></OrderDtl_PartNum>
    <Part_PartNum><![CDATA[S17 150-150]]></Part_PartNum>
    <TotalOnHandQty><![CDATA[1.00]]></TotalOnHandQty>
    <TotalDemandQty><![CDATA[35.00]]></TotalDemandQty>
    <OnHandDemandLine><![CDATA[-24.00]]></OnHandDemandLine>
    <JobHead_PartNum><![CDATA[S17 150-150]]></JobHead_PartNum>
    <JobHead_JobNum><![CDATA[018816]]></JobHead_JobNum>
    <JobHead_JobComplete><![CDATA[No]]></JobHead_JobComplete>
    <JobOper_JobNum><![CDATA[018816]]></JobOper_JobNum>
    <JobOper_OpCode><![CDATA[INSP PR6]]></JobOper_OpCode>
    <JobOper_OpDesc><![CDATA[INSP FOLD PR6]]></JobOper_OpDesc>
    <JobOper_OpComplete><![CDATA[No]]></JobOper_OpComplete>
    <JobOper_QtyCompleted><![CDATA[0.00]]></JobOper_QtyCompleted>
    <NotEnoughStock><![CDATA[TRUE]]></NotEnoughStock>
    <JobMtl_JobNum><![CDATA[018816]]></JobMtl_JobNum>
    <JobMtl_PartNum><![CDATA[1.4301 2J PC1 1500 X 1250 X 1.2]]></JobMtl_PartNum>
    <JobMtl_RequiredQty><![CDATA[0.86]]></JobMtl_RequiredQty>
  </ELEC01_SalesOrdersWaitingParts>

This is my code for my XSL and this is where I need it to filter on the OrderHed.RequestDate.

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" omit-xml-declaration="yes"/>

<xsl:template match="/">
  <html>
  <body bgcolor="white">
Orders to be shipped before TODAY + 7 days
    <table cellpadding="4px">
      <tr>
	<td align="center"><B1>Customer</B1></td>
        <td align="center"><B1>Order No.</B1></td>
        <td align="center"><B1>Order Date</B1></td>
        <td align="center"><B1>Ship By</B1></td>
        <td><B1>Part Required</B1></td>
        <td align="right"><B1>Req Qty</B1></td>
	<td align="right"><B1>O/H</B1></td>
        <td align="right"><B1>Dem.</B1></td>
        <td align="center"><B1>Job</B1></td>
        <td><B1>Stage</B1></td>
        <td align="right"><B1>Complete</B1></td>
	<td align="center"><B1>Status</B1></td>
      </tr>
      <xsl:for-each select="ExportQuery/ELEC01_SalesOrdersWaitingParts[NotEnoughStock='TRUE']">

	<xsl:if test="position()  mod 2 = 1">

      	<tr bgcolor="#CCCCCC">

	<td align="center"><B2><xsl:value-of select="Customer_CustID"/></B2></td>
        <td align="center"><xsl:value-of select="OrderHed_OrderNum"/></td>
      	<td align="center"><xsl:value-of select="OrderHed_OrderDate"/></td>
      	<td align="center"><xsl:value-of select="OrderHed_RequestDate"/></td>
      	<td><xsl:value-of select="Part_PartNum"/></td>
      	<td align="right"><xsl:value-of select="OrderDtl_OrderQty"/></td>
      	<td align="right"><xsl:value-of select="TotalOnHandQty"/></td>
      	<td align="right"><xsl:value-of select="TotalDemandQty"/></td>
      	<td align="center"><xsl:value-of select="JobHead_JobNum"/></td>
      	<td><xsl:value-of select="JobOper_OpDesc"/></td>
      	<td align="right"><xsl:value-of select="JobOper_QtyCompleted"/></td>

   	</tr>
	</xsl:if>


	<xsl:if test="position()  mod 2 = 0">

      	<tr bgcolor="#FFFFFF">

	<td align="center"><B2><xsl:value-of select="Customer_CustID"/></B2></td>
        <td align="center"><xsl:value-of select="OrderHed_OrderNum"/></td>
      	<td align="center"><xsl:value-of select="OrderHed_OrderDate"/></td>
      	<td align="center"><xsl:value-of select="OrderHed_RequestDate"/></td>
      	<td><xsl:value-of select="Part_PartNum"/></td>
      	<td align="right"><xsl:value-of select="OrderDtl_OrderQty"/></td>
      	<td align="right"><xsl:value-of select="TotalOnHandQty"/></td>
      	<td align="right"><xsl:value-of select="TotalDemandQty"/></td>
      	<td align="center"><xsl:value-of select="JobHead_JobNum"/></td>
      	<td><xsl:value-of select="JobOper_OpDesc"/></td>
      	<td align="right"><xsl:value-of select="JobOper_QtyCompleted"/></td>

   	</tr>
	</xsl:if>



      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

I want to firstly see anything that has a OrderHed.RequestDate >= TODAY + 7 days.

I then want to be able to filter it in a separate XSL so it displays any OrderHed.RequestDate >= TODAY + 7 days BUT <= TODAY + 14 days.

Any help / suggestions would be appreciated.

Polite nudge - does any one have any suggestions?

I'm also looking for this answer. I need to show results for the past 7 days.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.