Rules for replacing namespaces in attributes

This Version: August 6, 2001

Latest Version: http://simonstl.com/ns/namespaces/attributes/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 com.simonstl.namespace.attributes.AttribReplNamespaceFilter to define which namespaces on attributes should be replaced by other namespaces. 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 attributes 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" xmlns:z="http://simonstl.com/nothing" >
  <z:name p:givenName="Chip" p:familyName="Skillet" />
  <z:address p:city="Ithaca" z:planet="earth" />
</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/attributes/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.attributes.AttribReplFilterTester replaceAtt1.xml atttest2.xml
<?xml version="1.0" standalone="yes"?>

<p:person xmlns:p="http://simonstl.com/person">

<z:name __NS1:givenName="Chip" __NS1:familyName="Skillet" xmlns:z="http://simonstl.com/nothing" xmlns:__NS1="http://example.com/"></z:name>

<z:address __NS1:city="Ithaca" z:planet="earth" xmlns:z="http://simonstl.com/nothing" xmlns:__NS1="http://example.com/"></z:address>
</p:person>

The http://simonstl.com/address namespace remains unchanged, while http://simonstl.com/person becomes http://example.com/. The XMLWriter creates its own namespace prefix for the new namespace URI.

The next set of rules also changes the prefix:

<namespaces xmlns="http://simonstl.com/ns/namespaces/attributes/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.attributes.AttribReplFilterTester replaceAtt2.xml atttest2.xml
<?xml version="1.0" standalone="yes"?>

<p:person xmlns:p="http://simonstl.com/person">

<z:name example:givenName="Chip" example:familyName="Skillet" xmlns:z="http://simonstl.com/nothing" xmlns:example="http://example.com/"></z:name>

<z:address example:city="Ithaca" z:planet="earth" xmlns:z="http://simonstl.com/nothing" xmlns:example="http://example.com/"></z:address>
</p: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 (well, not quite) remove a prefix:

<namespaces xmlns="http://simonstl.com/ns/namespaces/attributes/replace/">
<replace matchURI="http://simonstl.com/person" 
  resultURI="http://example.com/1" 
  resultPrefix="example"/>
<replace matchURI="http://simonstl.com/nothing" 
  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.attributes.AttribReplFilterTester replaceAtt3.xml atttest2.xml
<?xml version="1.0" standalone="yes"?>

<p:person xmlns:p="http://simonstl.com/person">

<z:name example:givenName="Chip" example:familyName="Skillet" xmlns:z="http://simonstl.com/nothing" xmlns:example="http://example.com/1"></z:name>

<z:address example:city="Ithaca" __NS1:planet="earth" xmlns:z="http://simonstl.com/nothing" xmlns:example="http://example.com/1" xmlns:__NS1="http://example.com
/2"></z:address>
</p:person>

The http://simonstl.com/address namespace becomes http://example.com/2, and the z prefix is removed entirely - except that the XMLWriter used for output insists on applying its own prefix to namespace-qualified attributes. 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 attreplrules.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