[xquery-talk] XQuery 3 try/catch and duplicate attributes

David Sewell dsewell at virginia.edu
Thu Dec 19 11:23:07 PST 2013


Thanks. As I say, I'm cleaning up legacy code that has a lot of mess in it, and 
in fact a try/catch isn't the solution I'll be using to avoid the problem in 
this case; I was just curious about the (in)ability of try/catch to prevent 
duplicate attributes within an element constructor. Clearly there are better 
ways to prevent them than relying on that.

David

On Thu, 19 Dec 2013, Ryan Dew wrote:

> Adding to this, my personal opinion is that try/catches should be a last
> resort and you'll be a better programmer for avoiding them. Not to mention,
> you can often see performance increases by avoiding them if the code is
> something that is called repeatedly and are less likely to unintentionally
> mask problems. For example you can do the following (please excuse the
> formatting):
>
> xquery version "3.0";
> let $element := <el att1="one" att2="two" id="old-id">text</el>
> return element new {
>  $element/attribute(),
>  if (fn:empty($element/@id)) then attribute id { "new-id" } else (),
>  $element/node()
> }
>
>
>
> On Thu, Dec 19, 2013 at 11:31 AM, Christian Grün
> <christian.gruen at gmail.com>wrote:
>
>> Hi David,
>>
>>> […]
>>>   try {attribute id { "new-id" }} catch * {()},
>>> […]
>>>
>>> but it doesn't trap the error (in any XQuery 3 processor I've checked).
>> The
>>> try/catch will work only if put around the entire element constructor.
>>
>> The reason for this is that the attribute constructor itself is
>> correct. The error occurs when trying to add the newly created
>> attribute to the element.
>>
>> The following query should work:
>>
>> let $element := <el att1="one" att2="two" id="old-id">text</el>
>> return element new {
>>   $element/attribute(),
>>   if($element/@id) then () else attribute id { "new-id" },
>>   $element/node()
>> }
>>
>> Hope this helps,
>> Christian
>>
>> _______________________________________________
>> talk at x-query.com
>> http://x-query.com/mailman/listinfo/talk
>>
>

-- 
David Sewell, Editorial and Technical Manager
ROTUNDA, The University of Virginia Press
PO Box 400314, Charlottesville, VA 22904-4314 USA
Email: dsewell at virginia.edu   Tel: +1 434 924 9973
Web: http://rotunda.upress.virginia.edu/


More information about the talk mailing list