[xquery-talk] Setting the document URI on constructed nodes.

G. Ken Holman gkholman at CraneSoftwrights.com
Sun May 13 09:26:34 PDT 2012

At 2012-05-13 10:19 -0400, I wrote:
>I cannot think of any way in the spec to specify the document URI of 
>a temporary in-memory tree, because I don't think that is meaningful 
>in the general case.
>However, when I've chained transformations in the past, I've 
>sometimes needed to preserve the input document URI along all of the 
>steps (because there have been relative URI addresses in the data) 
>and so I've very simply used xml:base= on the document element of 
>the temporary tree (unless the document element already has an 
>xml:base= attribute).
>Remember that from a syntax perspective, the root document file 
>entity of an XML document must contain the document element, so 
>base-uri(/*) is always the same as document-uri(root()) when there 
>is no xml:base= already there on the document element.
>So, when I create a temporary tree I use:
>   <root xml:base="...whatever..."/>
>... and when I ask for base-uri(/*) I get "...whatever...".
>In my work I would do something along the lines of:
>let $d := doc("input.xml") return
>element {$d/name(.)}
>  {
>   attribute xml:base { "...whatever..." },
>   $d/@xml:base, (:in case there already exists one:)
>   "rest of the processing"
>  }

Actually, two mistakes in throwing together that example.  I was 
asking for the name of the root node (not the document element) and I 
wouldn't hard-code the xml:base value.  Rather I'd use the 
document-uri() function to preserve the URI of the input file, or 
even shorten it further by using base-uri() on the document element 
so as to accommodate any existing xml:base= attribute:

let $d := doc("input.xml") return
element {$d/*/name(.)}
   attribute xml:base { base-uri($d/*) },
   "rest of the processing"

>I hope this helps.
>. . . . . . . . . . . Ken
>At 2012-05-13 13:58 +0000, David Lee wrote:
>>I noticed somewhat of a hole in XQuery ... hoping I am wrong.
>>I can find no way to set the base URI of a constructed document e.g
>>document {  <root/> }
>>such that fn:document-uri() would return it.
>>It seem this is one of those properties that must come from the 
>>outside environment.
>>I know if I were to store the document to a file then re-read it, 
>>likely the XML processor would pick it up ... I also know how to do 
>>this programmatically in API's like say Saxon.
>>But it seems missing from XQuery itself.   My guess is that since 
>>XQuery doesnt support storing documents it was thought unimportant 
>>to set the document URI.
>>Why I ask ?
>>I'm still looking at Streaming XDM formats, including pure 
>>in-memory streaming.
>>But it seems like I cannot directly encode and pass on a document 
>>(or base) URI through XQuery by itself to the output.   So any 
>>constructed documents would have none or undefined URI's to the 
>>receiver.   this leads me to think I need a meta encoding for XDM 
>>much like proposed at Balisage (XDML).   basically passing the URI 
>>information as seperate XDM values and a convention for 
>>interpreting them.  This seems like it could not be done in pure 
>>XQuery however because the target even if it got say  a sequence ( 
>>"uri" , document ) has no way to apply the uri to the document 
>>using XQuery so it must be done by external code.
>>Thanks for any commentary.

Public XSLT, XSL-FO, UBL and code list classes in Europe -- Oct 2012
Contact us for world-wide XML consulting and instructor-led training
Free 5-hour lecture: http://www.CraneSoftwrights.com/links/udemy.htm
Crane Softwrights Ltd.            http://www.CraneSoftwrights.com/q/
G. Ken Holman                   mailto:gkholman at CraneSoftwrights.com
Google+ profile: https://plus.google.com/116832879756988317389/about
Legal business disclaimers:    http://www.CraneSoftwrights.com/legal

More information about the talk mailing list