A Look at the xml:base attribute and the .NET Framework's XmlReader

The W3C xml:base recommendation describes the attribute xml:base when appearing on an XML element allows one to specify a base URI for the element and its children other than the base URI of the document or external entity. The base URI of a document or entity is the URI from which the document or entity was loaded. The following example taken from the W3C recommendation shows how xml:base processing works.

<?xml version="1.0"?>
<doc xml:base="https://example.org/today/"
     xmlns:xlink="https://www.w3.org/1999/xlink">
  <head>
    <title>Virtual Library</title>
  </head>
  <body>
   <paragraph>See
    <link xlink:type="simple"
          xlink:href="new.xml">what's new</link>!
   </paragraph>
   <paragraph>
     Check out the hot picks of the day!
   </paragraph>
   <olist xml:base="/hotpicks/">
     <item>
       <link xlink:type="simple"
      xlink:href="pick1.xml">Hot Pick #1</link>
     </item>
     <item>
       <link xlink:type="simple"
      xlink:href="pick2.xml">Hot Pick #2</link>
     </item>
     <item>
       <link xlink:type="simple"
      xlink:href="pick3.xml">Hot Pick #3</link>
     </item>
   </olist>
  </body>
</doc>

The URIs in the xlink:href attributes in this example resolve to full URIs as follows:

  • "what's new" resolves to the URI "https://example.org/today/new.xml"

  • "Hot Pick #1" resolves to the URI "https://example.org/hotpicks/pick1.xml"

  • "Hot Pick #2" resolves to the URI "https://example.org/hotpicks/pick2.xml"

  • "Hot Pick #3" resolves to the URI "https://example.org/hotpicks/pick3.xml"

xml:base exists as a mechanism to mimic HTML's BASE element and bring that functionality to the XML world. This was supposed to be a companion technology to XLink which was supposed to be a generic way to describe links in XML documents. Both XLink and xml:base were expected to be used in XHTML 2.0. However the XHTML working group rejected them and instead proposed HLink which was rejected by the W3C Technical Architecture Group. A lot of this is covered in the XML.com articles Introducing HLink and TAG Rejects HLink by Kendall Clark.

Even though xml:base has been rejected by the designers of the technologies it was primarily intended to be used with it has still made its way into the core of the XML family of technologies. Specifically, xml:base is used by the XML Infoset recommendation to define base URIs. This elevated xml:base and HTML-style base URI processing from being an application-specific construct to being a core part of XML that should be supported by XML parsers. For example, XQuery and XPath 2.0 will have the base-uri() function which returns the base URI of a node and takes into account the xml:base attribute.

The next question is whether the .NET Framework supports the xml:base recommendation. At first glance it looks this way since there is BaseURI property on both the XmlNode and XmlReader classes. However these properties report the BaseURI in the classic sense only (i.e. where the node was loaded from which is either the URI of the document or the URI of the entity it was expanded from). We were planning to add support for xml:base to the core XML parser as part of implementing XInclude but given that that it recently went from being a W3C candidate recommendation to going back to being a W3C working draft (partly due to a number of the architectural issues raised by Murata Makoto) the future of the spec is currently uncertain so we've backed off on our implementation. In the meantime, developers can use XInclude.NET if they need XML Inclusions and its associated support for the xml:base attribute in the .NET Framework.

Comments