Forum Discussion

stiley's avatar
stiley
New Contributor
12 years ago

XPath Assertions

Hello, I have the following response from a very simple WebService

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetRandomResponse xmlns="http://seant/webservices/">
<GetRandomResult>15</GetRandomResult>
</GetRandomResponse>
</soap:Body>
</soap:Envelope>


I am asserting that the value in the <GetRandomResult> tag is numeric with the following XPath assertion (its working just fine)


declare namespace soap='http://schemas.xmlsoap.org/soap/envelope/';
declare namespace ns1='http://seant/webservices/';
matches(/soap:Envelope/soap:Body/*[namespace-uri()='http://seant/webservices/' and local-name()='GetRandomResponse']/*[namespace-uri()='http://seant/webservices/' and local-name()='GetRandomResult']/text(),'^\d{1,2}$')


This is quite a bit of typing so I would like to use this instead
matches(//GetRandomResult/text(),'^\d{1,2}$')
but it keeps failing and I am not sure why. I have validated this expression in other editors and its fine, just not sure what is going on.

Any thoughts would be appreciated
Sean

6 Replies

  • nmrao's avatar
    nmrao
    Community Hero
    May be the count is changing and the pattern is not matching along with namespace missing, a wild guess?

    Would you mind trying with pattern ^\d+$ instead of current one?

    matches(//ns1:GetRandomResult/text(),'^\d+$')


    Hoping the expected value is true
  • stiley's avatar
    stiley
    New Contributor
    Hi there, many thanks for the response.
    What you posted works just fine.

    I am not really up on the whole namespace thing so its not clear to me what the ns1 in your expression actually means / does?
    matches(//ns1:GetRandomResult/text(),'^\d+$')


    Sean
  • stiley's avatar
    stiley
    New Contributor
    Actually, nevermind, just had a look at the declared namespace in the wizard..

    declare namespace soap='http://schemas.xmlsoap.org/soap/envelope/';
    declare namespace ns1='http://seant/webservices/';
    matches(//ns1:GetRandomResult/text(),'^\d{1,2}$')I think I get it now.

    Sorry.

    Sean
  • nmrao's avatar
    nmrao
    Community Hero
    ^\d{1,2}$ - works for two digit numbers only, where as ^\d+$ works for any number of digits but there should be minimum 1 integer.
  • Hi,

    Is it possible to retrieve more than 1 data in REST service using X-Path assertion? .

    My XML is -
    <ul id="subjects">
    <li>
    <a class="uri" href="/OvidWS/journals/adisovft/subjects/Arts+%26+Humanities">
    <span class="name">Arts &amp; Humanities</span>
    </a>
    ; # Journals:
    <span class="num_journals">1</span>
    </li>
    <li>
    <a class="uri" href="/OvidWS/journals/adisovft/subjects/Arts+%26+Humanities%3ABusiness+%26+Economics">
    <span class="name">Arts &amp; Humanities:Business &amp; Economics</span>
    </a>

    It contains more than 100 names which i want to retrieve using X-Path. Is it possble?? Please reply.
  • nmrao's avatar
    nmrao
    Community Hero
    Retrieving one or many values should not be problem if you use right xpath. Are you facing any issue currently?