How to extract value from fn:analyze-string

Thursday, 10 February 2022 · 5 messages · last reply 11 Feb 2022
BE
Ben Engbers10 Feb 2022 18:30 #
Hi, This query produces the following result:
let $debates := collection("Parliament")
for $debate-item in $debates
   let $item-file := $debate-item/officiele-publicatie//meta/@content
   let $debate-id := fn:analyze-string(
     $debate-item/officiele-publicatie//meta/@content,
"(\d{8}-\d*)-(\d*)")
   return ($debate-id)
=>
<analyze-string-result xmlns="http://www.w3.org/2005/xpath-functions">
   <non-match>https://zoek.officielebekendmakingen.nl/h-tk-</non-match>
   <match>
     <group nr="1">20202021-102</group>-<group nr="2">1</group>
   </match>
   <non-match>/metadata.xml</non-match>
</analyze-string-result>
...
I am trying to extract the values from group 1 and 2 but this query returns 0 results:
let $debates := collection("Parliament")
for $debate-item in $debates
   let $item-file := $debate-item/officiele-publicatie//meta/@content
   let $debate-id := fn:analyze-string(
     $debate-item/officiele-publicatie//meta/@content, "(\d{8}-\d*)-(\d*)")

     let $debate-nr := $debate-id//match/group[@nr="1"]/text()
     let $item-nr := $debate-id//match/group[@nr="2"]/text()
return ($debate-nr, $item-nr) My guess is that analyze-string inserts new elements in the query and that that is the reason why this does not work. How can I extract debate-nr and item-nr from $debate-id? Ben Engbers
IL
Imsieke, Gerrit, le-tex10 Feb 2022 18:46 #
It’s a namespace thing. The analyze-string() result is in the http://www.w3.org/2005/xpath-functions namespace, which is bound to the fn prefix. So you should write fn:match etc. instead of match, or, as Bridger suggested, *:match. But such a wildcard always seems a bit desperate to me (no offense, Bridger ;). Whereas you don’t need to use the privileged fn prefix when you invoke analyze-string(), it’s only important when you select the namespaced results. Gerrit
Quoted text · Ben Engbers · 43 lines
On 10.02.2022 18:30, Ben Engbers wrote: > Hi, > > This query produces the following result: > > let $debates := collection("Parliament") > for $debate-item in $debates > let $item-file := $debate-item/officiele-publicatie//meta/@content > let $debate-id := fn:analyze-string( > $debate-item/officiele-publicatie//meta/@content, "(\d{8}-\d*)-(\d*)") > return ($debate-id) > > => > > <analyze-string-result xmlns="http://www.w3.org/2005/xpath-functions"> > <non-match>https://zoek.officielebekendmakingen.nl/h-tk-</non-match> > <match> > <group nr="1">20202021-102</group>-<group nr="2">1</group> > </match> > <non-match>/metadata.xml</non-match> > </analyze-string-result> > ... > > I am trying to extract the values from group 1 and 2 but this query > returns 0 results: > > let $debates := collection("Parliament") > for $debate-item in $debates > let $item-file := $debate-item/officiele-publicatie//meta/@content > let $debate-id := fn:analyze-string( > $debate-item/officiele-publicatie//meta/@content, "(\d{8}-\d*)-(\d*)") > > let $debate-nr := $debate-id//match/group[@nr="1"]/text() > let $item-nr := $debate-id//match/group[@nr="2"]/text() > > return ($debate-nr, $item-nr) > > My guess is that analyze-string inserts new elements in the query and > that that is the reason why this does not work. > > How can I extract debate-nr and item-nr from $debate-id? > > Ben Engbers
BD
Bridger Dyson-Smithin reply to Ben Engbers, 10 Feb 2022, 18:3010 Feb 2022 19:40 #
Include the namespace, or put in a wildcard; eg
let $debate-nr := $debate-id//*:match/*:group[@nr="1"]/text()
     let $item-nr := $debate-id//*:match/*:group[@nr="2"]/text()
I'm mobile at the moment, but think that's right (: untested and off the cuff, in other words :). Best, Bridger
Quoted text · Ben Engbers · 46 lines
On Thu, Feb 10, 2022, 12:30 PM Ben Engbers <Ben.Engbers at be-logical.nl> wrote: > Hi, > > This query produces the following result: > > let $debates := collection("Parliament") > for $debate-item in $debates > let $item-file := $debate-item/officiele-publicatie//meta/@content > let $debate-id := fn:analyze-string( > $debate-item/officiele-publicatie//meta/@content, > "(\d{8}-\d*)-(\d*)") > return ($debate-id) > > => > > <analyze-string-result xmlns="http://www.w3.org/2005/xpath-functions"> > <non-match>https://zoek.officielebekendmakingen.nl/h-tk-</non-match> > <match> > <group nr="1">20202021-102</group>-<group nr="2">1</group> > </match> > <non-match>/metadata.xml</non-match> > </analyze-string-result> > ... > > I am trying to extract the values from group 1 and 2 but this query > returns 0 results: > > let $debates := collection("Parliament") > for $debate-item in $debates > let $item-file := $debate-item/officiele-publicatie//meta/@content > let $debate-id := fn:analyze-string( > $debate-item/officiele-publicatie//meta/@content, "(\d{8}-\d*)-(\d*)") > > let $debate-nr := $debate-id//match/group[@nr="1"]/text() > let $item-nr := $debate-id//match/group[@nr="2"]/text() > > return ($debate-nr, $item-nr) > > My guess is that analyze-string inserts new elements in the query and > that that is the reason why this does not work. > > How can I extract debate-nr and item-nr from $debate-id? > > Ben Engbers >
BE
Ben Engbersin reply to Imsieke, Gerrit, le-tex, 10 Feb 2022, 18:4610 Feb 2022 19:57 #
I'll experiment a little with the namespace but for the moment adding *: works! Thanks, Ben
Quoted text · Imsieke, Gerrit, le-tex · 58 lines
Op 10-02-2022 om 18:46 schreef Imsieke, Gerrit, le-tex: > It’s a namespace thing. The analyze-string() result is in the > http://www.w3.org/2005/xpath-functions namespace, which is bound to the > fn prefix. So you should write fn:match etc. instead of match, or, as > Bridger suggested, *:match. But such a wildcard always seems a bit > desperate to me (no offense, Bridger ;). > Whereas you don’t need to use the privileged fn prefix when you invoke > analyze-string(), it’s only important when you select the namespaced > results. > > Gerrit > > On 10.02.2022 18:30, Ben Engbers wrote: >> Hi, >> >> This query produces the following result: >> >> let $debates := collection("Parliament") >> for $debate-item in $debates >> let $item-file := $debate-item/officiele-publicatie//meta/@content >> let $debate-id := fn:analyze-string( >> $debate-item/officiele-publicatie//meta/@content, >> "(\d{8}-\d*)-(\d*)") >> return ($debate-id) >> >> => >> >> <analyze-string-result xmlns="http://www.w3.org/2005/xpath-functions"> >> <non-match>https://zoek.officielebekendmakingen.nl/h-tk-</non-match> >> <match> >> <group nr="1">20202021-102</group>-<group nr="2">1</group> >> </match> >> <non-match>/metadata.xml</non-match> >> </analyze-string-result> >> ... >> >> I am trying to extract the values from group 1 and 2 but this query >> returns 0 results: >> >> let $debates := collection("Parliament") >> for $debate-item in $debates >> let $item-file := $debate-item/officiele-publicatie//meta/@content >> let $debate-id := fn:analyze-string( >> $debate-item/officiele-publicatie//meta/@content, >> "(\d{8}-\d*)-(\d*)") >> >> let $debate-nr := $debate-id//match/group[@nr="1"]/text() >> let $item-nr := $debate-id//match/group[@nr="2"]/text() >> >> return ($debate-nr, $item-nr) >> >> My guess is that analyze-string inserts new elements in the query and >> that that is the reason why this does not work. >> >> How can I extract debate-nr and item-nr from $debate-id? >> >> Ben Engbers >
BD
Bridger Dyson-Smithin reply to Imsieke, Gerrit, le-tex, 10 Feb 2022, 18:4611 Feb 2022 19:17 #
Gerrit! You wound me - I'm cut to the very core :) For the life of me, I couldn't remember if the fn prefix was correct or not. @Ben - Gerrit's absolutely right - wildcards are desperate measures (or for quick, one-off scripts). Using the namespace is much preferable. Best, Bridger
Quoted text · le-tex · 60 lines
On Thu, Feb 10, 2022, 12:46 PM Imsieke, Gerrit, le-tex < gerrit.imsieke at le-tex.de> wrote: > It’s a namespace thing. The analyze-string() result is in the > http://www.w3.org/2005/xpath-functions namespace, which is bound to the > fn prefix. So you should write fn:match etc. instead of match, or, as > Bridger suggested, *:match. But such a wildcard always seems a bit > desperate to me (no offense, Bridger ;). > Whereas you don’t need to use the privileged fn prefix when you invoke > analyze-string(), it’s only important when you select the namespaced > results. > > Gerrit > > On 10.02.2022 18:30, Ben Engbers wrote: > > Hi, > > > > This query produces the following result: > > > > let $debates := collection("Parliament") > > for $debate-item in $debates > > let $item-file := $debate-item/officiele-publicatie//meta/@content > > let $debate-id := fn:analyze-string( > > $debate-item/officiele-publicatie//meta/@content, > "(\d{8}-\d*)-(\d*)") > > return ($debate-id) > > > > => > > > > <analyze-string-result xmlns="http://www.w3.org/2005/xpath-functions"> > > <non-match>https://zoek.officielebekendmakingen.nl/h-tk-</non-match> > > <match> > > <group nr="1">20202021-102</group>-<group nr="2">1</group> > > </match> > > <non-match>/metadata.xml</non-match> > > </analyze-string-result> > > ... > > > > I am trying to extract the values from group 1 and 2 but this query > > returns 0 results: > > > > let $debates := collection("Parliament") > > for $debate-item in $debates > > let $item-file := $debate-item/officiele-publicatie//meta/@content > > let $debate-id := fn:analyze-string( > > $debate-item/officiele-publicatie//meta/@content, > "(\d{8}-\d*)-(\d*)") > > > > let $debate-nr := $debate-id//match/group[@nr="1"]/text() > > let $item-nr := $debate-id//match/group[@nr="2"]/text() > > > > return ($debate-nr, $item-nr) > > > > My guess is that analyze-string inserts new elements in the query and > > that that is the reason why this does not work. > > > > How can I extract debate-nr and item-nr from $debate-id? > > > > Ben Engbers > >

⚡Generated with XQuery