<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xml:base="http://www.jenitennison.com/blog" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
 <title>xlinq</title>
 <link>http://www.jenitennison.com/blog/taxonomy/term/15</link>
 <description>The taxonomy view with a depth of 0.</description>
 <language>en</language>
<item>
 <title>The perils of default namespaces</title>
 <link>http://www.jenitennison.com/blog/node/36</link>
 <description>&lt;p&gt;A lot of people run into problems with namespaces, and most of those arise from using default namespaces (ie not giving namespaces prefixes). The transformation technology you use can have a big effect on how confusing and irritating it gets.&lt;/p&gt;

&lt;p&gt;Default namespaces make XML documents easier to read because they allow you to just give the local name of an element rather than using prefixes all over the place. For example, using:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;house status=&quot;For Sale&quot; xmlns=&quot;http://www.example.com/ns/house&quot;&amp;gt;
  &amp;lt;askingPrice&amp;gt;...&amp;lt;/askingPrice&amp;gt;
  &amp;lt;address&amp;gt;...&amp;lt;/address&amp;gt;
  &amp;lt;layout&amp;gt;...&amp;lt;/layout&amp;gt;
&amp;lt;/house&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;!--break--&gt;

&lt;p&gt;rather than:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;h:house status=&quot;For Sale&quot; xmlns:h=&quot;http://www.example.com/ns/house&quot;&amp;gt;
  &amp;lt;h:askingPrice&amp;gt;...&amp;lt;/h:askingPrice&amp;gt;
  &amp;lt;h:address&amp;gt;...&amp;lt;/h:address&amp;gt;
  &amp;lt;h:layout&amp;gt;...&amp;lt;/h:layout&amp;gt;
&amp;lt;/h:house&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In some cases, specifically documents that are validated against a DTD or interpreted by non-namespace-aware applications, you might be forced to use the default namespace. The biggest example of this is (X)HTML.&lt;/p&gt;

&lt;p&gt;In transformation technologies, such as &lt;a href=&quot;http://www.w3.org/Style/XSL/&quot;&gt;XSLT&lt;/a&gt;, &lt;a href=&quot;http://www.w3.org/XML/Query/&quot;&gt;XQuery&lt;/a&gt; and &lt;a href=&quot;http://www.xlinq.net/&quot;&gt;XLinq in VB.NET&lt;/a&gt;, you have to deal with at least two documents: the source documents that you are processing and the result documents that you are creating. Often, the source and result documents will use default namespaces, or at any rate you&amp;#8217;ll want to query and create the documents without using prefixes. Sometimes, the source and result documents all use the same namespace, but it&amp;#8217;s far more common that they don&amp;#8217;t.&lt;/p&gt;

&lt;p&gt;So transformation technologies have to support at least &lt;em&gt;two&lt;/em&gt; default namespaces: one for querying and one for construction.&lt;/p&gt;

&lt;p&gt;In XPath 1.0, you must specify a prefix for each namespace you want to use. A path like &lt;code&gt;/house/layout&lt;/code&gt; will only select &lt;code&gt;&amp;lt;layout&amp;gt;&lt;/code&gt; elements in no namespace. In XSLT 1.0, the default namespace in the stylesheet (as declared by the &lt;code&gt;xmlns&lt;/code&gt; attribute on &lt;code&gt;&amp;lt;xsl:stylesheet&amp;gt;&lt;/code&gt;) is then free to be used for the result documents. For example, I might do&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;xsl:stylesheet version=&quot;1.0&quot;
  xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;
  xmlns:h=&quot;http://www.example.com/ns/house&quot;
  exclude-result-prefixes=&quot;h&quot;
  xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&amp;gt;

&amp;lt;xsl:template match=&quot;h:house&quot;&amp;gt;
  &amp;lt;div class=&quot;house&quot;&amp;gt;
    &amp;lt;h1&amp;gt;&amp;lt;xsl:apply-templates select=&quot;h:askingPrice&quot; /&amp;gt;&amp;lt;/h1&amp;gt;
    ...
  &amp;lt;/div&amp;gt;
&amp;lt;/xsl:template&amp;gt;

&amp;lt;/xsl:stylesheet&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;[The best way to deal with multiple result documents in different default namespaces is to simply have different stylesheet documents to handle their generation, all included or imported into your main stylesheet application.]&lt;/p&gt;

