Rules for replacing namespaces in elements

This Version: August 6, 2001

Latest Version: http://simonstl.com/ns/namespaces/elements/replace/

Previous Version: None

Editors:

Table of contents

  1. Introduction
  2. Rules
  3. Resources
  4. Normative References
  5. Informative References

1. Introduction

This vocabulary is used by a filter, com.simonstl.namespace.elements.ElementReplNamespaceFilter, which simply replaces namespaces used by elements in the document. The vocabulary provides a list of namespaces which should be replaced by other specified namespaces, optionally set with a particular prefix.

Warning: use of this technique may change the meaning of your documents substantially. Use it with care after studying options and impact.

2. Rules

The rules for replacing namespaces on elements work quite simply. Namespaces in the original document (identified by matchURI) are mapped to namespaces in the result (resultURI). If desired, a prefix (resultPrefix) may be specified for the result namespace.

The example rule sets will be applied to this document:

<p:person xmlns:p="http://simonstl.com/person">
<givenName>Chip</givenName>
<familyName>Skillet</familyName>
<address>
<z:street xmlns:z="http://simonstl.com/address">1259 Zingzang Road</z:street>
<city>Ithaca</city>
</address>
</p:person>

The first rule we'll examine changes the http://simonstl.com/person namespace to http://example.com/, but makes no other changes:

<namespaces xmlns="http://simonstl.com/ns/namespaces/elements/replace/">
<replace matchURI="http://simonstl.com/person" resultURI="http://example.com/" />
</namespaces>

When applied to the document, we get:

C:\opensrc\namespace>java com.simonstl.namespace.elements.ElementReplFilterTester replaceEl1.xml test2.xml
<?xml version="1.0" standalone="yes"?>
<p:person xmlns:p="http://example.com/">
<givenName>Chip</givenName>
<familyName>Skillet</familyName>
<address>
<z:street xmlns:z="http://simonstl.com/address">1259 Zingzang Road</z:street>
<city>Ithaca</city>
</address>
</p:person>

The http://simonstl.com/address namespace remains unchanged, while http://simonstl.com/person becomes http://example.com/.

The next set of rules also changes the prefix:

<namespaces xmlns="http://simonstl.com/ns/namespaces/elements/replace/">
<replace matchURI="http://simonstl.com/person" 
  resultURI="http://example.com/" 
  resultPrefix="example"/>
</namespaces>

When applied to the document, the namespace URI and the prefix both change:

C:\opensrc\namespace>java com.simonstl.namespace.elements.ElementReplFilterTester replaceEl2.xml test2.xml
<?xml version="1.0" standalone="yes"?>
<example:person xmlns:example="http://example.com/">
<givenName>Chip</givenName>
<familyName>Skillet</familyName>
<address>
<z:street xmlns:z="http://simonstl.com/address">1259 Zingzang Road</z:street>
<city>Ithaca</city>
</address>
</example:person>

Once again, the http://simonstl.com/address namespace remains unchanged, while http://simonstl.com/person becomes http://example.com/. This time, however, the prefix p became example.

The last example demonstrates how to replace multiple namespaces simultaneously, and remove a prefix:

<namespaces xmlns="http://simonstl.com/ns/namespaces/elements/replace/">
<replace matchURI="http://simonstl.com/person" 
  resultURI="http://example.com/1" 
  resultPrefix="example"/>
<replace matchURI="http://simonstl.com/address" 
  resultURI="http://example.com/2" 
  resultPrefix=""/>
</namespaces>

This rule has a more dramatic effect on the contents of the document:

C:\opensrc\namespace>java com.simonstl.namespace.elements.ElementReplFilterTester replaceEl3.xml test2.xml
<?xml version="1.0" standalone="yes"?>
<example:person xmlns:example="http://example.com/1">
<givenName>Chip</givenName>
<familyName>Skillet</familyName>
<address>
<street xmlns="http://example.com/2">1259 Zingzang Road</street>
<city>Ithaca</city>
</address>
</example:person>

The http://simonstl.com/address namespace becomes http://example.com/2, and the z prefix is removed entirely. The http://simonstl.com/person becomes http://example.com/1. Again, the prefix p became example.

3. Related Resources

3.1 Document Type Definition

A DTD elreplrules.dtd.

3.2 SAX Filter

A SAX Filter implementing these rules. JavaDoc describing its class structure and usage is available here.

This code is as an example and is not normative.

3.3 JAR

The above code packaged as a java archive.

4. Normative References

  1. Extensible Markup Language (XML) 1.0
  2. W3C XML Names
  3. W3C XML Schema Part 1 - Structures

5. Informative References