There are numerous Holiday check scripts out there for CCX; I saw one yesterday that comes with some scripts Cisco provides for free (or used to anyhow) and thought I’d write this post with a script that is quite the dichotomy from that particular script..Of course the objective is the same: Is Today a Holiday or Not?
Below is the Document we are going to parse..
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <Holidays> <Holiday>1/1/14</Holiday> <Holiday>1/20/14</Holiday> <Holiday>2/17/14</Holiday> <Holiday>7/4/14</Holiday> <Holiday>9/1/14</Holiday> <Holiday>10/17/14</Holiday> <Holiday>11/27/14</Holiday> <Holiday>12/25/14</Holiday> <Holiday>1/1/15</Holiday> </Holidays>
Let’s briefly go over some terminology when it comes to XML. Holidays is considered the Root of this Document; other ways in which to refer to an item in an XML file are: Element, Tag, Node. In fact every Element within an XML Document is considered a Node; you have the Elements
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <Holidays><Holiday></Holiday></Holidays>
Holidays is the Root Node/Element of the Document..another way to say this is the Root Tag is Holidays and the child element (Tag) is Holiday each of these Elements can be referred to as XML Nodes as well..another XML Node is the Text Node values of the Holiday Elements: the Dates in “M/dd/yy” Format (Short). Ok..let’s break this down in the most simple term I possibly could; once I’m done I feel that you will get it!
//First We Have a String that will be converted to a //(java.text.DateFormat.SHORT) style //Cisco Set Step Follows: Set todaysDate = new Date() //Properties of this shows Short Date //Download the Document, Setup for PARSING //DOC is Store in DOC Repository dHolidays = Create XML Document(DOC[Holidays.xml]) //The Parser is SAX (from the STD Java Library) //Next we use the Get XML DOC Data //To do this you have to use XPath //holiday is a String type holiday = Get XML Documnet Data(dHolidays, "//Holiday[.='" + todaysDate + "']") //The Xpression above is 3 Strings //Concat'd together If(todaysDate == holiday) Then True Set isHoliday = true False //Today is Not a Holiday
Because there are multiple Holiday Nodes, in our XPath Expression we need to pass in a “String” Value that matches the Style in the Document (“M/dd/yy”) which essential returns the same text if that Date exists in the document; hopefully that makes sense..it is a weird looking “syntax” (better than descendant/child IMO) but if you read what XPath is, Wikipedia says it best:
XPath, the XML Path Language, is a query language for selecting nodes from an XML document. In addition, XPath may be used to compute values (e.g. strings, numbers, or Boolean values) from the content of an XML document. XPath was defined by the World Wide Web Consortium (W3C).
We are done..it can’t get much easier than that to check for a Holiday. I’m going to leave you with the Script and the DOC that I used in this Post. Most use HolidayChecks with SubFlow Steps in the Script..but why? In fact, why do we store the Holidays in an XML file to begin with; as a guy said it best to me the other day in the SupportForums, “this is how I was taught” and that voodoo is the hardest break…Open the script in the version of CCX you are running (it should work on 7.x and above)
Lastly I decided I would leave you with a video..this video depicts 2 Debug Sessions..1 where the Date Doesn’t Match Today (Not A Holiday) and 1 where I Manually Set Today’s Date to Holiday in the Document..so (isHoliday will equal TRUE)..If you want to watch the video in HD..then you’ll have to go over to the vimeo site and watch it there..And before I forget, here are the files I promised: HolidayCheck