Re: [xsl] Namespace handling question

Subject: Re: [xsl] Namespace handling question
From: David Carlisle <davidc@xxxxxxxxx>
Date: Fri, 14 May 2010 20:13:22 +0100
On 14/05/2010 19:36, Bridger Dyson-Smith wrote:
All -
thanks for reading. I'm trying to generate OAI records for a series of
XML files; while I have the bulk of the processing working, I'm stuck
on a namespaces issue. I understand how to include/add a namespace to
my output file, I'm curious if there's a way to keep all of the
namespace information in the XML header - primarily to keep things
tidy. I've found a short XSL from Michael Kay on the dpawson website
for cleaning up the XML after the initial transform and I've
incorporated that as a second step in the process. Is there a good way
to combine the two?

And a second, related question: there is an xsi:schemaLocation that I
apparently need to include in the XML header. Attempts at using
<xsl:import-schema>  and<xsl:namespace>  haven't yielded correct
results. Would someone be willing to recommend a method for this? (See
the final example for my goal output).

I'm using XLST 2.0 and the Saxon-HE 9.2.0.6 in the oXygen editor.

Again, many thanks. Please excuse me if this has been beaten to death
- web searches and the list archives have so far refused to answer
this.
Best,
Bridger

My original XML looks something like this:
<root>
	<row>
		<title>Mice</title>
		<author>Kat, Krazy</author>
		<subject>bricks</subject>
		<number>01101100011011110111011001100101</number>
	</row>	
</root>

And I'm processing with the following XSL:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="2.0" xmlns:oai_dc="http://www.openarchives.org/OAI/2.0/oai_dc/";
     xmlns:dc="http://purl.org/dc/elements/1.1/";
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";>

<xsl:output method="xml" media-type="text/xml" indent="yes"/>


<xsl:template match="/"> <xsl:apply-templates /> </xsl:template>

That is the default so you don't need to declare this template.

<xsl:template match="row"> <xsl:for-each select=".">

xsl:for-each iterating over . is a no-op so thi scan be omitted



             <xsl:result-document href="{concat(child::title,
child::number, '.xml')}">
You only need xsl;element and xsl:namespace if these are going to use values dynnamically generated at run time.
                 <xsl:element name="oai_dc:dc">
                     <xsl:namespace name="oai_dc"
select="'http://www.openarchives.org/OAI/2.0/oai_dc/'"/>
                     <xsl:namespace name="xsi"
select="'http://www.w3.org/2001/XMLSchema-instance'"/>




so the above could be written
<oai_dc:dc>
as all these namespaces are already in scope
although you say above you want a schemalocation attribute, this is not a special attribute to xsl so you just generate it as any other, so for example as a lteral result element attribute


<oai_dc:dc xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/oai_dc/http://www.openarchives.org/OAI/2.0/oai_dc.xsd";>


so putting it together and ignoring the xsl:result-document for now I think you want something like



<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; xmlns:xs="http://www.w3.org/2001/XMLSchema"; xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; xmlns:oai_dc="http://www.openarchives.org/OAI/2.0/oai_dc/"; xmlns:dc="http://purl.org/dc/elements/1.1/"; >

<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="row">
<oai_dc:dc xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/oai_dc/http://www.openarchives.org/OAI/2.0/oai_dc.xsd";>
<dc:publisher>Ignatz Mouse Publishers</dc:publisher>
<dc:creator><xsl:value-of select="author"/></dc:creator>
<dc:title><xsl:value-of select="title"/></dc:title>
<dc:subject><xsl:value-of select="subject"/></dc:subject>
<dc:identifier><xsl:value-of select="number"/></dc:identifier>
</oai_dc:dc>
</xsl:template>
</xsl:stylesheet>





which produces



$ saxon9 ns1.xml ns1.xsl <?xml version="1.0" encoding="UTF-8"?> <oai_dc:dc xmlns:xs="http://www.w3.org/2001/XMLSchema"; xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; xmlns:oai_dc="http://www.openarchives.org/OAI/2.0/oai_dc/"; xmlns:dc="http://purl.org/dc/elements/1.1/";

xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/oai_dc/http://www.openarchives.org/OAI/2.0/oai_dc.xsd";>
   <dc:publisher>Ignatz Mouse Publishers</dc:publisher>
   <dc:creator>Kat, Krazy</dc:creator>
   <dc:title>Mice</dc:title>
   <dc:subject>bricks</dc:subject>
   <dc:identifier>01101100011011110111011001100101</dc:identifier>
</oai_dc:dc>

Current Thread