This Version: August 5, 2001
Latest Version: http://simonstl.com/ns/namespaces/elements/strip/
Previous Version: None
Editors:
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.
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.