Example Schema: XML with namespaces

When timport is namespace-aware, it allows you to properly process XML namespaces without worrying about matching the exact prefix that the XML uses.

For example, the following XML document might come from a report generating program that lets you use your own custom fields. The builtin fields would appear in the software's namespace, while your custom fields would use your own:

<?xml version="1.0" encoding="utf-8"?>
<reports xmlns="http://www.coolreports.com"
    <name>First Report</name>
    <description>The first report generated</description>
    <mc:tpsComment>Great Report!</mc:tpsComment>
    <name>Budget Analysis</name>
    <description>Yearly review.</description>
    <mc:tpsComment>Need to reduce budget.</mc:tpsComment>

The XML document uses two namespaces: one defined as the default namespace, one as a prefix that is only used when that prefix is applied.

This is not the only way that the document could be defined. The following XML code defines the exact same document, only swapping which namespace is default:

<?xml version="1.0" encoding="utf-8"?>
<cool:reports xmlns:cool="http://www.coolreports.com"
    <cool:name>First Report</cool:name>
    <cool:description>The first report generated</cool:description>
    <tpsComment>Great Report!</tpsComment>
    <cool:name>Budget Analysis</cool:name>
    <cool:description>Yearly review.</cool:description>
    <tpsComment>Need to reduce budget.</tpsComment>

The actual data of the document is the same: elements like report and description still belong to the http://www.coolreports.com/ namespace, and elements like tpsid still belong to the http://www.mycompany.com namespace.

Either of these documents could be processed with the following schema:

table reports
xmlns    http://www.coolreports.com
xmlns:mc http://www.mycompany.com
field   name        varchar reports/report/name           
field   description varchar reports/report/description
field   tpsid       long    reports/report/mc:tpsid
field   tpsComment  varchar reports/report/mc:tpsComment

Like the first XML document, this schema uses coolreport's namespace as the default and uses a prefix for mycompany. The following schema would also work:

table reports
xmlns:rc     http://www.coolreports.com
xmlns:myComp http://www.mycompany.com
field name        varchar rc:reports/rc:report/rc:name
field description varchar rc:reports/rc:report/rc:description
field tpsid       long    rc:reports/rc:report/myComp:tpsid
field tpsComment  varchar rc:reports/rc:report/myComp:tpsComment

Here there's no default namespace; both namespaces use a prefix. Also note that the prefix we use for http://www.mycompany.com is different than any other example. This doesn't matter, as long as the URI that the prefix resolves to is the same.

For a final example, we can show that the default namespace doesn't need to be used for only the top-level element:

table reports
xmlns:cool http://www.coolreports.com
xmlns    http://www.mycompany.com
field name        varchar cool:reports/cool:report/cool:name
field description varchar cool:reports/cool:report/cool:description
field tpsid       long    cool:reports/cool:report/tpsid
field tpsComment  varchar cool:reports/cool:report/tpsComment

Here mycompany's namespace is the default (even though it's only used for two elements), and coolreport's namespace has a prefix. Once again, this schema will properly handle both of the previous XML documents, since it's all the same data being presented in different ways.

Copyright © Thunderstone Software     Last updated: May 19 2023
Copyright © 2023 Thunderstone Software LLC. All rights reserved.