cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1152
Views
0
Helpful
3
Replies

XML PATH question

JIANDE ZHANG
Level 1
Level 1

I want to get these date from one XML file. My script works well, except one part doesn't work. I can not write correct XML PATH.

So, every time. I got null from this XML file.

Thanks in advance

<?xml version="1.0" encoding="ISO-8859-1"?>
<Holidays>
       <Holiday>12/10/16</Holiday>
       <Holiday>1/20/14</Holiday>
       <Holiday>4/26/14</Holiday>
       <Holiday>7/4/14</Holiday>
       <Holiday>9/1/14</Holiday>
       <Holiday>10/17/14</Holiday>
       <Holiday>11/27/14</Holiday>
       <Holiday>12/10/16</Holiday>
       <Holiday>10/12/16</Holiday>
       <Holiday>"10/12/16"</Holiday>
       <Holiday>"12/10/16"</Holiday>
</Holidays>

2 Accepted Solutions

Accepted Solutions

Jonathan Schulenberg
Hall of Fame
Hall of Fame

Don't look at Cisco's documentation for anything to do with XPath. The W3Schools Tutorial is how I figured XPath out.

In your case, the XPath query is as simple as "/Holidays/Holiday[1]" to get 12/10/16; "/Holidays/Holiday[2]" to get 1/20/14, etc.

Since you have an unpredictable quantity of <Holiday> elements you would want to wrap the logic within a Do While loop. The iterator variable would replace the number, making your XPath query (in concatenated String format) "/Holidays/Holiday[" + i + "]". When the Get XML Document Data step returns a null value you've reached the end of the list.

View solution in original post

nathan.a.reeves
Level 1
Level 1

Agreed with Jonathan below on the XPath comments.

I'd take a punt you're possibly wanting to check the holidays xml file to see if today is a holiday.  I'd originally done a loop to check each holiday, but ended up changing the format of my xml to allow me to do a single xpath lookup.

XML format looks like:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- NO LEADING ZEROES IN DATE NUMBERS !!!! -->
<!-- DD/MM/YYYY Format -->
<!-- HolidaysObserved: -->
<!-- NewYearsDay, AustraliaDay, EightHoursDay -->
<Holidays>
<Holiday Date="1/1/2016">NewYearsDay</Holiday>
<Holiday Date="26/1/2016">AustraliaDay</Holiday>
<Holiday Date="14/3/2016">EightHoursDay</Holiday>
</Holidays>

I then use the following script code to query my xml:

Couple of things to note:

  • I'm using dd/mm/yyyy format in the xml.
  • I've included name of the holiday to make it easier to keep track of the actual holidays in my file, and it also allows me to make routing decisions where one of the holidays I check for actually starts at 11:00am so I need to know if I've hit that day.  You could just drop the holiday name if it's of no use.
  • I run this code as a subflow and return the 'bTodayIsAHoliday' and 'sHolidayName' variables to the calling script.  Just makes my parent script cleaner.

Hope this assists.

View solution in original post

3 Replies 3

Jonathan Schulenberg
Hall of Fame
Hall of Fame

Don't look at Cisco's documentation for anything to do with XPath. The W3Schools Tutorial is how I figured XPath out.

In your case, the XPath query is as simple as "/Holidays/Holiday[1]" to get 12/10/16; "/Holidays/Holiday[2]" to get 1/20/14, etc.

Since you have an unpredictable quantity of <Holiday> elements you would want to wrap the logic within a Do While loop. The iterator variable would replace the number, making your XPath query (in concatenated String format) "/Holidays/Holiday[" + i + "]". When the Get XML Document Data step returns a null value you've reached the end of the list.

nathan.a.reeves
Level 1
Level 1

Agreed with Jonathan below on the XPath comments.

I'd take a punt you're possibly wanting to check the holidays xml file to see if today is a holiday.  I'd originally done a loop to check each holiday, but ended up changing the format of my xml to allow me to do a single xpath lookup.

XML format looks like:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- NO LEADING ZEROES IN DATE NUMBERS !!!! -->
<!-- DD/MM/YYYY Format -->
<!-- HolidaysObserved: -->
<!-- NewYearsDay, AustraliaDay, EightHoursDay -->
<Holidays>
<Holiday Date="1/1/2016">NewYearsDay</Holiday>
<Holiday Date="26/1/2016">AustraliaDay</Holiday>
<Holiday Date="14/3/2016">EightHoursDay</Holiday>
</Holidays>

I then use the following script code to query my xml:

Couple of things to note:

  • I'm using dd/mm/yyyy format in the xml.
  • I've included name of the holiday to make it easier to keep track of the actual holidays in my file, and it also allows me to make routing decisions where one of the holidays I check for actually starts at 11:00am so I need to know if I've hit that day.  You could just drop the holiday name if it's of no use.
  • I run this code as a subflow and return the 'bTodayIsAHoliday' and 'sHolidayName' variables to the calling script.  Just makes my parent script cleaner.

Hope this assists.

JIANDE ZHANG
Level 1
Level 1

Thanks for Nathan and Jonathan!  Your answers are really helped a lot.

I noticed that your names are very closed.

and, I figured out other way myself which is very close to Nathan's method.

XML PATH part: "//Holiday[text()='"+todaysDate+"']"