Simple XSL Namespace Question

J

joe.osowski

OK, I hate to ask, but I seem to have run across one of those issues
where there answer is so grossly obvious, I can't see it.

Scenario:

XML:

<?xml version="1.0" encoding="utf-8"?>
<Report>
<user>
<lastname>Smith</lastname>
</user>
</Report>

XSL:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:eek:utput method="text" encoding="utf-8" />

<xsl:template match="/">
<xsl:value-of select="Report/user/lastname" />
</xsl:template>
</xsl:stylesheet>

Outputs:

Smith

Scenario 2: (adding a namespace)

<?xml version="1.0" encoding="utf-8"?>
<Report xmlns="http://schema.somedomain.com/Schema">
<user>
<lastname>Smith</lastname>
</user>
</Report>

XSL: (same)

Outputs: (null)

How / where do I add the namespace reference in the XSL to make this
work?

Thanks
 
C

C. M. Sperberg-McQueen

OK, I hate to ask, but I seem to have run across one of those issues
where there answer is so grossly obvious, I can't see it.

[details and examples snipped.]
Scenario 2: (adding a namespace)
...
How / where do I add the namespace reference in the XSL to make this
work?

Thanks

Two places: (1) somewhere in the document, declare a prefix
for the namespace http://schema.somedomain.com/Schema, and
(2) in the XPath expression, use that prefix for the
elements in that namespace.

<xsl:stylesheet version="1.0"
xmlns:x="http://schema.somedomain.com/Schema"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:eek:utput method="text" encoding="utf-8" />

<xsl:template match="/">
<xsl:value-of select="x:Report/x:user/x:lastname" />
</xsl:template>
</xsl:stylesheet>

Don't try to use the default namespace: XPath is defined not
to honor any declarations for the default namespace: if a
name has no prefix, it's assumed to have no namespace.
(Don't ask me why; I've never quite understood. I believe
it seemed like a good idea at the time, to the responsible
people.)

I hope this helps.

-C. M. Sperberg-McQueen
World Wide Web Consortium
 
D

David Carlisle

Don't try to use the default namespace: XPath is defined not
to honor any declarations for the default namespace: if a
name has no prefix, it's assumed to have no namespace.
(Don't ask me why; I've never quite understood. I believe
it seemed like a good idea at the time, to the responsible
people.)

One reason is that if you bind NCNames to a namespace then it is no
longer possible to access elements in no-namespace (as it's not possible
to bind no-namespace to a prefix)

Xpath2 which does allow you to specify namespaces for unprefixed
element names and function names in Xpaths needs to put all the core
functions in a namespace so that you can still access the core functions
even if the default function namespace is changed. Xpath1 functions are
in no-namespace.

David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,539
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top