Rules for stripping namespaces from elements

This Version: August 5, 2001

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

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.ElementStripNamespaceFilter, which simply strips namespaces from elements in the document. For example, the document below has a mixture of qualified and unqualified names:

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

Instead of going to a fully-qualified version, this approach would produce something like:

<person>
  <givenName>Chip</givenName>
  <familyName>Skillet</familyName>
  <address>
    <city>Ithaca</city>
  </address>
</person>

(There may be a few stray namespace declarations remaining in the output.) This filter just strips namespaces according to rules. It does not precisely reverse the effect of the com.simonstl.namespace.elements.ElementNamespaceFilter SAX filter.

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

2. Rules

Element Namespace-Stripping Examples

The examples use the ElementStripFilterTester class, which takes a rules file and a target file as arguments and returns results to standard output. All of the examples apply rules 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>

For our first example, we'll use a small rule set:

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

This strips out the http://simonstl.com/person namespace, but not the http://simonstl.com/address namespace:

C:\opensrc\namespace>java com.simonstl.namespace.elements.ElementStripFilterTester stripEl1.xml test2.xml
<?xml version="1.0" standalone="yes"?>
<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>
</person>

The next rule set obliterates all namespaces on elements:

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

It produces namespace-free results:

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

Next, we'll demonstrate that exclude elements override include elements:

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

The result is an unmodified document (with regard to namespaces):

C:\opensrc\namespace>java com.simonstl.namespace.elements.ElementStripFilterTester stripEl3.xml test2.xml
<?xml version="1.0" standalone="yes"?>
<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 next example will strip all namespaces except the http://simonstl.com/person namespace:

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

The result retains the http://simonstl.com/person namespace but not the http://simonstl.com/address namespace:

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

As with any code that discards data, this filter should be applied carefully and only after extended consideration of its consequences.

3. Related Resources

3.1 Document Type Definition

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