cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1815
Views
25
Helpful
4
Replies

Cisco UCCX 10.6 Get XML Document Data XPath count() function

noisey_uk
Level 1
Level 1

Hi

I'd like to retrieve the number of XML nodes in a document to make a script more efficient.

e.g. with this XML, I'd like to know how many <message> nodes there are:

<messages>

<message>contentA</message>

<message>contentA</message>

<message>contentA</message>

</messages>

I've tried iNodeCount = Get XML Document Data (inputXMLfile, "count(//messages/message)")

If I try this in an XPath expression tester then I get the result I'm expecting - an integer of 3. However, in UCCX this produces the error "Can not convert #NUMBER to a NodeList!"... What am I doing wrong?

Thanks

 

1 Accepted Solution

Accepted Solutions

Gergely Szabo
VIP Alumni
VIP Alumni

Hi,

nothing. The Get XML Document Data step expects the XPath expression return a NodeList.

Apparently, there's some post processing afterwards.

It just gets stuck because this post processing cannot be performed since the XPath expression gave it a single number.

Alternatives:

- use some Java code to do the counting,

- recursively count the matching nodes.

G.

View solution in original post

4 Replies 4

Gergely Szabo
VIP Alumni
VIP Alumni

Hi,

nothing. The Get XML Document Data step expects the XPath expression return a NodeList.

Apparently, there's some post processing afterwards.

It just gets stuck because this post processing cannot be performed since the XPath expression gave it a single number.

Alternatives:

- use some Java code to do the counting,

- recursively count the matching nodes.

G.

Thanks Gergely, I thought that may be the case. If this is what's happening, how can I work around with Java? (I'm currently doing the recursive counting and it's messy plus is problematic because of the 1,000+ executed steps). My first thought was that NodeList could be assigned to an array and a count operation performed on that. However, when I changed iNodeCount to a one-dimensional array it failed script validation... so I'm stumped!

Hi, 

you may want to use a Set or a Do step to execute Java code. 

First, reference the XML file (within the repository) by calling DOC[myFile.xml] and of course, assigning this value to a Document type variable, e.g.: 

Set myDocument = DOC[myFile.xml]

Then add a Do step that actually does the XPath part of the job. 

There's a nice step by step explanation of it here: http://viralpatel.net/blogs/java-xml-xpath-tutorial-parse-xml/

Remember, you can get the Inputstream object of the myDocument variable using the getInputStream() method on it.

G.

noisey_uk
Level 1
Level 1

Java is the way forward. Recursive looping puts you at risk of exceeding the maximum executed steps. Here's what I did (to save you the pain!)

  try {
      javax.xml.parsers.DocumentBuilderFactory factory = javax.xml.parsers.DocumentBuilderFactory.newInstance();
      javax.xml.parsers.DocumentBuilder builder = factory.newDocumentBuilder();
      org.w3c.dom.Document domDoc = builder.parse(XMLDocument);
      javax.xml.xpath.XPath XPath = javax.xml.xpath.XPathFactory.newInstance().newXPath();
      String XpathEval = "count(//messages/message
)";
      Integer RETURNVAL = XPath.evaluate(XpathEval,domDoc);
      return RETURNVAL;
  } 
  catch (java.lang.Exception e) {
      e.printStackTrace();
  }