Using URIs in COLLADA

From COLLADA
Jump to: navigation, search
Tutorials
This article is one several tutorials, guides, and annotated examples available in this wiki.
Multipage tutorials:  • COLLADA DOM user guide
Shorter how-tos:  • Using accessors  • Schema validation • Using URIs
 • Various annotated examples

Instructions for adding a tutorial

[[Category: ]]


URIs are used extensively in COLLADA documents to reference other COLLADA elements or external resources such as texture files, shader source code, and so on. A URI must be formatted properly for the application to locate and retrieve the resource that the URI identifies. This article describes how to use URIs in COLLADA.

URIs in COLLADA

The COLLADA schema uses the XML Schema xs:anyURI type to represent URIs. This XML Schema type describes absolute and relative URI, including URI references, so you can use any of these forms in a COLLADA document.

Some examples of URI usage in COLLADA include

  • The target attribute of <instance_material> elements
  • The url attribute on <instance_geometry>, <instance_node>, and <instance_effect> elements
  • The <image>/<init_from> element

For URIs that reference COLLADA elements (such as the url attribute on the <instance_geometry> element), the URI fragment portion identifies the element by its id attribute. Because COLLADA documents are XML documents, the syntax for the URI fragment is defined by the XML Pointer Language (XPointer) and the shorthand pointer is the most common usage. An example of this:

<instance_geometry url="file:///models/car.dae#carGeometry" />

This <instance_geometry> references the <geometry> element whose id is carGeometry in the document file:///models/car.dae.

The following example uses a relative URI reference, which must be resolved to an absolute URI before the application can obtain the referenced <geometry> element:

<instance_geometry url="../car.dae#carGeometry" />

Base URIs in a COLLADA document

To resolve relative references, a base URI is needed. In COLLADA, the base URI can be specified in the root <COLLADA> element:

<COLLADA xmlns="http://www.collada.org/2005/11/COLLADASchema" version="1.4.1"
         xml:base="file:///home/sthomas/models/duck.dae">

If the xml:base attribute isn't specified, then the base URI is the document's URI according to the rules defined in RFC 3986 section 5.1. For example, if the document's URI is file:///car.dae, then the URI parsing logic in your software uses that as the base URI to resolve any relative references in the document.

It's important to note that a relative reference cannot indicate a change to the scheme part of the base URI.

Native Paths versus URIs

An ordinary Windows or Linux native filesystem path is not a valid URI!

A file-scheme URI (see RFC 1738 section 5) represents a file on the local machine. File paths need to be converted to file-scheme URIs before they can be used properly by URI aware (i.e. COLLADA) applications. If the base URI's scheme is file (which is often the case), then you can use a relative reference to the file instead of specifying a full file-scheme URI. When the reference is resolved by the application, it becomes a valid file-scheme URI.

The following examples specify a file via a relative URI reference in COLLADA. These examples assume that the base URI's scheme is file; otherwise, the reference won't resolve correctly. You might use these examples in the <image>/<init_from> element to refer to an image file on disk.

Example Description File Path URI Reference
Windows absolute path
C:\folder\image.tga
/C:/folder/image.tga
Windows relative path
..\folder\image.tga  
../folder/image.tga
Linux absolute path
/folder/image.tga 
/folder/image.tga
Linux relative path
../folder/image.tga
../folder/image.tga
UNC path
\\remoteMachine\folder\image.tga 
file://///remoteMachine/folder/image.tga

In particular, note that "\" characters convert to "/", and that Windows volume specifiers (e.g. "C:") must be prepended with "/" to become proper URI absolute-path references.

So, in a COLLADA document you could write

<image>
  <init_from>../folder/image.tga</init_from>
</image>

Suppose the base URI is file:///C:/models/maya/car.dae. An application will resolve the relative reference against the base URI to get file:///C:/models/folder/image.tga, which is a valid file-scheme URI. More examples of how relative references are resolved can be found in RFC 3986 section 5.4.

As the last example in the table shows, an exception is made for Windows UNC paths. In theory, you could convert the UNC file path \\remoteMachine\folder\image.tga to the relative reference //remoteMachine/folder/image.tga. The spec calls this a network-path reference (as described here). The authority is remoteMachine and the path is /folder/image.tga. Assuming that the base URI is file:///C:/models/maya/car.dae, this resolves to the URI file://remoteMachine/folder/image.tga.

However, many XML parsers can't work correctly with such URIs. One example is libxml, which the COLLADA DOM uses to load COLLADA documents. Instead of specifying the UNC path as a URI reference, it's better to use a file-scheme URI as shown in the table, with which most XML libraries can work correctly.

See also