Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Predicates

Avatar

Level 2

I have the following predicate:

var predicate = "$record.TopMerchants.Record[*]";

var nodeitems = $data.resolveNodes(predicate);

which searches an XML structure similar to the following:

<TopMerchants>

     <Record>

          <Country>United Kingdom</Country>

          <X1>testing</X1>

          <X2>testing again</X2>

     </Record>

     <Record>

          <Country>United Kingdom</Country>

          <X1>testing</X1>

          <X2>testing again</X2>

     </Record>

     <Record>

          <Country>United States</Country>

          <X1>testing</X1>

          <X2>testing again</X2>

     </Record>

</TopMerchants>

This works fine in retrieving all of the records under the TopMerchants node, however what I really want is to be able to bring back records that have a specific country but I am not sure how this is done in the predicate, can anyone shed any light as to how you would achieve this??

1 Accepted Solution

Avatar

Correct answer by
Level 8

You sure can use predicates to search almost anything. In your case

xfa.record.resolveNodes('TopMerchants.Record.[Country=="United Kingdom"]')

The period denotes the start of the predicate. It returns all the Record's that meet the condition between [] for FormCalc and () for JavaScript.

I personally tend to use the applyXPath method just because I'm use to using XPath expressions but it's only a personal preference (not sure which one is faster):

XMLData.applyXPath(xfa.data,"TopMerchants/Record[Country='United Kingdom']")

It looks more complicated but on intense searches with multiple parameters I find it easier. In your case though I'd use the predicates.

Kyle

View solution in original post

7 Replies

Avatar

Level 10

You are working with XML format, so this is all nodes

to be able to access Country from Record, I don't think you can do Record[*] (not sure)

to my knowledge, to be able to access Country you would have to write something like so

for (var i = 0; i < predicate.nodes.length; i++){

     if (predicate.nodes.item(i).value.text.value == "Any country"){

          //Code here

     }

}

Because you have already written resolveNodes() i am not sure if you can do this but try this or the other above

for (var i = 0; i < nodeItems.nodes.length; i++){

     //Code here

}

Avatar

Level 2

Thanks Robert, I should have mentioned that I am already performing a for loop as above, however this seems like an unecessary and potentially processor intensive step especially in that there might 100's of records returned that I don't necessarily need as they are not related to the country I am showing, so I was wondering if there was a way of doing this at predicate level so that only the nodes with a matching country sub-item are returned. This of course may not be possible but it would be good to know for sure.

Avatar

Level 10

for 100 record it should not be really long to process...

Even for 1000 it would not be long to process since its only a loop and not a recursive method

I've already made a recursive method that loops over all the pdf document for each nodes which is over 1000's of nodes

and it doesn't take too long to get through all the nodes...

the loop i provided is the best option you have in this case

Avatar

Correct answer by
Level 8

You sure can use predicates to search almost anything. In your case

xfa.record.resolveNodes('TopMerchants.Record.[Country=="United Kingdom"]')

The period denotes the start of the predicate. It returns all the Record's that meet the condition between [] for FormCalc and () for JavaScript.

I personally tend to use the applyXPath method just because I'm use to using XPath expressions but it's only a personal preference (not sure which one is faster):

XMLData.applyXPath(xfa.data,"TopMerchants/Record[Country='United Kingdom']")

It looks more complicated but on intense searches with multiple parameters I find it easier. In your case though I'd use the predicates.

Kyle

Avatar

Level 2

Hi Kyle,

It's some time since you posted this answer, however I am now running into the situation where I need to specify multiple parameters, are you able to provide me with a couple of examples of the applyXPath method doing a query using multiple parameters?

Thanks in advance.

Graham

Avatar

Level 8

Maybe instead, if you want to send me an example of what you require I can try to send you a predicate that will work for you.

Kyle

Avatar

Level 2

Thanks for the reply Kyle, however since the post I have now managed to work it out, for the benefit of others see an example below:

var nodeitems = xfa.record.resolveNodes('datasourcename.Record[Criteria1=="Test" and Criteria2=="Test" and Criteria3=="Test"]');