Rules for stripping namespaces from attributes

This Version: August 5, 2001

Latest Version: http://simonstl.com/ns/namespaces/attributes/strip/

Previous Version: None

Editors:

Table of contents

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

1. Introduction

This vocabulary addresses problems created by the expectation that attributes without prefixes fall into the namespace of the element containing them (since they don't fall into the default namespace). Some vocabularies, notably RDF, are moving toward requirements that all attributes have explicit markup identifying the namespace to which they belong. It also provides a filter that will strip explicit namespace qualifications from attributes.

The com.simonstl.namespace.attributes.AttribStripNamespaceFilter will use this vocabulary to convert markup that looks like:

<p:person xmlns:p="http://simonstl.com/person" 
  p:givenName="Chip" p:familyName="Skillet" p:city="Ithaca"
/>

to:

<p:person xmlns:p="http://simonstl.com/person" 
  givenName="Chip" familyName="Skillet" city="Ithaca"
/>

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

2. Rules

Attribute Namespace-Stripping examples

There may be times when you want to remove explicit namespace qualification on attributes. The com.simonstl.namespace.attributes.AttribNamespaceFilter allows you to specify which namespaces you want removed or not removed. This filter may substantially change the contents of your document, so be careful in using it.

All of the examples will test rules against 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 set of rules strips attributes in the http://simonstl.com/person namespace:

<namespaces xmlns="http://simonstl.com/ns/namespaces/attributes/strip/">
<include nsURI="http://simonstl.com/person" />
</namespaces>

This removes the namespace from the attributes (and only the attributes) while leaving the other namespace used for attributes in place:

C:\opensrc\namespace>java com.simonstl.namespace.attributes.AttribStripFilterTester stripAtt1.xml atttest2.xml
<?xml version="1.0" standalone="yes"?>
<p:person xmlns:p="http://simonstl.com/person">
<z:name givenName="Chip" familyName="Skillet" xmlns:z="http://simonstl.com/nothi
ng"></z:name>
<z:address city="Ithaca" z:planet="earth" xmlns:z="http://simonstl.com/nothing">
</z:address>
</p:person>

The next set of rules strips all namespaces from attributes in the document:

<namespaces xmlns="http://simonstl.com/ns/namespaces/attributes/strip/">
<include nsURI="##any" />
</namespaces>

The result is a document with no explicitly-namespace-qualified attributes:

C:\opensrc\namespace>java com.simonstl.namespace.attributes.AttribStripFilterTester stripAtt2.xml atttest2.xml
<?xml version="1.0" standalone="yes"?>
<p:person xmlns:p="http://simonstl.com/person">
<z:name givenName="Chip" familyName="Skillet" xmlns:z="http://simonstl.com/nothi
ng"></z:name>
<z:address city="Ithaca" planet="earth" xmlns:z="http://simonstl.com/nothing"></
z:address>
</p:person>

The next two examples demonstrate the exclusion of namespaces from this stripping. The first demonstrates that exclude is more powerful than include:

<namespaces xmlns="http://simonstl.com/ns/namespaces/attributes/strip/">
<include nsURI="http://simonstl.com/person" />
<exclude nsURI="http://simonstl.com/person" />
</namespaces>

All the namespaces are preserved:

C:\opensrc\namespace>java com.simonstl.namespace.attributes.AttribStripFilterTester stripAtt3.xml atttest2.xml
<?xml version="1.0" standalone="yes"?>
<p:person xmlns:p="http://simonstl.com/person">
<z:name p:givenName="Chip" p:familyName="Skillet" xmlns:z="http://simonstl.com/n
othing"></z:name>
<z:address p:city="Ithaca" z:planet="earth" xmlns:z="http://simonstl.com/nothing
"></z:address>
</p:person>

The last example will strip all namespaces from attributes except for http://simonstl.com/person:

<namespaces xmlns="http://simonstl.com/ns/namespaces/attributes/strip/">
<include nsURI="##any" />
<exclude nsURI="http://simonstl.com/person"/>
</namespaces>

As a result, the attribute planet is no longer explicitly a member of the http://simonstl.com/nothing namespace:

C:\opensrc\namespace>java com.simonstl.namespace.attributes.AttribStripFilterTester stripAtt4.xml atttest2.xml
<?xml version="1.0" standalone="yes"?>
<p:person xmlns:p="http://simonstl.com/person">
<z:name p:givenName="Chip" p:familyName="Skillet" xmlns:z="http://simonstl.com/n
othing"></z:name>
<z:address p:city="Ithaca" planet="earth" xmlns:z="http://simonstl.com/nothing">
</z:address>
</p:person>

3. Related Resources

3.1 Document Type Definition

A DTD attstriprules.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