Xpath Assistance


I have a SOAP response received as below:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
                  <faultstring>Fatura ID sistemde mevcut</faultstring>
                        <ProcessingFault xmlns="http:/abcde.com/eInvoice/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

I want to check the existence of the element Code or Message using string-length and if it is greater than zero. How can I achieve that using the Xpath.

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Gertone (Geert Bormans)Information ArchitectCommented:
XPath 2.0
//*:Code[normalize-space(.)] | //*:Message[normalize-space(.)]

Note that you never should use string-length() > 0 for checking content
simple string casting in a boolean expression will return true() if the string-length > 0 false() if zero
if you don't want space normalisation
//*:Code[string(.)] | //*:Message[string(.)]

string-length is only usefull if you really need to know the length, never for testing
(all examples out there that do that are wrong!)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Gertone (Geert Bormans)Information ArchitectCommented:
XPath 1.0 if you can't use a namespace manager
//*[local-name() = 'Code'][normalize-space(.)] | //*[local-name() = 'Message'][normalize-space(.)]
Gertone (Geert Bormans)Information ArchitectCommented:
Note that a node-set in a boolean expression returns false() when empty,
so in an XSLT you would do
<xsl:if test="//*[local-name() = 'Code'][normalize-space(.)] | //*[local-name() = 'Message'][normalize-space(.)] ">...
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

Gertone (Geert Bormans)Information ArchitectCommented:
And XPath1.0 and you would be able to use a namespace manager

//abc:Code[normalize-space(.)] | //abc:Message[normalize-space(.)]

just make sure you bind prefix abc: to this namespace in the namespacemanager "http:/abcde.com/eInvoice/"
Pradeep0308Author Commented:
Hi Geert,

I can use Xpath 2.0 If you are suggesting not to use string-length than how should I check the existence of either of these two elements and then do a certain step?

Gertone (Geert Bormans)Information ArchitectCommented:
By string casting

both string() and normalize-space() cast the node to string
the [] requires a boolean expression inside
casting a string to a boolean expression returns true() if the string length is > 0

will only return true() if there is a Code element in the envelope that has a stringvalue other than the empty string
Pradeep0308Author Commented:
Thanks Geert. Will this return true() or only true?
Gertone (Geert Bormans)Information ArchitectCommented:
will return the node-set of all the *:Code elements that have non zero content

If you use //*:Code[string(.)] in a test (anywhere Xpath expects a boolean expression, example: inside the test attribute of an if statement in XSLT, inside an if() or inside the predicate [], ...)
then it will return an xs:boolean
true() when the node-set is not empty
false() when the node-set is empty

that is because of the casting rules from node-set to boolean
Pradeep0308Author Commented:
ok so what happens if I do CodeTest=//*:Code[string(.)]

Will CodeTest have the value as xs:boolean i.e. true()?
Gertone (Geert Bormans)Information ArchitectCommented:
no, it will contain the nodeset
but you can force the boolean cast

be carefull not to cast to boolean prematurely
if you have this
and you remember that Codetest is a nodeset, not a boolean,
it will cast to boolean more efficiently when you use it in a test
Pradeep0308Author Commented:
Thanks Geert. Do you provide any freelance consulting on XML and related technologies? Like Xpath and XSLT?

Gertone (Geert Bormans)Information ArchitectCommented:
Hi Pradeep,
Yes I do, I am a full time XML/XSLT freelance consultant.
If you want to contact me, you can push the "hire me" button or find my email adress on my profile page
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.