XSLT doesn't like xmlns?


B

Boris

I'm making my first steps with XSLT and run into a problem where I didn't
find an explanation yet for. Have a look at this very simple XML document
(DocBook) ...

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="test.xslt"?>
<book version="5.0">
<title>Title</title>
</book>

.... and another simple XSLT document (test.xslt):

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html><body>
<h1><xsl:value-of select="book/title" /></h1>
</body></html>
</xsl:template>
</xsl:stylesheet>

The problem is that if I add xmlns="http://docbook.org/ns/docbook" to the
book-element neither my XML editor (XML Notepad 2007) nor my browser
(Opera 9.26) show the book title anymore. However without xmlns my XML
editor doesn't know of course where the DocBook tags come from.

Is this a problem of my tools or XSLT simply doesn't like xmlns? In the
latter case is there any workaround?

Boris
 
Ad

Advertisements

B

Bjoern Hoehrmann

* Boris wrote in comp.text.xml:
I'm making my first steps with XSLT and run into a problem where I didn't
find an explanation yet for. Have a look at this very simple XML document
(DocBook) ...

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="test.xslt"?>
<book version="5.0">
<title>Title</title>
</book>

... and another simple XSLT document (test.xslt):

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html><body>
<h1><xsl:value-of select="book/title" /></h1>
</body></html>
</xsl:template>
</xsl:stylesheet>

The problem is that if I add xmlns="http://docbook.org/ns/docbook" to the
book-element neither my XML editor (XML Notepad 2007) nor my browser
(Opera 9.26) show the book title anymore. However without xmlns my XML
editor doesn't know of course where the DocBook tags come from.

You have to declare that namespace and use the prefix to refer to the
elements, for example,

<xsl:stylesheet xmlns:d='http://docbook.org/ns/docbook' ...>
...
<xsl:value-of select="d:book/d:title" />
...

Bare names in XPath 1.0 always refer to an element in no namespace,
if you put them into one using the default namespace declaration, the
names change, but your paths still refer to the old names.
 
Ad

Advertisements

J

Joseph J. Kesselman

XSLT is fully namespace-aware.
The problem is that if I add xmlns="http://docbook.org/ns/docbook" to
the book-element

By doing so, you have moved that element and all its children which
don't have prefixes into the docbook namespace. (See the XML Namespaces
specification for details, or a good modern XML tutorial.) To select a
namespaced node, XPath and XSLT must explicitly say that this is what
they're doing. Since XSLT 1.0 doesn't support the concept of a default
namespace, you must use prefixes to do this. For example, you could change
<xsl:value-of select="book/title" />
to
<xsl:value-of select="docbook:book/docbook:title"
xmlns:docbook="http://docbook.org/ns/docbook" />






neither my XML editor (XML Notepad 2007) nor my browser
 

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

Top