vas
vas

Reputation: 25

Filter Blocked Dates XSLT

Have the following input

Sample Input : 

<GetShippingTimeSlotsRes xmlns:xsdLocal="example.com/xml/ShippingTimeSlotsRes" xmlns="example.com/xml/ShippingTimeSlotsRes">
    <xsdLocal:GetShippingTimeSlotsRes>
        <xsdLocal:Weekdate>04/24/2017</xsdLocal:Weekdate>
        <xsdLocal:Weekday>1</xsdLocal:Weekday>
        <xsdLocal:Category>2</xsdLocal:Category>
        <xsdLocal:From>08:00</xsdLocal:From>
        <xsdLocal:To>18:00</xsdLocal:To>
        <xsdLocal:AvailabilityId>1</xsdLocal:AvailabilityId>
        <xsdLocal:Sequence>2</xsdLocal:Sequence>
    </xsdLocal:GetShippingTimeSlotsRes>
    <xsdLocal:GetShippingTimeSlotsRes>
        <xsdLocal:Weekdate>04/25/2017</xsdLocal:Weekdate>
        <xsdLocal:Weekday>3</xsdLocal:Weekday>
        <xsdLocal:Category>3</xsdLocal:Category>
        <xsdLocal:From>13:00</xsdLocal:From>
        <xsdLocal:To>18:00</xsdLocal:To>
        <xsdLocal:AvailabilityId>1</xsdLocal:AvailabilityId>
        <xsdLocal:Sequence>123</xsdLocal:Sequence>
    </xsdLocal:GetShippingTimeSlotsRes>
    <xsdLocal:GetShippingTimeSlotsRes>
        <xsdLocal:Weekdate>04/26/2017</xsdLocal:Weekdate>
        <xsdLocal:Weekday>3</xsdLocal:Weekday>
        <xsdLocal:Category>3</xsdLocal:Category>
        <xsdLocal:From>13:00</xsdLocal:From>
        <xsdLocal:To>18:00</xsdLocal:To>
        <xsdLocal:AvailabilityId>1</xsdLocal:AvailabilityId>
        <xsdLocal:Sequence>123</xsdLocal:Sequence>
    </xsdLocal:GetShippingTimeSlotsRes>
    <xsdLocal:GetShippingTimeSlotsRes>
        <xsdLocal:Weekdate>04/27/2017</xsdLocal:Weekdate>
        <xsdLocal:Weekday>3</xsdLocal:Weekday>
        <xsdLocal:Category>3</xsdLocal:Category>
        <xsdLocal:From>13:00</xsdLocal:From>
        <xsdLocal:To>18:00</xsdLocal:To>
        <xsdLocal:AvailabilityId>1</xsdLocal:AvailabilityId>
        <xsdLocal:Sequence>123</xsdLocal:Sequence>
    </xsdLocal:GetShippingTimeSlotsRes>
    <xsdLocal:GetShippingTimeSlotsRes>
        <xsdLocal:Weekdate>04/28/2017</xsdLocal:Weekdate>
        <xsdLocal:Weekday>3</xsdLocal:Weekday>
        <xsdLocal:Category>6</xsdLocal:Category>
        <xsdLocal:From>18:00</xsdLocal:From>
        <xsdLocal:To>22:00</xsdLocal:To>
        <xsdLocal:AvailabilityId>1</xsdLocal:AvailabilityId>
        <xsdLocal:Sequence>123</xsdLocal:Sequence>
    </xsdLocal:GetShippingTimeSlotsRes>
    <xsdLocal:BlockedDates>
        <xsdLocal:Date>04/24/2017</xsdLocal:Date>
    </xsdLocal:BlockedDates>
    <xsdLocal:BlockedDates>
        <xsdLocal:Date>04/27/2017</xsdLocal:Date>
    </xsdLocal:BlockedDates>
</GetShippingTimeSlotsRes>

Sample Output :