&lt;p&gt;Users of XSLT 1.0 found it confusing that they couldn&amp;#8217;t just copy the namespace declarations (including a default namespace declaration) from a sample source document and have the paths just work. So in XPath 2.0, rather than no prefix meaning no namespace, the &lt;strong&gt;default element/type namespace&lt;/strong&gt; in the context is used for element names with no prefix. If the default element/type namespace is set to &lt;code&gt;http://www.example.com/ns/house&lt;/code&gt; then the path &lt;code&gt;/house/layout&lt;/code&gt; will select all &lt;code&gt;&amp;lt;layout&amp;gt;&lt;/code&gt; elements in the &lt;code&gt;http://www.example.com/ns/house&lt;/code&gt; namespace. You can set this default element/type namespace in XSLT 2.0 using the &lt;code&gt;[xsl:]xpath-default-namespace&lt;/code&gt; attribute, which can go anywhere but will usually be situated on the &lt;code&gt;&amp;lt;xsl:stylesheet&amp;gt;&lt;/code&gt; element (in which case it appears without the &lt;code&gt;xsl:&lt;/code&gt; prefix). The default element/type namespace can be scoped to a particular area of your stylesheet in the same way as namespace declarations.&lt;/p&gt;

&lt;p&gt;Otherwise, XSLT 2.0 works like XSLT 1.0 in that the default namespace in the stylesheet supplies the default namespace for created elements, so you can do:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;xsl:stylesheet version=&quot;2.0&quot;
  xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;
  xmlns=&quot;http://www.w3.org/1999/xhtml&quot;
  xpath-default-namespace=&quot;http://www.example.com/ns/house&quot;&amp;gt;

&amp;lt;xsl:template match=&quot;house&quot;&amp;gt;
  &amp;lt;div class=&quot;house&quot;&amp;gt;
    &amp;lt;h1&amp;gt;&amp;lt;xsl:apply-templates select=&quot;askingPrice&quot; /&amp;gt;&amp;lt;/h1&amp;gt;
    ...
  &amp;lt;/div&amp;gt;
&amp;lt;/xsl:template&amp;gt;

&amp;lt;/xsl:stylesheet&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;By keeping the default query namespace and the default construction namespace separate, you&amp;#8217;re able to use unprefixed names in both paths and element constructors, even if the default namespaces in the two cases are different.&lt;/p&gt;

&lt;p&gt;XQuery and VB.NET, on the other hand, provide a single default namespace that is used for both queries and construction, and they work in slightly different ways.&lt;/p&gt;

&lt;p&gt;In XQuery you can declare the default namespace for the query, with&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;declare default element namespace &quot;http://www.example.com/ns/house&quot;;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;which means that you can query the source document with paths like &lt;code&gt;/house/askingPrice&lt;/code&gt; and create elements in the &lt;code&gt;http://www.example.com/ns/house&lt;/code&gt; namespace with direct element constructors without prefixes, like&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;house status=&quot;For Sale&quot;&amp;gt;
  &amp;lt;askingPrice&amp;gt;...&amp;lt;/askingPrice&amp;gt;
  &amp;lt;address&amp;gt;...&amp;lt;/address&amp;gt;
  &amp;lt;layout&amp;gt;...&amp;lt;/layout&amp;gt;
