relative path between two element in xquery
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
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
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----- Van: basex-talk-bounces@mailman.uni-konstanz.de [mailto:basex-talk-bounces@mailman.uni-konstanz.de] Namens Dirk Kirsten Verzonden: vrijdag 15 augustus 2014 12:11 Aan: basex-talk@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
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@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
Marc, I’ve solved the issue in another way but thanks anyway for the code snippet. Rob. Van: Marc van Grootel [mailto:marc.van.grootel@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@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
participants (3)
-
Dirk Kirsten -
Marc van Grootel -
Rob Stapper