cancel
Showing results for 
Search instead for 
Did you mean: 

Handling Attributes in xpath expressions in Sap Cloud Integration CPI

mark_fryu
Participant
0 Kudos
276

hi,

I have to read an attribute of the xml as a property, the namespace is also present.

the xml is like this:

<ns0:test>

  <test2 ns1:testattribute="test">

  </test2>

</ns0:test>

How do I read the "testattribute" attribute with xpath?

I tried:

/test/test2/@testattribute

but looking in trace, the property is empty and does not contain the value:

mark_fryu_1-1720455563352.png

 

 

Thank you

 

 

 

mark_fryu
Participant
0 Kudos
Hi, the purpose is to take the value of the attribute. for example, in this case the attribute name is 'testattribute' and its value is 'test' (the value can change with each call). I need to valorise the value of the attribute in a property (so in this case 'test') because then I have to use the property in the flow

Accepted Solutions (1)

Accepted Solutions (1)

Ryan-Crosby
Active Contributor

I think you are looking for something like the following, otherwise you must qualify your namespaces in your XPath expression.

/*[local-name() = 'test']/test2/@*[local-name() = 'testattribute']

 

Regards,

Ryan Crosby 

mark_fryu
Participant
0 Kudos

hi,

thank you for the reply.

I don't know the value of the attribute, can I make it dynamic?

I find this: 

mark_fryu_0-1720514284199.png

but since I don't know the value of the attribute, I don't know what to put in place of 'value'

https://www.simplilearn.com/tutorials/selenium-tutorial/xpath-in-selenium#:~:text=XPath%20Text()%20f....

 

Ryan-Crosby
Active Contributor
@mark_fryu what would be the purpose of trying to select an attribute if you don't know what the attribute represents or if one even exists and at what level?
mark_fryu
Participant
0 Kudos

hi, @Ryan-Crosby 

the purpose is to take the value of the attribute. for example, in this case the attribute name is 'testattribute' and its value is 'test' (the value can change with each call).

I need to valorise the value of the attribute in a property (so in this case 'test') because then I have to use the property in the flow.

the attribute level is just like the example below (and namespaces are also present):

<ns0:test>

<tes2 ns1:testattribute="test">

</tes2>

</ns0:test>

Ryan-Crosby
Active Contributor

@mark_fryu still don't know how you would make use of an unknown attribute in any flow.  If you don't know what you are looking for, then how would you know when you've found it?

mark_fryu
Participant
0 Kudos

hi @Ryan-Crosby 

the flow on integration suite receives a call via SOAP, the xml attribute can have different values.

For example, I can receive the following XML:

<ns0:test>

<tes2 ns1:testattribute="control">

</tes2>

</ns0:test>

 

Or I can receive the following XML:

 

<ns0:test>

<tes2 ns1:testattribute="invoice">

</tes2>

</ns0:test>

 

so I know the name of the attribute, but I don't know the value.


Let me know if I made myself understood
Thank you

Ryan-Crosby
Active Contributor
@mark_fryu if you know the name of the attribute but not the value, then the answer I already provided should be sufficient.
mark_fryu
Participant
0 Kudos

in the formula you indicated to me,

I see that you refer to 'test' which is the value of the attribute.

If I don't know the value of the attribute, what should I put?

This is your formula /*[local-name() = 'test']/test2/@*[local-name() = 'testattribute']

what do I put here instead of 'test'?

Ryan-Crosby
Active Contributor
@mark_fryu that value of 'test' is for the root node of the xml which is named 'test', not the value of the attribute in your example.
mark_fryu
Participant
0 Kudos

hi @Ryan-Crosby 

oh okay thanks, it worked!!

Can I ask you if you can tell me why we need to use the local-name() function?
Is there a site/blog where it explains the existence of all the functions that can be used?
A thousand thanks

Ryan-Crosby
Active Contributor

@mark_fryu the * is to denote any namespace, otherwise you have to give the exact reference - e.g. ns0:test. The other option is *:test which I tested to confirm it also works.  Personally I would opt for the use of local-name() because it looks cleaner to read vs. noticing the tiny asterisk character.

Answers (1)

Answers (1)

mark_fryu
Participant
0 Kudos

hi, @Ryan-Crosby 

Hi, the purpose is to take the value of the attribute.
for example, in this case the attribute name is 'testattribute' and its value is 'test' (the value can change with each call).

I need to valorise the value of the attribute in a property (so in this case 'test') because then I have to use the property in the flow

the attribute level is just like the example below (and namespaces are also present):

 

<ns0:test>

<tes2 ns1:testattribute="test">

</tes2>

</ns0:test>