&amp;lt;/house&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If you then want to generate XHTML (or some other result for which you&amp;#8217;d prefer to use the default namespace), you can use a default namespace declaration on the XHTML you generate:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;div class=&quot;house&quot; xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&amp;gt;
  &amp;lt;h1&amp;gt;...&amp;lt;/h1&amp;gt;
  ...
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;But the default namespace declaration in the element constructor carries through into the embedded expressions, so&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;  &amp;lt;div class=&quot;house&quot; xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&amp;gt;
    &amp;lt;h1&amp;gt;{ /house/askingPrice }&amp;lt;/h1&amp;gt;
    ...
  &amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;won&amp;#8217;t work. As a result, you end up having to use the query&amp;#8217;s default namespace declaration to set the default namespace to XHTML (or whatever the default namespace is in the result), and use prefixes in your queries (essentially the same situation as in XSLT 1.0).&lt;/p&gt;

&lt;p&gt;In XLinq in VB.NET, there&amp;#8217;s the same kind of pattern. The &lt;code&gt;Imports&lt;/code&gt; statement allows you to declare a default namespace that&amp;#8217;s used in both queries and construction, as in:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Imports &amp;lt;xmlns=&quot;http://www.example.com/ns/house&quot;&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;and you can use a default namespace declaration on the XHTML you generate to provide the default namespace for the elements in the XML literal:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;houseDiv =
  &amp;lt;div class=&quot;house&quot; xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&amp;gt;
    &amp;lt;h1&amp;gt;...&amp;lt;/h1&amp;gt;
    ...
  &amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;But unlike in XQuery, the default XHTML namespace declaration in the &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; &lt;em&gt;doesn&amp;#8217;t&lt;/em&gt; have an effect on the default namespace used in embedded expressions, which means you can still use unprefixed element names in any paths used within the XML literal, like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;houseDiv =
  &amp;lt;div class=&quot;house&quot; xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&amp;gt;
    &amp;lt;h1&amp;gt;&amp;lt;%= doc.&amp;lt;house&amp;gt;.&amp;lt;askingPrice&amp;gt; %&amp;gt;&amp;lt;/h1&amp;gt;
    ...
  &amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;However, if you build up your XHTML gradually, perhaps by using separate variables or methods, then every time you create a snippet of XHTML you have to specify this default namespace. Again, people will end up using the &lt;code&gt;Imports&lt;/code&gt; statement to set the default namespace to the default result namespace and using prefixes in their paths.&lt;/p&gt;

&lt;p&gt;The other factor to consider is that sometimes no prefix really does mean no namespace. If you&amp;#8217;re querying an XML document that contains elements in no namespace, you have to set the default query namespace to no namespace. In XSLT 1.0, that&amp;#8217;s always the case anyway; in XSLT 2.0, the &lt;code&gt;xpath-default-namespace&lt;/code&gt; shouldn&amp;#8217;t be set (or should be unset for those places that need to query no-namespace elements). In XQuery you can&amp;#8217;t use the query default namespace declaration and in XLinq in VB.NET you can&amp;#8217;t use the &lt;code&gt;Imports&lt;/code&gt; statement. In both these cases, you better hope your result is in no namespace too. If not, the best route (to make it work at all in XQuery, and to avoid repetitive &lt;code&gt;xmlns&lt;/code&gt; attributes in VB.NET) is to create a no-namespace version of your result first, and have a standard function or method that will add the right default namespace to that result.&lt;/p&gt;
</description>
 <comments>http://www.jenitennison.com/blog/node/36#comments</comments>
 <category domain="http://www.jenitennison.com/blog/taxonomy/term/14">xml</category>
 <category domain="http://www.jenitennison.com/blog/taxonomy/term/5">xslt</category>
 <category domain="http://www.jenitennison.com/blog/taxonomy/term/15">xlinq</category>
 <category domain="http://www.jenitennison.com/blog/taxonomy/term/29">xquery</category>
 <pubDate>Sun, 01 Jul 2007 20:32:44 +0100</pubDate>
 <dc:creator>Jeni</dc:creator>
 <guid isPermaLink="false">36 at http://www.jenitennison.com/blog</guid>
</item>
<item>
 <title>XTech 2007: Wednesday 16th May Morning</title>
 <link>http://www.jenitennison.com/blog/node/18</link>
 <description>&lt;p&gt;Since there&amp;#8217;s next to no &amp;#8216;net connection at XTech 2007 (obviously the Web is not so ubiquitous as all that), I have nothing to do in the sessions but listen! Here are some thoughts about the sessions that I attended on the morning of Wednesday 16th. I haven&amp;#8217;t included the keynotes not because they weren&amp;#8217;t interesting but because I can&amp;#8217;t think of anything to say about them at the moment.&lt;/p&gt;

&lt;!--break--&gt;

&lt;h2&gt;&lt;a href=&quot;http://2007.xtech.org/public/schedule/paper/60&quot; title=&quot;XML and LINQ: What&#039;s New in Orcase and Beyond&quot;&gt;XML and LINQ: What&amp;#8217;s New in Orcas and Beyond&lt;/a&gt;&lt;/h2&gt;

&lt;h3&gt;&lt;a href=&quot;http://research.microsoft.com/~emeijer/&quot; title=&quot;Erik Meijer&#039;s Website&quot;&gt;Erik Meijer&lt;/a&gt; (Microsoft)&lt;/h3&gt;

&lt;p&gt;I thought I&amp;#8217;d better go to this one because I&amp;#8217;m supposed to be talking about XML APIs at this year&amp;#8217;s &lt;a href=&quot;http://www.xmlsummerschool.com/&quot; title=&quot;XML Summer School, Oxford&quot;&gt;XML Summer School&lt;/a&gt; and LINQ, or XLINQ, is one of the hot topics. I&amp;#8217;m not a .NET developer, so it&amp;#8217;s all kinda passed me by thus far, and I&amp;#8217;m not sure I really understand it now. (I&amp;#8217;d welcome corrections and clarifications.) The three things that seemed to be important are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;You can get at information held in objects, databases or XML using the same syntax. (Erik showed accessing XML with faulty XQuery syntax, which made me and &lt;a href=&quot;http://www.datypic.com/&quot; title=&quot;Priscilla Walmsley&#039;s Website&quot;&gt;Priscilla Walmsley&lt;/a&gt; grimace at each other.) This means you can decide how you want to actually hold your data further down the line. A big distinction between previous attempts to work across paradigms is that the &lt;em&gt;data&lt;/em&gt; doesn&amp;#8217;t get converted, but the &lt;em&gt;queries&lt;/em&gt; do. So you write your LINQ query in LINQ syntax and it gets mapped on to SQL to query your SQL database, or on to XQuery (I guess) to query your XML document. This all seemed to assume data-oriented information: I have no idea, yet, how or whether mixed content gets handled.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;XML is a &amp;#8220;first class datatype&amp;#8221; in LINQ, so to create static XML you just write XML in your program (a bit like in XQuery). The example Erik showed included an XML declaration, which is just plain weird: dunno if that was an error or it&amp;#8217;s a way of indicating what version of XML you&amp;#8217;re using, or what. To create dynamic portions of the XML, you use &lt;code&gt;&amp;lt;%=...%&amp;gt;&lt;/code&gt; &amp;#8220;expression holes&amp;#8221; which can contain .NET code, including calls to a new API for creating XML elements and attributes (a DOM replacement).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Erik talked about writing applications in .NET and then automatically refactoring them (with a click in a context menu) to work in client/server architectures, and refactoring again to work across several clients. Presumably this creates all the code necessary to make the application work with WS* messaging, so you don&amp;#8217;t have to program it. This all sounded really dodgy to me: I don&amp;#8217;t want to rely on a tool to make a language/approach/architecture usable.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;There was an amusing digression into the art of rendering triangles, and thus three-dimensional models, with zero-width, zero-height, bordered &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt;s in XHTML. And a mention of the &amp;#8220;backbutton&amp;#8221; problem that you get when you spawn tabs/windows in your web browser and then go back to your original tab/window and hit submit, which made me think that perhaps a RESTful architecture would make a whole lot of complexity go away.&lt;/p&gt;

&lt;h2&gt;&lt;a href=&quot;http://2007.xtech.org/public/schedule/detail/159&quot; title=&quot;Data Model Perspectives for XML Schema&quot;&gt;Data Model Perspectives for XML Schema&lt;/a&gt;&lt;/h2&gt;

&lt;h3&gt;&lt;a href=&quot;http://www.ee.ethz.ch/&quot; title=&quot;Felix Michel&#039;s Website&quot;&gt;Felix Michel&lt;/a&gt; (ETH Zurich), &lt;a href=&quot;http://dret.net/netdret/&quot; title=&quot;Erik Wilde&#039;s Website&quot;&gt;Erik Wilde&lt;/a&gt; (UC Berkeley)&lt;/h3&gt;

&lt;p&gt;Felix mentioned that I might be interested in his talk in a &lt;a href=&quot;http://www.jenitennison.com/blog/node/2#comment-24&quot; title=&quot;Comment: Re: XTech Preparation&quot;&gt;comment here&lt;/a&gt;, and sure enough I found it fascinating. He&amp;#8217;s created a single-file representation of XML Schemas (consolidating schemas that, by virtue of using different namespaces, must be in different physical documents), and a set of XSLT 2.0 user-defined functions that provide access to and queries on the XML Schema information.&lt;/p&gt;

&lt;p&gt;For example, you can go from an instance element in your document to its type, find out if it&amp;#8217;s an extension or restriction, go to its base type, look at the annotations on it, and so on and so on. And all this in Basic XSLT 2.0 (the functions that work on instance elements traverse the instance document and schema in parallel to locate the element declaration that applies). You could use these functions to do everything you can do in Schema-Aware XSLT 2.0, with more flexibility, at the expense of performance.&lt;/p&gt;

&lt;p&gt;He also mapped content models onto &lt;code&gt;&amp;lt;occurrence&amp;gt;&lt;/code&gt; elements that encode the &amp;#8220;follow set&amp;#8221; for a particular occurrence, so you can easily answer the question &amp;#8220;what elements could come next?&amp;#8221;. I can&amp;#8217;t immediately think of a way of using that information in a stylesheet, but perhaps he can describe one.&lt;/p&gt;

&lt;p&gt;Anyway, I think Felix&amp;#8217;s point was not to provide XSLT programmers with a set of useful functions, but to demonstrate the kind of standard, fairly light-weight, API that we might use to access XML Schema information. There was some discussion, in the development of XPath 2.0, of providing this kind of API, but getting agreement on XDM was hard enough!&lt;/p&gt;

&lt;p&gt;However, my thoughts were veering off in different directions. To my mind, validation and annotation are separable processes, and the data types, element groups and linking behaviour that you might find useful on a data set are processing-specific. For example, it might make sense for one process to annotate the element &lt;code&gt;&amp;lt;foo&amp;gt;2007-05-17&amp;lt;/foo&amp;gt;&lt;/code&gt; as having the type date, while for another process (such as a transformation that deletes all &lt;code&gt;&amp;lt;foo&amp;gt;&lt;/code&gt; elements) it&amp;#8217;s unnecessary. I really don&amp;#8217;t want to have to define an XSD schema for my entire schema just to indicate that the &lt;code&gt;&amp;lt;foo&amp;gt;&lt;/code&gt; element is of type &lt;code&gt;xs:date&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Just as it&amp;#8217;s better to define the links between elements using keys, rather than relying on ID annotations made by a DTD, I think type annotations and node groups (why limit it to elements?) could be defined in the stylesheet. To give an idea:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;!-- all date attributes have the type named &#039;xs:date&#039; --&amp;gt;
&amp;lt;ann:type name=&quot;xs:date&quot; match=&quot;@date&quot; /&amp;gt;
&amp;lt;!-- h1, h2, h3, h4, h5, h6 elements are heading elements --&amp;gt;
&amp;lt;ann:group name=&quot;xhtml:heading&quot; 
  match=&quot;xhtml:h1 | xhtml:h2 | xhtml:h3 | xhtml:h4 | xhtml:h5 | xhtml:h6&quot; /&amp;gt;
&amp;lt;!-- oh, and so&#039;s the h element --&amp;gt;
&amp;lt;ann:group name=&quot;xhtml:heading&quot; match=&quot;xhtml:h&quot; /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It&amp;#8217;d be reasonably easy to give rudimentary support for &lt;code&gt;ann:type($node)&lt;/code&gt; and &lt;code&gt;ann:group($node)&lt;/code&gt; user-defined functions based on these, but they&amp;#8217;d really have to be built into the XSLT processor to get full pattern support and to work with modularised stylesheets. This all requires more detail than I have time to write right now, but is it even worth pursuing?&lt;/p&gt;
</description>
 <comments>http://www.jenitennison.com/blog/node/18#comments</comments>
 <category domain="http://www.jenitennison.com/blog/taxonomy/term/5">xslt</category>
 <category domain="http://www.jenitennison.com/blog/taxonomy/term/8">schema</category>
 <category domain="http://www.jenitennison.com/blog/taxonomy/term/15">xlinq</category>
 <category domain="http://www.jenitennison.com/blog/taxonomy/term/4">xtech</category>
 <pubDate>Thu, 17 May 2007 21:50:59 +0100</pubDate>
 <dc:creator>Jeni</dc:creator>
 <guid isPermaLink="false">18 at http://www.jenitennison.com/blog</guid>
</item>
</channel>
</rss>
