RE: [xsl] how to read attribute if defined as <namespace>:<attname>

Subject: RE: [xsl] how to read attribute if defined as <namespace>:<attname>
From: "Lars Huttar" <lars_huttar@xxxxxxx>
Date: Mon, 14 Jul 2003 13:48:56 -0500
> here is sample xml document, I want to read the value of 
> attribute xmlns.Res
[read xmlns:Res]
> using XSL stylesheet 

This is going to be different than you expect, because namespace
declarations, like xmlns:Res, are not really attributes
(they're pseudo-attributes). (Oh, Michael Kay told you that already.)

It looks like you're trying to get the namespace URI for the "Res"
namespace prefix. The way to do that would be:
	<xsl:value-of select="namespace::Res" />
in your template.

(xsl:copy-of will not work here unless there is an element
node to contain the result. You can't create a namespace node
without an element parent.)

The above is assuming you want the namespace-URI for Res
on each HFPT, which I'm guessing is a descendant of PT,
otherwise you wouldn't be asking in that context.

However, this is not guaranteed to work because
XSL isn't obligated to preserve prefix names (though it
usually will). It's only obligated to make sure the right
namespace-URIs continue to be associated with the right
nodes.  (Someone please correct me if that's inaccurate.)
The rationale seems to be that namespace prefixes are just
syntactic convenience, and don't really mean anything beyond
the scope of a particular document; it's the namespace URIs that
are important.

So, if you're counting on namespace prefixes being meaningful,
it really looks like you're trying to go about something the wrong
way.
Maybe what you really want is to find out the namespace URI of
a particular node. In that case the XSL is:

  <xsl:value-of select="namespace-URI(/PT/aResNode)" />

for example.

HTH.

Incidentally, for the XSLT knowledgeable out there, in testing
a stylesheet for this question I got an error in XMLSpy but
Saxon didn't complain. Is XMLSpy wrong? My stylesheet was
(almost like sumit's):

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
  <xsl:output method="html" indent="yes"/>
  <xsl:template match="HFPT" xmlns="http://www.w3.org/XML/1998/namespace";>
    <xsl:value-of select="namespace::Res"/>
  </xsl:template>
</xsl:stylesheet>

I tested it on this XML input, if that matters:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="sumit.xsl"?>
<PT xmlns:Res="PT_MSG">
  <HFPT>3</HFPT>
</PT>

When I try to run the stylesheet on this input in XMLSPy, it gives
the error:
  This file is not valid:
  Error in XPath expression, Invalid prefix
And it highlights the part of the stylesheet that says
       select="namespace::Res"

Saxon on the other hand runs the stylesheet and prints the expected
  PT_MSG

Thanks,
Lars

> 
> <?xml version="1.0" ?> 
> <PT xmlns.Res="PT_MSG"> 
> ....
> </PT>
> 
> here's my stylesheet
> <?xml version="1.0"?>
> <xsl:stylesheet  version="1.0" 
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
> <xsl:output method="html" indent="yes" />
> 
> 		<xsl:template match="HFPT" 
> xmlns="http://www.w3.org/XML/1998/namespace";> 
> 		<xsl:copy-of select="@xmlns:Res" />
> 	        </xsl:template>
> 
> </xsl:stylesheet>
> 
> I tried <xsl:copy-of select="@xmlns:Res" />, I got error message -
> "Namespace prefix xmlns has not been declared".
> 
> So how can I do it in XSL ?
> 
> Any solution will be appreciated.
> 
> Thanks


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread