[XML4Lib] need to remove namespace

Conal Tuohy Conal.Tuohy at vuw.ac.nz
Tue Jun 6 17:57:58 EDT 2006


John Fitzgibbon wrote:

> I have written a style sheet that transforms an XML file that has been
> created with MarcEdit. The default namespace in this file is marc. All
> elements have the marc: prefix.
> 
> My style sheet does not reference elements in the marc: 
> namespace. When
> I created test data I removed marc: from the text and wrote the style
> sheet without regard to any marc namespace. It is not possible to do
> this with the real data as the file is too large and such a 'find and
> replace' is taking several minutes.
> 
> How do I make my style sheet automatically treat all elements as
> belonging to the marc namespace? I tried including an attribute in the
> style sheet element of 

The short answer is: "you can't" :-)

You will need to declare the marc namespace in your stylesheet WITH A
PREFIX. i.e. not use a default namespace. The reason is that (at least
in XSLT 1.0), you have to use namespace prefix in all your XPath
expressions. A default namespace is a convenience only for CREATING
elements in a namespace in your output document - it doesn't apply at
all to the namespace of your input document. 

You might think that the default namespace of your stylesheet matches
the namespace of elements in the document you are transforming, that you
could match elements like so: <xsl:template match="controlfield">, but
this is not the case. In XSLT 1.0, this would match on an element called
"controlfield" in NO NAMESPACE. If you want to match on a namespaced
element, you MUST use a namespace prefix.

e.g. 

<xsl:stylesheet 
	version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:marc="http://www.loc.gov/MARC21/slim">

	<xsl:template match="marc:datafield/marc:subfield">
		etc.
	</xsl:template>

</xsl:stylesheet>

Regards

Con


More information about the XML4Lib mailing list