Try/catch

Thursday, 15 September 2016 · 7 messages
M
meumapple15 Sep 2016 13:28 #
Hi, I have been working with a collection of XML files, trying to make changes using the copy clause. It may happen that sometimes I get an error message because of one of the files I am trying to change. My question is whether there is a way to know which file and at which line/column the error occurs. I have tried try/catch but it seems not to be designed for this (or am I missing something here?). Thanks! Best, Joseph
CG
Christian Grün15 Sep 2016 13:38 #
Hi Joseph,
Quoted text · meumapple · 1 line
> or am I missing something here?
I guess so; Did you check the documentation of the function [1,2]? Christian [1] http://docs.basex.org/wiki/XQuery_3.0#Try.2FCatch [2] https://www.w3.org/TR/xquery-31/#id-try-catch
M
meumapple15 Sep 2016 14:02 #
Hi Christian, I use all the $err but the line error refers to the one inside my query not that of the file where the query cannot be applied (more precisely, a node cannot be replaced more than once, but I do not know where this problematic node is in my files)
Quoted text · Christian Grün · 1 line
Il giorno 15 set 2016, alle ore 13:38, Christian Grün <christian.gruen at gmail.com> ha scritto:
Hi Joseph,
Quoted text · Christian Grün · 1 line
> or am I missing something here?
I guess so; Did you check the documentation of the function [1,2]? Christian [1] http://docs.basex.org/wiki/XQuery_3.0#Try.2FCatch [2] https://www.w3.org/TR/xquery-31/#id-try-catch
CG
Christian Grün15 Sep 2016 14:11 #
Hi Joseph,
Quoted text · meumapple · 1 line
> I use all the $err but the line error refers to the one inside my query not that of the file where the query cannot be applied (more precisely, a node cannot be replaced more than once, but I do not know where this problematic node is in my files)
With “file”, do you mean an XML document? That’s not possible indeed. Maybe you can show us how your query looks like? Christian
Quoted text · meumapple · 10 lines
> Hi Joseph, > >> or am I missing something here? > > I guess so; Did you check the documentation of the function [1,2]? > > Christian > > [1] http://docs.basex.org/wiki/XQuery_3.0#Try.2FCatch > [2] https://www.w3.org/TR/xquery-31/#id-try-catch
J
Joseph15 Sep 2016 14:54 #
Yes, I mean the XML file. I using a query like the following. There are nodes for which the replace updating expression does not work, and I do not know how to identify the file(s) where the problem is. I could simply try to exclude files from the collection until the query works, but I was wondering whether there is a more intelligent way to do that. Thanks!
try {

for $f in collection("/mycollection/")
return
copy $e := $f
modify
(
for $x in $e//C[text()="*"][./following-sibling::C[matches(., "\p{L}")]]
let $y := $x/(following-sibling::C[matches(., "\p{L}")])[1]
return
replace value of node $y with upper-case($y)
)
return $e
}
catch * {
  'Error [' || $err:code || ']: ' || $err:description || ',' || $err:module
||',' ||$err:line-number || ','|| $err:column-number
  || ','|| $err:value || ','|| $err:additional
}
2016-09-15 14:11 GMT+02:00 Christian Grün <christian.gruen at gmail.com>:
Quoted text · Christian Grün · 25 lines
> Hi Joseph, > > > I use all the $err but the line error refers to the one inside my query > not that of the file where the query cannot be applied (more precisely, a > node cannot be replaced more than once, but I do not know where this > problematic node is in my files) > > With “file”, do you mean an XML document? That’s not possible indeed. > Maybe you can show us how your query looks like? > > Christian > > > > > Hi Joseph, > > > >> or am I missing something here? > > > > I guess so; Did you check the documentation of the function [1,2]? > > > > Christian > > > > [1] http://docs.basex.org/wiki/XQuery_3.0#Try.2FCatch > > [2] https://www.w3.org/TR/xquery-31/#id-try-catch >
CG
Christian Grün15 Sep 2016 15:08 #
Something like this might do the trick (it only works if the C elements have no descendant C elements):
  for $f in collection("/mycollection/")
  return
    copy $e := $f
    modify (
      for $c in $e//C[text() = "*"]/
        (following-sibling::C[matches(., "\p{L}")])[1]
      return replace value of node $c with upper-case($c)
    )
    return $e
