<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>CDuce: XML Namespaces</title><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/><link href="cduce.css" type="text/css" rel="stylesheet"/></head><body style="margin: 0; padding : 0;"><table width="100%" border="0" cellspacing="10" cellpadding="0"><tr><td valign="top" style="width:20%;" align="left"><div class="leftbar" id="leftbar"><div class="smallbox"><ul><li><a href="#ov">Overview</a></li><li><a href="#reuse">Reusing namespace declarations</a></li><li><a href="#ns">XML Schema and namespaces</a></li><li><a href="#types">Types for atoms</a></li><li><a href="#print">Printing XML documents</a></li><li><a href="#pretty">Pretty-printing of XML values and types</a></li><li><a href="#acc">Accessing namespace bindings</a></li><li><a href="#misc">Miscellaneous</a></li></ul></div></div></td><td><h1>XML Namespaces</h1><div class="mainpanel"><div class="smallbox"><p><a href="index.html">CDuce: documentation</a>: <a href="manual.html">User's manual</a>: XML Namespaces</p><p><a href="manual_expressions.html"><img class="icon" width="16" alt="Previous page:" src="img/left.gif" height="16"/> Expressions</a> <a href="manual_schema.html"><img class="icon" width="16" alt="Next page:" src="img/right.gif" height="16"/> XML Schema</a></p></div><div><h2><a name="ov">Overview</a></h2><p> CDuce fully implements the W3C <a href="http://www.w3.org/TR/REC-xml-names/">XML Namespaces</a> Recommendation. Atom names (hence XML element tags) and record labels (hence XML attribute names) are logically composed of a namespace URI and a local part. Syntactically, they are written as <em>qualified names</em>, conforming to the QName production of the Recommendation: </p><div class="code"><pre> QName ::= (Prefix ':')? LocalPart Prefix ::= NCName LocalPart ::= NCName </pre></div><p> The prefix in a QName must be bound to a namespace URI. In XML, the bindings from prefixes to namespace URIs are introduction through special <b><tt>xmlns:prefix</tt></b> attributes. In CDuce, instead, there are explicit namespace binders. For instance, the following XML documents </p><div class="code"><pre> <p:a q:c="3" xmlns:p="http://a.com" xmlns:q="http://b.com"/> </pre></div><p> can be written in CDuce: </p><div class="code"><pre> namespace p = "http://a.com" in namespace q = "http://b.com" in <p:a q:c="3">[] </pre></div><p> This element can be bound to a variable <b><tt>x</tt></b> by a <b><tt>let</tt></b> binding as follows: </p><div class="code"><pre> let x = namespace p = "http://a.com" in namespace q = "http://b.com" in <p:a q:c="3">[] </pre></div><p> In which case the namespace declarations are local to the scope of the let. Alternatively, it is possible to use global prefix bindings: </p><div class="code"><pre> namespace p = "http://a.com" namespace q = "http://b.com" let x = <p:a q:c="3">[] </pre></div><p> Similarly, CDuce supports namespace <i>defaulting</i>. This is introduced by a local or global <b><tt>namespace "..."</tt></b> construction. As in the XML, default namespaces apply only to tags (atoms), not attributes (record labels). For instance, in the expression <b><tt>namespace "A" in <x y="3">[]</tt></b>, the namespace for the element tag is "A", and the attribute has no namespace. </p><p> The toplevel directive <b><tt>#env</tt></b> causes CDuce to print, amongst others, the current set of global bindings. </p></div><div><h2><a name="reuse">Reusing namespace declarations</a></h2><p> A global namespace declaration actually defines an identifier which is exported by the current compilation unit. It is possible to use this identifier in another unit to redefine another prefix with the same namespace URI. E.g., if the unit <b><tt>a</tt></b> contains: </p><div class="code"><pre> namespace ns = "http://a.com" </pre></div> then, in another unit, it is possible to declare: <div class="code"><pre> namespace ans = a.ns </pre></div><p> The <b><tt>open</tt></b> statement operates on namespace declarations; all the declarations from the open'ed unit are re-exported by the current unit. </p></div><div><h2><a name="ns">XML Schema and namespaces</a></h2><p> If an XML Schema has been bound to some identifier (in the current compilation unit or another one), it is possible to use this identifier in the right-hand side of a namespace declarations. The namespace URI is the targetNamespace of the XML Schema. E.g.: </p><div class="code"><pre> schema s = "..." namespace ns = s </pre></div></div><div><h2><a name="types">Types for atoms</a></h2><p> The type <b><tt>Atom</tt></b> represents all the atoms, in all the namespaces. An underscore in tag position (as in <b><tt><_>[]</tt></b>) stands for this type. </p><p> Each atom constitutes a subtype of <b><tt>Atom</tt></b>. In addition to these singelton types, there are the ``any in namespace'' subtypes, written: <b><tt>p:*</tt></b> where <b><tt>p</tt></b> is a namespace prefix; this type has all the atoms in the namespace denoted by <b><tt>p</tt></b>. The token <b><tt>.:*</tt></b> represents all the atoms in the current default namespace. </p><p> When used as atoms and not tags, the singleton types and ``any in namespace'' types must be prefixed by a backquote, as for atom values: <b><tt>`p:x, `p:*, `.:*</tt></b>. </p></div><div><h2><a name="print">Printing XML documents</a></h2><p> The <b><tt>print_xml</tt></b> and <b><tt>print_xml_utf8</tt></b> operators produce a string representation of an XML document. They have to assign prefixes to namespace. In the current implementation, CDuce produces XML documents with no default namespace and only toplevel prefix bindings (that is, <b><tt>xmlns:p="..."</tt></b> attributes are only produced for the root element). Prefix names are chosen using several heuristics. First, CDuce tries using the prefixes bound in the scope of the <b><tt>print_xml</tt></b> operator. When this is not possible, it uses global ``hints'': each time a prefix binding is encountered (in the CDuce program or in loaded XML documents), it creates a global hint for the namespace. Finally, it generates fresh prefixes of the form <b><tt>ns<i>n</i></tt></b> where <b><tt><i>n</i></tt></b> is an integer. For instance, consider the expression: </p><div class="code"><pre> print_xml (namespace "A" in <a>[]) </pre></div><p> As there is no available name the prefix URI "A", CDuce generates a fresh prefix and produces the following XML documents: </p><div class="code"><pre> <ns1:a xmlns:ns1="A"/> </pre></div><p> Now consider this expression: </p><div class="code"><pre> print_xml (namespace p = "A" in <p:a>[]) </pre></div><p>CDuce produces:</p><div class="code"><pre> <p:a xmlns:p="A"/> </pre></div><p> In this case, the prefix binding for the namespace "A" is not in the scope of <b><tt>print_xml</tt></b>, but the name <b><tt>p</tt></b> is available as a global hint. Finally, consider: </p><div class="code"><pre> namespace q = "A" in print_xml (namespace p = "A" in <p:a>[]) </pre></div><p> Here, the prefix <b><tt>q</tt></b> is available in the scope of the <b><tt>print_xml</tt></b>. So it is used in priority: </p><div class="code"><pre> <q:a xmlns:q="A"/> </pre></div><p> As a final example, consider the following expression: </p><div class="code"><pre> print_xml (namespace p ="A" in <p:a>[ (namespace p = "B" in <p:a>[]) ]) </pre></div><p> A single name <b><tt>p</tt></b> is available for both namespaces "A" and "B". CDuce choses to assign it to "A", and it generates a fresh name for "B", so as to produce: </p><div class="code"><pre> <p:a xmlns:ns1="B" xmlns:p="A"><ns1:a/></p:a> </pre></div><p> Note that the fresh names are ``local'' to an application of <b><tt>print_xml</tt></b>. Several application of <b><tt>print_xml</tt></b> will re-use the same names <b><tt>ns1, ns2</tt></b>, ... </p></div><div><h2><a name="pretty">Pretty-printing of XML values and types</a></h2><p> The CDuce interpreter and toplevel use an algorithm similar to the one mentioned in the previous section to pretty-print CDuce values and types that involve namespace. </p><p> The main difference is that it does <em>not</em> use by default the current set of prefix bindings. The rationale is that this set can change and this would make it difficult to understand the output of CDuce. So only global hints are used to produce prefixes. Once a prefix has been allocated, it is not re-used for another namespace. The toplevel directive <b><tt>#env</tt></b> causes CDuce to print, amongst other, the table of prefixes used for pretty-printing. It is possible to reinitialize this table with the directive <b><tt>#reinit_ns</tt></b>. This directive also sets the current set if prefix bindings as a primary source of hints for assigning prefixes for pretty-printing in the future. </p></div><div><h2><a name="acc">Accessing namespace bindings</a></h2><p> CDuce encourages a processing model where namespace prefixes are just considered as macros (for namespaces) which are resolved by the (CDuce or XML) parser. However, some XML specifications require the application to keep for each XML element the set of locally visible bindings from prefixes to namespaces. CDuce provides some support for that. </p><p> Even if this is not reflected in the type system, CDuce can optionally attach to any XML element a table of namespace bindings. The following built-in functions allow the programmer to explictly access this information: </p><div class="code"><pre> type Namespaces = [ (String,String)* ] namespaces: AnyXml -> Namespaces set_namespaces: Namespaces -> AnyXml -> AnyXml </pre></div><p> The <b><tt>namespaces</tt></b> function raises an exception when its argument has no namespace information attached. </p><p> When XML elements are generated, either as literals in the CDuce code or by <b><tt>load_xml</tt></b>, it is possible to tell CDuce to remember in-scope namespace bindings. This can be done with the following construction: </p><div class="code"><pre> namespace on in <i>e</i> </pre></div><p> The XML elements built within <b><tt><i>e</i></tt></b> (including by calling <b><tt>load_xml</tt></b>) will be annotated. There is a similar <b><tt>namespace off</tt></b> construction to turn off this mecanism in a sub-expression, and both constructions can be used at top-level. </p><div class="code"><pre> # namespace cduce = "CDUCE";; # namespaces <cduce:a>[];; Uncaught CDuce exception: [ `Invalid_argument 'namespaces' ] # namespace on;; # namespaces <cduce:a>[];; - : Namespaces = [ [ "xsd" 'http://www.w3.org/2001/XMLSchema' ] [ "xsi" 'http://www.w3.org/2001/XMLSchema-instance' ] [ "cduce" 'CDUCE' ] ] # namespaces (load_xml "string:<a xmlns='xxx'/>");; - : Namespaces = [ [ "" 'xxx' ] ] </pre></div><p> The default binding for the prefix <b><tt>xml</tt></b> never appear in the result of <b><tt>namespaces</tt></b>. </p><p> The <b><tt>xtransform</tt></b> iterator does not change the attached namespace information for XML elements which are just traversed. The generic comparison operator cannot distinguish two XML elements which only differ by the attached namespace information. </p></div><div><h2><a name="misc">Miscellaneous</a></h2><p> Contrary to the W3C <a href="http://www.w3.org/TR/xml-names11">Namespaces in XML 1.1</a> Candidate Recommendation, a CDuce declaration <b><tt>namespace p = ""</tt></b> does <em>not</em> undeclare the prefix <b><tt>p</tt></b>. Instead, it binds it to the null namespace (that is, a QName using this prefix is interpreted as having no namespace). </p></div><div class="meta"><p><a href="sitemap.html">Site map</a></p></div><div class="smallbox"><p><a href="index.html">CDuce: documentation</a>: <a href="manual.html">User's manual</a>: XML Namespaces</p><p><a href="manual_expressions.html"><img class="icon" width="16" alt="Previous page:" src="img/left.gif" height="16"/> Expressions</a> <a href="manual_schema.html"><img class="icon" width="16" alt="Next page:" src="img/right.gif" height="16"/> XML Schema</a></p></div></div></td></tr></table></body></html>