[MarkLogic Dev General] question about namespace as attribute

helen chen hchen at aip.org
Mon Aug 30 07:31:05 PDT 2010


Hi Georges and David,

I tried the code (I list at the end)  and I found no matter what I do, the namespace binding will eventually show up at the point that the data shows up.  It is added as needed.  I'm trying to convert one xml to another xml with adding/changing some information.

I was thinking that the original raw data has this namespace binding at the top and I wanted to keep it, thinking about your question: "Why would you like to copy this namespace binding?" , I think you are right. The data will be correct, it is just where the namespace binding shows up.  Maybe I should not worry about keeping the namespace binding at the top.

Thanks a lot for all your suggestions.

Helen



xquery version "1.0-ml";

declare function local:add-ns-node($elem   as element(),
    $prefix as xs:string,  $ns-uri as xs:string  ) as element()
{
  element { QName($ns-uri, concat($prefix, ":x")) }{ $elem }/*
};


let $a := <article xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns="mynamespace">
<subnode>test</subnode>
<m:a>test mnode</m:a>
</article>

return
  element {fn:QName("mynamespace","article")}
  {
    $a/@*
    ,
(:
    for $subnode in $a/node()
    let $ns := fn:namespace-uri-from-QName(fn:node-name($subnode))
    return
      if($ns eq "http://www.w3.org/1998/Math/MathML")
      then
        local:add-ns-node($subnode, "m", "http://www.w3.org/1998/Math/MathML")
      else $subnode
:)
    $a/node()

  }




On Aug 28, 2010, at 8:03 AM, Florent Georges wrote:

> helen chen wrote:
> 
>  Hi,
> 
>> I missed the namespace definition in the original article node
>> xmlns:m="http://www.w3.org/1998/Math/MathML" in my new article
>> node.  [...]  It looks like the $a/@* does not put the
>> namespace attribute there.
> 
>  Yes, even though they are serialized in a similar way, XDM
> defines attributes and namespaces as two very different beasts.
> 
>> How can I get all the attributes into my new node dynamically
>> so I know I didn't miss anything?
> 
>  If you really want to copy the in-scope bindings at some point,
> you should be able to define a function to do so, by using both
> fn:namespace-uri-for-prefix() and fn:in-scope-prefixes(), and the
> following trick:
> 
> http://fgeorges.blogspot.com/2006/08/add-namespace-node-to-element-in.html
> 
>  But maybe that's not what you really want to do.  Why would you
> like to copy this namespace binding?
> 
>  Regard,
> 
> -- 
> Florent Georges
> http://fgeorges.org/
> 
> 
> 
> 
> 
> _______________________________________________
> General mailing list
> General at developer.marklogic.com
> http://developer.marklogic.com/mailman/listinfo/general



More information about the General mailing list