Hi Tim,

Failing Load-Time Default Namespace Assignment with xdmp:document-load as
Mike has pointed out, if you wish to use xdmp:document-insert, you have at
least two further options:

1. Recursively add a namespace to each element of your dynamically
constructed XML document, for instance:

------------------------------------------------------------------

declare namespace ns = "uri:new-namespace";

(:~
 : Recursively adds a namespace to all elements in an XML document.
 :
 : Takes an element() or a document-node() as input,
 : Returns the equivalent XML structure, with namespaces attached.
 :     NOTE:
 : RELIES on the namespace prefix "ns" being declared in the prolog.
 :
 : @param $node an element() or document-node() to attach a namespace to
 : @return an element() node, with namespaces attached to every descendant
 : @author Charles Foster
 :)
declare function local:namespace-insert($node as node()) as node()?
{
  typeswitch($node)
    case $a as element() return
      element { fn:concat("ns:",fn:local-name($a)) } {
        $a/@*,
        for $x in $a/node()
        return local:namespace-insert($x)
      }
    case $a as document-node() return local:namespace-insert($a/element())
    default return $node
};

------------------------------------------------------------------

2. Add a default namespace to the root level element, via a Regular
Expression replace:

------------------------------------------------------------------

(:~
 : Fast function to attach a namespace to a namespace-less XML document.
 :
 : Takes a valid XML Document serialized as an xs:string for input,
 : returns an XML Document also serialized as an xs:string with a
 : namespace attached.
 :
 : @param $doc a valid XML Document serialized as an xs:string.
 : @param $namespace the namespace URI which to attach to the document.
 : @return xs:string representation of an XML document.
 : @author Charles Foster
 :)
declare function local:attach-namespace(
  $doc as xs:string,
  $namespace as xs:string) as xs:string
{
 fn:replace($doc,
 fn:concat("^(<[a-zA-Z0-9_-]*)([^>]*)(.*)"),
 fn:concat("$1$2 xmlns=&quot;",$namespace,"&quot;$3"))
};

------------------------------------------------------------------

If you are inserting large XML documents, or are inserting many XML
documents and performance is important to you, I would suggest using the
Regular Expression replace option.

Ideally, it would be best to attach a default namespace to the XML
structure before adding it to MarkLogic in the first place.

Would you mind if I asked where you're getting the XML documents from and
what programming environment you are using e.g. Java / C# / C++, if at
all?

Regards,

Charles

> There is a <default_namespace> option you can use with
> xdmp:document-load that could help.
>
> -Mike
>
> Tim Meagher wrote:
>>
>> Hi Folks,
>>
>>
>>
>> I have some XML content that is missing namespace declarations that
>> I'm trying to insert into MarkLogic.  I don't know if this is a common
>> problem, especially when dealing with documents that reference DTDs by
>> location in a file system and which might expect the namespace
>> declarations to be included as entities in the DTD.
>>
>>
>>
>> Is there a simple way to have namespaces added to a document during
>> the insert?  I'm wondering if I have to create a precommit trigger
>> that adds the namespace to the document prior to inserting it, but it
>> would be nice if there was just a simple way of adding the namespaces
>> during the insert.
>>
>>
>>
>> Thank you!
>>
>>
>>
>> Tim Meagher
>>
>>
>>
>> ------------------------------------------------------------------------
>>
>> _______________________________________________
>> General mailing list
>> [email protected]
>> http://xqzone.com/mailman/listinfo/general
>>
> _______________________________________________
> General mailing list
> [email protected]
> http://xqzone.com/mailman/listinfo/general
>

_______________________________________________
General mailing list
[email protected]
http://xqzone.com/mailman/listinfo/general

Reply via email to