By collecting all relevant nodes in a single path expression, duplicates will be filtered out.
Quoted text · Joseph · 58 lines
On Thu, Sep 15, 2016 at 2:54 PM, Joseph <meumapple at gmail.com> wrote: > Yes, I mean the XML file. I using a query like the following. There are > nodes for which the replace updating expression does not work, and I do not > know how to identify the file(s) where the problem is. I could simply try to > exclude files from the collection until the query works, but I was wondering > whether there is a more intelligent way to do that. Thanks! > > > try { > > for $f in collection("/mycollection/") > return > copy $e := $f > modify > ( > for $x in $e//C[text()="*"][./following-sibling::C[matches(., "\p{L}")]] > let $y := $x/(following-sibling::C[matches(., "\p{L}")])[1] > return > replace value of node $y with upper-case($y) > ) > return $e > } > catch * { > 'Error [' || $err:code || ']: ' || $err:description || ',' || $err:module > ||',' ||$err:line-number || ','|| $err:column-number > || ','|| $err:value || ','|| $err:additional > } > > > > 2016-09-15 14:11 GMT+02:00 Christian Grün <christian.gruen at gmail.com>: >> >> Hi Joseph, >> >> > I use all the $err but the line error refers to the one inside my query >> > not that of the file where the query cannot be applied (more precisely, a >> > node cannot be replaced more than once, but I do not know where this >> > problematic node is in my files) >> >> With “file”, do you mean an XML document? That’s not possible indeed. >> Maybe you can show us how your query looks like? >> >> Christian >> >> >> >> > Hi Joseph, >> > >> >> or am I missing something here? >> > >> > I guess so; Did you check the documentation of the function [1,2]? >> > >> > Christian >> > >> > [1] http://docs.basex.org/wiki/XQuery_3.0#Try.2FCatch >> > [2] https://www.w3.org/TR/xquery-31/#id-try-catch > >
J
Joseph15 Sep 2016 18:00 #
Seems to work! Thanks! 2016-09-15 15:08 GMT+02:00 Christian Grün <christian.gruen at gmail.com>:
Quoted text · Christian Grün · 84 lines
> Something like this might do the trick (it only works if the C > elements have no descendant C elements): > > for $f in collection("/mycollection/") > return > copy $e := $f > modify ( > for $c in $e//C[text() = "*"]/ > (following-sibling::C[matches(., "\p{L}")])[1] > return replace value of node $c with upper-case($c) > ) > return $e > > By collecting all relevant nodes in a single path expression, > duplicates will be filtered out. > > > > > On Thu, Sep 15, 2016 at 2:54 PM, Joseph <meumapple at gmail.com> wrote: > > Yes, I mean the XML file. I using a query like the following. There are > > nodes for which the replace updating expression does not work, and I do > not > > know how to identify the file(s) where the problem is. I could simply > try to > > exclude files from the collection until the query works, but I was > wondering > > whether there is a more intelligent way to do that. Thanks! > > > > > > try { > > > > for $f in collection("/mycollection/") > > return > > copy $e := $f > > modify > > ( > > for $x in $e//C[text()="*"][./following-sibling::C[matches(., "\p{L}")]] > > let $y := $x/(following-sibling::C[matches(., "\p{L}")])[1] > > return > > replace value of node $y with upper-case($y) > > ) > > return $e > > } > > catch * { > > 'Error [' || $err:code || ']: ' || $err:description || ',' || > $err:module > > ||',' ||$err:line-number || ','|| $err:column-number > > || ','|| $err:value || ','|| $err:additional > > } > > > > > > > > 2016-09-15 14:11 GMT+02:00 Christian Grün <christian.gruen at gmail.com>: > >> > >> Hi Joseph, > >> > >> > I use all the $err but the line error refers to the one inside my > query > >> > not that of the file where the query cannot be applied (more > precisely, a > >> > node cannot be replaced more than once, but I do not know where this > >> > problematic node is in my files) > >> > >> With “file”, do you mean an XML document? That’s not possible indeed. > >> Maybe you can show us how your query looks like? > >> > >> Christian > >> > >> > >> > >> > Hi Joseph, > >> > > >> >> or am I missing something here? > >> > > >> > I guess so; Did you check the documentation of the function [1,2]? > >> > > >> > Christian > >> > > >> > [1] http://docs.basex.org/wiki/XQuery_3.0#Try.2FCatch > >> > [2] https://www.w3.org/TR/xquery-31/#id-try-catch > > > > >

⚡Generated with XQuery