<GetShippingTimeSlotsRes xmlns:xsdLocal="example.com/xml/ShippingTimeSlotsRes" xmlns="example.com/xml/ShippingTimeSlotsRes">
    <xsdLocal:GetShippingTimeSlotsRes>
        <xsdLocal:Weekdate>04/25/2017</xsdLocal:Weekdate>
        <xsdLocal:Weekday>3</xsdLocal:Weekday>
        <xsdLocal:Category>3</xsdLocal:Category>
        <xsdLocal:From>13:00</xsdLocal:From>
        <xsdLocal:To>18:00</xsdLocal:To>
        <xsdLocal:AvailabilityId>1</xsdLocal:AvailabilityId>
        <xsdLocal:Sequence>123</xsdLocal:Sequence>
    </xsdLocal:GetShippingTimeSlotsRes>
    <xsdLocal:GetShippingTimeSlotsRes>
        <xsdLocal:Weekdate>04/26/2017</xsdLocal:Weekdate>
        <xsdLocal:Weekday>3</xsdLocal:Weekday>
        <xsdLocal:Category>3</xsdLocal:Category>
        <xsdLocal:From>13:00</xsdLocal:From>
        <xsdLocal:To>18:00</xsdLocal:To>
        <xsdLocal:AvailabilityId>1</xsdLocal:AvailabilityId>
        <xsdLocal:Sequence>123</xsdLocal:Sequence>
    </xsdLocal:GetShippingTimeSlotsRes>
    <xsdLocal:GetShippingTimeSlotsRes>
        <xsdLocal:Weekdate>04/28/2017</xsdLocal:Weekdate>
        <xsdLocal:Weekday>3</xsdLocal:Weekday>
        <xsdLocal:Category>6</xsdLocal:Category>
        <xsdLocal:From>18:00</xsdLocal:From>
        <xsdLocal:To>22:00</xsdLocal:To>
        <xsdLocal:AvailabilityId>1</xsdLocal:AvailabilityId>
        <xsdLocal:Sequence>123</xsdLocal:Sequence>
    </xsdLocal:GetShippingTimeSlotsRes>
    <xsdLocal:BlockedDates>
        <xsdLocal:Date>04/24/2017</xsdLocal:Date>
    </xsdLocal:BlockedDates>
    <xsdLocal:BlockedDates>
        <xsdLocal:Date>04/27/2017</xsdLocal:Date>
    </xsdLocal:BlockedDates>
</GetShippingTimeSlotsRes>

Input comprises of 2 parts

1) List of dates with time slots 2) List of blocked dates

Time slots related to the dates which are blocked have to be filtered based on the dates which are present below.

Tried multiple approaches but none seems to work.

In the example shown above 24th and 27th april 2017 are shown as blocked dates and in the output there shouldnt be any node "xsdLocal:GetShippingTimeSlotsRes" which has the weekdate matching to those dates

Any help please?

Upvotes: 1

Views: 39

Answers (1)

Valdi_Bo
Valdi_Bo

Reputation: 31001

You didn't specify the XSLT version, so I used version 2.0.

The whole thing boils down to creation of a template which matches the items (time slots) which are in the "forbidden" list and does nothing with them.

The remaining time slots (and other content) will be output by the identity template.

How to create this template:

  • Create a key for the blocked dates (at the beginning).
  • Include a reference to this key in the select attribute of this "do-nothing" template.

A weird point in your XML is that your root GetShippingTimeSlotsRes tag has the same name as the subordinate GetShippingTimeSlotsRes tag.

To refer to the subordinate tags only, I included GetShippingTimeSlotsRes twice in the match attribute.

Below you have a working solution, as you see, quite short.

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xsdLocal="example.com/xml/ShippingTimeSlotsRes"
  xpath-default-namespace="example.com/xml/ShippingTimeSlotsRes">
  <xsl:output method="xml" encoding="UTF-8" indent="yes" />
  <xsl:strip-space elements="*"/>
  <xsl:key name="dates" match="BlockedDates/Date" use="."/>

  <xsl:template match="GetShippingTimeSlotsRes/GetShippingTimeSlotsRes[key('dates',Weekdate)]"/>

  <xsl:template match="@*|node()">
    <xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy>
  </xsl:template>
</xsl:transform>

Upvotes: 1

Related Questions