XSLT: Normalizing namespaces

Discussion in 'XML' started by Thomas Wittek, Aug 30, 2007.

  1. Hi!

    Suppose I have to handle documents with the same schema [1] but some
    instances use a (proper) namespace and some instances just use the
    default (empty) namespace.

    Examples:

    <doc xmlns="urn:some_ns">
    <foo>bar</foo>
    </doc>

    <doc>
    <foo>bar</foo>
    </doc>

    Currently I have to write each template like this:

    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:somens="urn:some_ns">

    <xsl:template match="foo | somens:foo" />

    </xsl:stylesheet>

    That's quite tedious to do for each and every XPath selection.

    I could imagine a modified identity transformation that changes the
    namespace for a given document.
    But then I have to run two XLSTs consecutively, whereas I would prefer
    to have the transformation in *one* file.

    So is there any possibilty to normalize the namespace of the input
    document instead of preprocessing it with another XSLT? Or override the
    default namespace of the input doc?

    Thank you for any ideas!

    [1] Or at least the same structure.
    Obviously the doc without the namespace cannot use the same
    "XML Schema" becaus it's not in the correct namespace.
    --
    Thomas Wittek
    Web: http://gedankenkonstrukt.de/
    Jabber: -pobox.net
    GPG: 0xF534E231
     
    Thomas Wittek, Aug 30, 2007
    #1
    1. Advertising

  2. Thomas Wittek wrote:
    > <doc xmlns="urn:some_ns">
    > <foo>bar</foo>
    > </doc>
    >
    > <doc>
    > <foo>bar</foo>
    > </doc>


    The whole point of namespaces is that those two documents have different
    semantic meanings. You can't, and shouldn't "normalize" them.

    If you really insist on ignoring these differences your choices are
    either to explicitly match all the possibilities:

    > <xsl:template match="foo | somens:foo" />


    or to write patterns that match on things like *[localname(.)='foo'].
    The latter which will get you either laughed at or screamed at by folks
    who understand the intent of namespaces.

    --
    Joe Kesselman / Beware the fury of a patient man. -- John Dryden
     
    Joseph Kesselman, Aug 30, 2007
    #2
    1. Advertising

  3. Joseph Kesselman schrieb:
    > Thomas Wittek wrote:
    >> <doc xmlns="urn:some_ns">
    >> <foo>bar</foo>
    >> </doc>
    >>
    >> <doc>
    >> <foo>bar</foo>
    >> </doc>

    >
    > The whole point of namespaces is that those two documents have different
    > semantic meanings. You can't, and shouldn't "normalize" them.


    Of course. But in this case the docs have perfectly the same semantics.
    The only difference is that on some docs the generator didn't specify
    the namespace.

    > If you really insist on ignoring these differences your choices are
    > either to explicitly match all the possibilities:
    >
    >> <xsl:template match="foo | somens:foo" />

    >
    > or to write patterns that match on things like *[localname(.)='foo'].
    > The latter which will get you either laughed at or screamed at by folks
    > who understand the intent of namespaces.


    :) And it's almost equally as ugly as "foo | somens:foo".

    The best solution might be adding the missing namespace to the docs.
    But this would add an inconvenient preprocessing step.
    Having it all in one XSLT would be nice.

    --
    Thomas Wittek
    Web: http://gedankenkonstrukt.de/
    Jabber: -pobox.net
    GPG: 0xF534E231
     
    Thomas Wittek, Aug 30, 2007
    #3
  4. Thomas Wittek schrieb:
    > The best solution might be adding the missing namespace to the docs.
    > But this would add an inconvenient preprocessing step.
    > Having it all in one XSLT would be nice.


    Suppose I'd do that.
    Hm, would I miss anything but the comment and pi nodes using this
    "identity" transformation to a new namespace "foo"?

    <xsl:template match="*">
    <xsl:choose>
    <!-- don't overwrite existing namespaces -->
    <xsl:when test="namespace-uri()">
    <xsl:copy>
    <xsl:copy-of select="@*" />
    <xsl:apply-templates />
    </xsl:copy>
    </xsl:when>
    <xsl:eek:therwise>
    <xsl:element name="{name()}" namespace="urn:foo">
    <xsl:copy-of select="@*" />
    <xsl:apply-templates />
    </xsl:element>
    </xsl:eek:therwise>
    </xsl:choose>
    </xsl:template>

    The identity transformations I've seen are a bit different.
    As I'm quite new to XSLT I'm not sure, if I miss anything with the code
    above.

    Maybe it would be easier to just "somehow" (DOM, regexp) add a default
    namespace to those documents...

    --
    Thomas Wittek
    Web: http://gedankenkonstrukt.de/
    Jabber: -pobox.net
    GPG: 0xF534E231
     
    Thomas Wittek, Aug 30, 2007
    #4
  5. Thomas Wittek wrote:
    > Of course. But in this case the docs have perfectly the same semantics.
    > The only difference is that on some docs the generator didn't specify
    > the namespace.


    And thus changed the semantics. That generator is misusing XML; fix
    and/or educate it.

    > The best solution might be adding the missing namespace to the docs.


    The _best_ solution involves not producing broken documents. Insist they
    say what they meant, rather than trying to guess what was meant. You'll
    be doing both them and yourself a favor in the long run.

    --
    Joe Kesselman / Beware the fury of a patient man. -- John Dryden
     
    Joseph Kesselman, Aug 30, 2007
    #5
  6. Thomas Wittek wrote:

    > <xsl:template match="foo | somens:foo" />



    > That's quite tedious to do for each and every XPath selection.


    With XSLT/XPath 2.0 you could use a wildcard * for the prefix e.g.
    <xsl:template match="*:foo">
    matches elements with local name foo in any namespace (including no
    namespace).



    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Aug 31, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris

    Method for normalizing URL?

    Chris, Jan 10, 2005, in forum: Java
    Replies:
    6
    Views:
    1,128
    Chris Smith
    Jan 12, 2005
  2. Ryan Stewart

    Normalizing XHTML with XML

    Ryan Stewart, May 11, 2006, in forum: XML
    Replies:
    3
    Views:
    416
    Ryan Stewart
    May 11, 2006
  3. Stu

    Normalizing tm structure past 2038

    Stu, Oct 31, 2003, in forum: C Programming
    Replies:
    5
    Views:
    435
    Chris Torek
    Nov 1, 2003
  4. Replies:
    1
    Views:
    375
    Joshua Cranmer
    Apr 9, 2007
  5. Dan Ellis

    Normalizing arguments

    Dan Ellis, Oct 17, 2008, in forum: Python
    Replies:
    6
    Views:
    262
    Dan Ellis
    Oct 19, 2008
Loading...

Share This Page