[xsl] adding namespace node to arbitrary <xsl:element> - xslt 1 or 2

Subject: [xsl] adding namespace node to arbitrary <xsl:element> - xslt 1 or 2
From: "Benjamin Li" <xycodex@xxxxxxxxx>
Date: Wed, 16 May 2007 17:47:10 -0400
Hi,

I'm having a problem dealing with namespaces in my stylesheet.

My problem is that I have to transform a document into a result document
that contains inline xml. let me explain.

The result document is read by another java program (created by an external
party), and a certain section/fragment of the result document is read as an
independent xml file, hence the term 'inline'.

The main problem is that I need to re-declare certain namespaces in this
fragment. I managed to get to this stage:


<mets:mets xmlns:dc=3D"http://purl.org/dc/elements/1.1/"; xmlns:xs=3D"http://www.w3.org/2001/XMLSchema"; xmlns:xsi=3D"http://www.w3.org/2001/XMLSchema-instance"; xmlns:xlink=3D"http://www.w3.org/1999/xlink"; xmlns:mets=3D"http://www.loc.gov/METS/"; OBJID=3D"test:chc03560000270001001" LABEL=3D"Ram=F3n" TYPE=3D"FedoraObject"> <mets:metsHdr RECORDSTATUS=3D"A"/> <mets:dmdSecFedora STATUS=3D"A" ID=3D"DC"> <mets:descMD ID=3D"DC1.0"> <mets:mdWrap MIMETYPE=3D"text/xml" MDTYPE=3D"DC" LABEL=3D"DC"> <mets:xmlData> <!--start inline xml--> <oai_dc:dc xmlns:oai_dc=3D" http://www.openarchives.org/OAI/2.0/oai_dc/";> <dc:title>Some Title</dc:title> <dc:creator>Barqu=EDn, B.J.</dc:creator> <dc:format>image/tiff</dc:format> <dc:type>Still Image</dc:type> </oai_dc:dc> <!--end inline xml--> </mets:xmlData> </mets:mdWrap> </mets:descMD>

ie, in this example, the fragment with the element <oai_dc> is read
independently as its own xml document. thus, the java program chokes up whe=
n
it tries to read the <dc:title> and other child elements, since dc is not
defined in the <oai_dc> scope in that fragment, as far as the program is
concerned.

it seems that when the xslt processor serializes the output document, it
does some 'fixup' with regards to namespaces.
i tried using xsl:copy to copy the dc namespace node, but it doesnt add the
declaration on the element, i think it got removed when the result tree was
serialized (is there an xslt debugger where i can see the result tree while
it is being built??)
i managed to get the the oai_dc namespace declared in the element,
presumably because it was not already declared in the document element of
the source document, which is the case for the dc namespace. here's the
template:

  <xsl:template match=3D"mets:xmlData">
      <xsl:element name=3D"mets:xmlData">
          <xsl:element name=3D"oai_dc:dc" namespace=3D"
http://www.openarchives.org/OAI/2.0/oai_dc/";>
              <xsl:copy-of select=3D"/mets:mets/namespace::dc" />
              <xsl:copy-of select=3D"node()" />
          </xsl:element>
      </xsl:element>
  </xsl:template>

from what i can see, this whole 'inline xml' business is a Bad Idea, but i
have little choice, so any help is appreciated!

cheers,
ben

Current Thread