Expand my Community achievements bar.

SOLVED

Set value with XPath dynamically

Avatar

Level 4

Dear All,

I am wonder if the following is possiable with Set Value service using XPath, any *simple* workaround is also welcome too.

If I have a variable myXML =

<root>

<item region="One">1</item>
<item region="Two">2</item>

</root>

I can reference to /root/item[1] by:

set /process/@myNum = 1

set /process/@dummy  = /process_data/myXml/root/item[number(\process\@myNum)]


But how can I access /root/item[@region="One"] ?

I tried the following but all failed:

1)

set /process/@myString = @region="One"

set /process/@dummy  = /process_data/myXml/root/item[string(\process\@myString)]

2)

set /process/@myString = One

set /process/@dummy  = /process_data/myXml/root/item[@region='string(\process\@myString)']

And are there any way to set a variable using template string? (i.e. You name is {$/process/@yourName$} )

Regards

Bill

1 Accepted Solution

Avatar

Correct answer by
Employee

Bill,

I have created 2 simple samples that I am attaching to the response as an .LCA file.

First about getting the value of a node using an attribute, what you need to do is create a variable that will contain the dynamic data - in your case the value of region. Then create another variable that will reveive the value of the desired XMl element - I called mine "value". So your setValue would look like this:

Location                                              Expression

/process_data/@region                         "One"

/process_data/@value                          /process_data/xmlIn/root/item[@region=/process_data/@region]

As you can see, there is no need to use the string() function because by default, everything in XML is a string. This is precisely why you need to use the number() function when trying to identify an element by index. Index has to be number, not a string.

Second, about the template value with xPath. I created a sample email process to demonstrate this solution. You are on the right path, it's just that your syntax was incorrect. You have:

It should be:

View solution in original post

6 Replies

Avatar

Correct answer by
Employee

Bill,

I have created 2 simple samples that I am attaching to the response as an .LCA file.

First about getting the value of a node using an attribute, what you need to do is create a variable that will contain the dynamic data - in your case the value of region. Then create another variable that will reveive the value of the desired XMl element - I called mine "value". So your setValue would look like this:

Location                                              Expression

/process_data/@region                         "One"

/process_data/@value                          /process_data/xmlIn/root/item[@region=/process_data/@region]

As you can see, there is no need to use the string() function because by default, everything in XML is a string. This is precisely why you need to use the number() function when trying to identify an element by index. Index has to be number, not a string.

Second, about the template value with xPath. I created a sample email process to demonstrate this solution. You are on the right path, it's just that your syntax was incorrect. You have:

It should be:

Avatar

Level 4

Hi Marcel,

Thanks for the reply, it help a lot.

But I would also like to know if there are any document about the dynamic syntax of XPath used in ALC, I know how to write the XPath, but in many case, if I want to write it dynamic with ALC, I just find no reference and need to try many times.

For the template question, sorry for the typo and not clearly asking the question.

I am fine while the entry allow me to input template, but I am try to set the variable with template.

What I am trying to do is to return the SOAP message used with the Web Service by a output variable.

If I cannot use template to set a variable, I can use function like concat to construct the same result, but I will need to modify the expression  every times I modify the SOAP message, and that's will not be a good idea.

Regards

Bill

Avatar

Employee

Bill,

As the nature of dynamic xPath statements is very use-case dependant, there is not much in the way of documentation. However, I will forward this thread to the documentation team to see if there is anything that can be added in the future to make these types of questions covered in the docs.

As for setting a variable by using a template, there is no way to do this in setValue service unfortunately. However, you can use the executeScript service in LC ES. This service allows you to write java code and have access to the LC ES APIs while in context of your process. If you go to http://help.adobe.com/en_US/livecycle/9.0/workbenchHelp/help.htm and look under Creating Processes Using LiveCycle Workbench ES2 / Service Reference / Execute Script you will find documentation about how ot use this service.

There is a method called replacePathExpressions(String aString) that is exposed by the patExecContext object. this method takes in a template like you are looking to use. Basically a string that contains some xPath expressions wrapped in {$ $}. I created a sample process (attached to this post) that has a process variable called templateString. I used a setValue service to set it's value to:

Of course, I also have variables called "name" and "address" that I have tagged as input variables. Then I have the executeScript service with the following code (basically 3 lines of code with alot of comments):

Now you can dynamically construct responses using template variables.


Also, thanks to Florentin Wandeler for the tip on the replacePathExpressions() method.

Avatar

Level 4

Thanks Marcel !!

Thanks for all the help, that's really make my life easier! And thanks to Florentin for provide the information!

Just checked the document and find no information about the function "replacePathExpressions", was wondering why there are so many useful function being hidden...

Regards

Bill

Avatar

Employee

Bill,

You are most welcome.

it's not that we want to hide useful information like this. It's just that the sheer number of APIs takes time to document.

Avatar

Level 4

Thanks Marcel,

Good to hear that it is just a matter of times.

Regards

Bill