0

I'm facing an issue with the parsing of a REST WebRequest' response

I'm using XQuery

For many years, I used a query which worked fine in my software (OneAutomation from Broadcom, formely Automic) I have updated the software to the next major version and now the query still work but the result is always empty I have tried a lot of different syntax but without success

Here is the query which worked with the old version of the software :

for $x in $input/BackgroundProcess/*/Process
return
if (($x/xs:string(@Created) > xs:string("&LastExecution#")) = true()) then
$x/xs:string(@Created)
else ()

The variable &LastExecution# contains a date (format for example : 2019-08-06 22:00:15 CEST) which is get by another GET Webrequest.

Here is a response example :

<BackgroundProcess>
  <Ended> 
    <Process Created="2019-08-06 22:00:15 CEST" Description="Export started for endpoint 'OIEP_ATTRIBUT' (2019-08-06 22:00:15)" Finished="2019-08-06 22:00:59 CEST" NumberOfErrors="0" NumberOfWarnings="0" Progress="0" StartedBy="STEPSYS" Started="2019-08-06 22:00:20 CEST" Status="succeeded" ID="BGP_1144216"/>
    <Process Created="2019-08-07 22:00:27 CEST" Description="Export started for endpoint 'OIEP_ATTRIBUT' (2019-08-07 22:00:27)" Finished="2019-08-07 22:01:15 CEST" NumberOfErrors="0" NumberOfWarnings="0" Progress="0" StartedBy="STEPSYS" Started="2019-08-07 22:00:32 CEST" Status="succeeded" ID="BGP_1152321"/>
  </Ended>
</BackgroundProcess>

The expected result is to have the most recent "Created" date, superior to the date passed in LastExecution#

Any help would be much appreciated

Edit : The following query works but I can't manage to get only the value of the attribute @Created

for $x in $input/BackgroundProcess/*/Process
return
if ($x [@Created > "&LastExcution#"]) then
$x
else()

It return the whole element :

<Process Created="2019-08-07 22:00:27 CEST" Description="Export started for endpoint 'OIEP_ATTRIBUT' (2019-08-07 22:00:27)" Finished="2019-08-07 22:01:15 CEST" NumberOfErrors="0" NumberOfWarnings="0" Progress="0" StartedBy="STEPSYS" Started="2019-08-07 22:00:32 CEST" Status="succeeded" ID="BGP_1152321"/> 

2 Answers2

1

I used latest BaseX 9.2.4 to test your XQuery. I ended up with the following:

XQuery

let $input := <BackgroundProcess>
  <Ended> 
    <Process Created="2019-08-06 22:00:15 CEST" Description="Export started for endpoint 'OIEP_ATTRIBUT' (2019-08-06 22:00:15)" Finished="2019-08-06 22:00:59 CEST" NumberOfErrors="0" NumberOfWarnings="0" Progress="0" StartedBy="STEPSYS" Started="2019-08-06 22:00:20 CEST" Status="succeeded" ID="BGP_1144216"/>
    <Process Created="2019-08-07 22:00:27 CEST" Description="Export started for endpoint 'OIEP_ATTRIBUT' (2019-08-07 22:00:27)" Finished="2019-08-07 22:01:15 CEST" NumberOfErrors="0" NumberOfWarnings="0" Progress="0" StartedBy="STEPSYS" Started="2019-08-07 22:00:32 CEST" Status="succeeded" ID="BGP_1152321"/>
  </Ended>
</BackgroundProcess>

let $LastExecution := '2019-08-06 22:00:15 CEST'

for $x in $input//Process
return 
if ($x [@Created > $LastExecution]) then
  data($x/@Created)
else()
Yitzhak Khabinsky
  • 18,471
  • 2
  • 15
  • 21
0

That syntax in both samples where you say you access a variable seems to be a proprietary extension of your product, in standard XQuery a variable reference is just $var.

Anyway, it seems the part if ($x [@Created > "&LastExcution#"]) then $x could just be changed to if ($x [@Created > "&LastExcution#"]) then $x/@Created if your intent is to return the attribute node or if ($x [@Created > "&LastExcution#"]) then xs:string($x/@Created) if your intent is to return a string value of the attribute node.

In general, $input/BackgroundProcess/*/Process/@Created[. > "&LastExcution#"]/string() should do, there is no gain by using the for expression. But that is partly a question of personal preference of coding style and might be based on my inclination to use a plain XPath expression instead of a for expression.

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
  • Thank you for your answer. My intent is to have the string value. Your 3 suggestions give me an empty result –  Aug 08 '19 at 17:59
  • And you're right about the variable. It is translated by the software before executing the XQuery. The value of this variable for my test is '2019-08-06 22:00:15 CEST' (without single quotes) –  Aug 08 '19 at 18:03
  • Edit : your first suggestion give me an error : java.lang.RuntimeException: java.lang.RuntimeException: java.lang.RuntimeException: java.lang.Exception: ERROR Cannot execute query. ch.ethz.mxquery.exceptions.DynamicException: No Attributes with an enclosing element allowed –  Aug 08 '19 at 18:05
  • The only problem with the XPath solution is that I also need the "ID" value of the element. Do you have any suggestion ? –  Aug 08 '19 at 18:17
  • Ok, I found the solution for the ID : $input/BackgroundProcess/*/Process[@Created>"&LastExecution#"]/@ID/string(). Thanks for your help again ! –  Aug 08 '19 at 18:32