[MarkLogic Dev General] Attribute insertion using node-insert-child

Eric Palmitesta eric.palmitesta at utoronto.ca
Wed Sep 17 09:42:45 PDT 2008


Go figure, Priscilla Walmsley's XQuery (sitting on my desk the whole 
time, http://www.amazon.com/gp/product/0596006349) has a section 
entitled "Adding Attributes to an Element".  It's nearly identical to 
the function I've been using (with your help):

define function util:add-attribute($element as element(), $name as 
xs:string, $value as xs:anyAtomicType?)
{
	element { node-name($element) } {
		attribute { $name } { $value },
		$element/@*,
		$element/node()
	}
}

Only difference is she's using $value as xs:anyAtomicType? (I had $value 
as xs:string, definitely not as flexible) and constructing and inserting 
the new attribute before the old ones.

My question is this: how expensive is this element/attribute 
reconstruction?  For example, a large xhtml structure full of div, p, a, 
span, table, tr, th, td, and actual data, can easily be 10kb.

Are there ML facilities for measuring expense of specific function calls?

Eric

Mary Holstege wrote:
> On Mon, 15 Sep 2008 12:44:47 -0700, Eric Palmitesta 
> <eric.palmitesta at utoronto.ca> wrote:
> 
>> Hey Mary, this seems fundamentally important, can you elaborate?
>>
>> (For example why x$/node() rather than $x/child::*)
>>
>> Eric
>>
> 
> Because $x/node() matches all child nodes, be there text, element, 
> comments,
> or processing instructions, whereas $x/child::* only selects elements.
> This is because wildcard tests only match against the "principal node
> kind" of the axis. In the case of child:: this is element.  So the
> query as written with $x/text(), $x/child::* means: take all the text
> content of the node, put it up front, then follow it by all the elements.
> 
> So:
>    <scramble>Here<!-- with a comment --> is a <b>simple</b> 
> example</scramble>
> would come out of function g as:
>    <scamble new="new">Here is a  example<b>simple</b></scramble>
> 
> //Mary
> 
>> Mary Holstege wrote:
>>> On Mon, 15 Sep 2008 10:55:08 -0700, Florent Georges 
>>> <lists at fgeorges.org> wrote:
>>>
>>>> Eric Palmitesta wrote:
>>>>
>>>>   Hi
>>>>
>>>>> It works.  Is there a better way though?  Also, what's the
>>>>> point of xdmp:node-insert-child if it can't operate on an
>>>>> argument to a function?
>>>>
>>>>   Yes it can, but not on constructed nodes.  You can make a bit
>>>> more generic:
>>>>
>>>>     define function g($x as element()) as element()
>>>>     {
>>>>        element { node-name($x) } {
>>>>          $x/text(),
>>>>          $x/child::*,
>>>>          $x/attribute::*,
>>>>          attribute new{ "new" }
>>>>        }
>>>>     }
>>>>
>>>  Actually, you need to be a little careful here.
>>> Attributes should come first, and the query above will reorder
>>> text children relative to your element children.  What you want is:
>>>  element {node-name($x)} {
>>>   $x/@*,
>>>   attribute new {"new"},
>>>   $x/node()
>>> }
>>>  //Mary
>>> _______________________________________________
>>> General mailing list
>>> General at developer.marklogic.com
>>> http://xqzone.com/mailman/listinfo/general
>> _______________________________________________
>> General mailing list
>> General at developer.marklogic.com
>> http://xqzone.com/mailman/listinfo/general
> 
> 
> _______________________________________________
> General mailing list
> General at developer.marklogic.com
> http://xqzone.com/mailman/listinfo/general


More information about the General mailing list