relative path between two element in xquery
Friday, 15 August 2014 · 5 messages
RS
Rob Stapper15 Aug 2014 11:55 #
Hi,
How can I construct the relative path between two elements in an xml-file.
Most probably overlooking the obvious I ended up with the solution below, which, unfortunately, triggers an runtime-error.
My not-working solution:
let $xml :=
<xml>
<A>
<B/>
</A>
</xml>
let $b := $xml//B
let $a := $xml//Areturn replace( string( path( $b))
, string( path( $a))
, ''
)
Two questions:
- How to retrieve the relative path between two element within a xml-file?
- Why does the solution above trigger an error? Can it be a bug? ( oops, three questions)
Thnx in advance for the reply,
Rob Stapper
---
Dit e-mailbericht bevat geen virussen en malware omdat avast! Antivirus-bescherming actief is. http://www.avast.com
DK
Dirk Kirsten15 Aug 2014 12:11 #
Hello Rob,
no, this not a bug. replace() uses regex for its pattern and as the path has {} and [] in them they will be interpreted as regex. You could write your own replace function or something trivial (but quite limited) like using substring and string-length, e.g.:
substring(path($b), string-length(path($a)) + 1)Cheers,
Dirk
Quoted text · Rob Stapper · 64 lines
On 15/08/14 11:55, Rob Stapper wrote: > Hi, > > > > How can I construct the relative path between two elements in an xml-file. > > Most probably overlooking the obvious I ended up with the solution below, > which, unfortunately, triggers an runtime-error. > > > > My not-working solution: > > > > let $xml := > > <xml> > > <A> > > <B/> > > </A> > > </xml> > > let $b := $xml//B > > let $a := $xml//A > > > > return replace( string( path( $b)) > > , string( path( $a)) > > , '' > > ) > > > > Two questions: > > - How to retrieve the relative path between two element within a > xml-file? > > - Why does the solution above trigger an error? Can it be a bug? ( > oops, three questions) > > > > Thnx in advance for the reply, > > Rob Stapper > > > > --- > Dit e-mailbericht bevat geen virussen en malware omdat avast! Antivirus-bescherming actief is. > http://www.avast.com >
--
Dirk Kirsten, BaseX GmbH, http://basex.org
|-- Firmensitz: Blarerstrasse 56, 78462 Konstanz
|-- Registergericht Freiburg, HRB: 708285, Geschäftsführer:
| Dr. Christian Grün, Dr. Alexander Holupirek, Michael Seiferle `-- Phone: 0049 7531 28 28 676, Fax: 0049 7531 20 05 22
MG
Hey Rob,
In my lib I have a function that protects all regexp special characters. Maybe a bit too much heavy machinery for what it does but it works ;-) I gladly receive improvements.
declare function route:re-escape($string as xs:string)
as xs:string {
let $regexp-chars := ('.','*','+','|','?','(',')','[',']','{','}','^')
return
(: Note that '\' and '$' in the fold caused invalid pattern errors
therefore put them in separate replace :)
fold-left(
$regexp-chars,
replace(replace($string, '\\', '\\\\'), '\$', '\\\$'),
function($a, $b) {
replace($a, '\'||$b, '\\'||$b )
}
)
};--Marc
Quoted text · Rob Stapper · 67 lines
On Fri, Aug 15, 2014 at 11:55 AM, Rob Stapper <r.stapper at lijbrandt.nl> wrote: > Hi, > > > > How can I construct the relative path between two elements in an xml-file. > > Most probably overlooking the obvious I ended up with the solution below, > which, unfortunately, triggers an runtime-error. > > > > My not-working solution: > > > > let $xml := > > <xml> > > <A> > > <B/> > > </A> > > </xml> > > let $b := $xml//B > > let $a := $xml//A > > > > return replace( string( path( $b)) > > , string( path( $a)) > > , '' > > ) > > > > Two questions: > > - How to retrieve the relative path between two element within a > xml-file? > > - Why does the solution above trigger an error? Can it be a bug? > ( oops, three questions) > > > > Thnx in advance for the reply, > > Rob Stapper > > > ------------------------------ > <http://www.avast.com/> > > Dit e-mailbericht bevat geen virussen en malware omdat avast! Antivirus > <http://www.avast.com/> actief is. > >
--
--Marc
RS
Hi Dirk,
Thanx for your quick response.
< no, this not a bug. replace() uses regex for its pattern and as the path has {} and [] in them they will be interpreted as regex. >
Hmm, wasn't aware of that, surprising.
< You could write your own replace function or something trivial (but quite limited) like using substring and string-length, e.g.:
substring(path($b), string-length(path($a)) + 1) >
Thnx, for the suggestion. This will do for now.
Greetings,
Rob
-----Oorspronkelijk bericht-----
Quoted text · basex-talk-bounces at mailman.uni-konstanz.de · 88 lines
Van: basex-talk-bounces at mailman.uni-konstanz.de [mailto:basex-talk-bounces at mailman.uni-konstanz.de] Namens Dirk Kirsten Verzonden: vrijdag 15 augustus 2014 12:11 Aan: basex-talk at mailman.uni-konstanz.de Onderwerp: Re: [basex-talk] relative path between two element in xquery Hello Rob, no, this not a bug. replace() uses regex for its pattern and as the path has {} and [] in them they will be interpreted as regex. You could write your own replace function or something trivial (but quite limited) like using substring and string-length, e.g.: substring(path($b), string-length(path($a)) + 1) Cheers, Dirk On 15/08/14 11:55, Rob Stapper wrote: > Hi, > > > > How can I construct the relative path between two elements in an xml-file. > > Most probably overlooking the obvious I ended up with the solution > below, which, unfortunately, triggers an runtime-error. > > > > My not-working solution: > > > > let $xml := > > <xml> > > <A> > > <B/> > > </A> > > </xml> > > let $b := $xml//B > > let $a := $xml//A > > > > return replace( string( path( $b)) > > , string( path( $a)) > > , '' > > ) > > > > Two questions: > > - How to retrieve the relative path between two element within a > xml-file? > > - Why does the solution above trigger an error? Can it be a bug? ( > oops, three questions) > > > > Thnx in advance for the reply, > > Rob Stapper > > > > --- > Dit e-mailbericht bevat geen virussen en malware omdat avast! Antivirus-bescherming actief is. > http://www.avast.com > -- Dirk Kirsten, BaseX GmbH, http://basex.org |-- Firmensitz: Blarerstrasse 56, 78462 Konstanz |-- Registergericht Freiburg, HRB: 708285, Geschäftsführer: | Dr. Christian Grün, Dr. Alexander Holupirek, Michael Seiferle `-- Phone: 0049 7531 28 28 676, Fax: 0049 7531 20 05 22 --- Dit e-mailbericht bevat geen virussen en malware omdat avast! Antivirus-bescherming actief is. http://www.avast.com
RS
Marc,
I’ve solved the issue in another way but thanks anyway for the code snippet.
Rob.
Quoted text · Marc van Grootel · 53 lines
Van: Marc van Grootel [mailto:marc.van.grootel at gmail.com] Verzonden: vrijdag 15 augustus 2014 12:50 Aan: Rob Stapper CC: BaseX Onderwerp: Re: [basex-talk] relative path between two element in xquery Hey Rob, In my lib I have a function that protects all regexp special characters. Maybe a bit too much heavy machinery for what it does but it works ;-) I gladly receive improvements. declare function route:re-escape($string as xs:string) as xs:string { let $regexp-chars := ('.','*','+','|','?','(',')','[',']','{','}','^') return (: Note that '\' and '$' in the fold caused invalid pattern errors therefore put them in separate replace :) fold-left( $regexp-chars, replace(replace($string, '\\', '\\\\'), '\$', '\\\$'), function($a, $b) { replace($a, '\'||$b, '\\'||$b ) } ) }; --Marc On Fri, Aug 15, 2014 at 11:55 AM, Rob Stapper <r.stapper at lijbrandt.nl> wrote: Hi, How can I construct the relative path between two elements in an xml-file. Most probably overlooking the obvious I ended up with the solution below, which, unfortunately, triggers an runtime-error. My not-working solution: let $xml := <xml> <A> <B/> </A> </xml> let $b := $xml//B let $a := $xml//A return replace( string( path( $b)) , string( path( $a)) , '' ) Two questions: - How to retrieve the relative path between two element within a xml-file? - Why does the solution above trigger an error? Can it be a bug? ( oops, three questions) Thnx in advance for the reply, Rob Stapper _____ <http://www.avast.com/> Afbeelding verwijderd door afzender. Dit e-mailbericht bevat geen virussen en malware omdat avast! Antivirus <http://www.avast.com/> actief is. -- --Marc --- Dit e-mailbericht bevat geen virussen en malware omdat avast! Antivirus-bescherming actief is. http://www.avast.com
Written for BaseX 7.9. BaseX may have changed since: please check the current documentation.
⚡Generated with XQuery