Ken - I hadn't seen that. Cool.

Here's another (easier to remember) MarkLogic-specific option for that:

xquery version "1.0-ml";

xdmp:document-insert("paths.xml",
<hello xmlns:abc="urn:X-abc">
   <abc:def>
     <ghi xmlns="urn:X-ghi"/>
   </abc:def>
</hello>);

for $node in doc('paths.xml')//* return xdmp:path($node)
-->
/hello
/hello/*:def
/hello/*:def/*:ghi

Or, another useful thing to know:

for $node in doc('paths.xml')//* return xdmp:describe($node)
-->

fn:doc("paths.xml")/hello
fn:doc("paths.xml")/hello/*:def
fn:doc("paths.xml")/hello/*:def/*:ghi

With the result of either xdmp:describe or xdmp:path, you can pass that string 
to xdmp:unpath to evaluate the expression:

for $node in doc('paths.xml')//* return xdmp:unpath(xdmp:path($node))
-->
<hello xmlns:abc="urn:X-abc">
  <abc:def>
    <ghi xmlns="urn:X-ghi"/>
  </abc:def>
</hello>
<abc:def xmlns:abc="urn:X-abc">
  <ghi xmlns="urn:X-ghi"/>
</abc:def>
<ghi xmlns:abc="urn:X-abc" xmlns="urn:X-ghi"/>

Finally, the function-mappy way, which I am trying to learn:

xdmp:unpath(xdmp:path(doc('paths.xml')//*))

Kelly

Message: 4
Date: Thu, 03 Jun 2010 23:40:45 -0400
From: "G. Ken Holman" <[email protected]>
Subject: Re: [MarkLogic Is there any code available that
        simply descends through a document node and discovers and prints out
        the namespace associated with each element?Dev General] 
To: General Mark Logic Developer Discussion
        <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"; format=flowed

At 2010-06-03 16:35 -0400, Tim Meagher wrote:
>Ultimately what I'm looking for is a way to automatically discover a 
>namespace in any given so that if I want to access it's contents I 
>don't have to know the namespace ahead of time,

Not sure what you are saying there.

>but it would be cool to have an app that can descend a document and 
>layout the xpath and namespace of each node.

I hope the example below helps.  It exposes the complete path of 
elements from the document element to each element in the instance.

. . . . . . . . . Ken

~/t $ cat paths.xml
<hello xmlns:abc="urn:X-abc">
   <abc:def>
     <ghi xmlns="urn:X-ghi"/>
   </abc:def>
</hello>
~/t $ cat paths.xq
'&#xa;',
for $each in doc( 'paths.xml' )//*
   return concat( '/',
                  string-join( $each/ancestor-or-self::*/concat('{',
                      namespace-uri-for-prefix(substring-before(name(.),':'),.),
                                           '}',local-name(.)),'/'),
                  '&#xa;' )

~/t $ xquery paths.xq
<?xml version="1.0" encoding="UTF-8"?>
  /{}hello
  /{}hello/{urn:X-abc}def
  /{}hello/{urn:X-abc}def/{urn:X-ghi}ghi
~/t $


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

Reply via email to