XSLT doesn't like xmlns?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Boris

    XSLT doesn't like xmlns?

    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:styleshe et version="1.0"
    xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
    <html><body>
    <h1><xsl:valu e-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
  • Bjoern Hoehrmann

    #2
    Re: XSLT doesn't like xmlns?

    * 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:styleshee t version="1.0"
    >xmlns:xsl="htt p://www.w3.org/1999/XSL/Transform">
    ><xsl:templat e match="/">
    <html><body>
    <h1><xsl:valu e-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:styleshe et 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.
    --
    Björn Höhrmann · mailto:bjoern@h oehrmann.de · http://bjoern.hoehrmann.de
    Weinh. Str. 22 · Telefon: +49(0)621/4309674 · http://www.bjoernsworld.de
    68309 Mannheim · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/

    Comment

    • Joseph J. Kesselman

      #3
      Re: XSLT doesn't like xmlns?

      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
      (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

      Comment

      Working...