Hi Charles,
Thank you for the examples. The basic problem is that while most documents come as: <root xmlns="http://www.example.com/dtd" xmlns:xlink=http://www.w3.org/1999/xlink xmlns:a="http://www.example.com/dtd/alpha" xmlns:b="http://www.example.com/dtd/beta"> <a:parent> <b:child>content</b:child> </a:parent> <root> I'm occasionally receiving a document such as the following: <root> <a:parent> <b:child>content</b:child> </a:parent> <root> I am trying to avoid using any programming languages other than xquery and the built-in MarkLogic functionality to preprocess any XML content, but I am using C# and XCC.net to interface with a relational SQL Server database and to issue HTTP requests as part of a workflow. I'm not at liberty to discuss the source of the content. I found that using the content repair options associated with xdmp:document-load() adds in the missing namespace declarations. Best regards! Tim -----Original Message----- From: [email protected] [mailto:[email protected]] On Behalf Of Charles Foster Sent: Wednesday, December 30, 2009 7:35 AM To: General Mark Logic Developer Discussion Subject: Re: [MarkLogic Dev General] Can I add missing namespaces during a document insert? 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="",$namespace,""$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
_______________________________________________ General mailing list [email protected] http://xqzone.com/mailman/listinfo/general
