Copying without namespace?

Discussion in 'XML' started by Andre-John Mas, Feb 29, 2008.

  1. Hi,

    I have some code that looks as follows:

    <xsl:for-each select="$pageMetaData/*">
    <xsl:choose>
    <xsl:when test="local-name() = 'meta'">
    <xsl:copy-of select="."/>
    </xsl:when>
    <xsl:eek:therwise>
    <meta content="{.}" name="{local-name()}"/>
    </xsl:eek:therwise>
    </xsl:choose>
    </xsl:for-each>

    The problem is that it copies the namespace when it does so (it
    appears to be implied, rather than declared). So this:

    <meta content="abc" name="dc.creator"/>

    becomes this in the final output:

    <meta xmlns="http://www.myco.com/datalist" content="yuri"
    name="dc.creator"/>

    How do I copy the element and not have the namespace in the result?
    Note that I only want to apply this here, and not globaly to all
    cases.

    Andre
     
    Andre-John Mas, Feb 29, 2008
    #1
    1. Advertising

  2. Andre-John Mas

    Pavel Lepin Guest

    Andre-John Mas <> wrote in
    <>:
    > <xsl:for-each select="$pageMetaData/*">
    > <xsl:choose>
    > <xsl:when test="local-name() = 'meta'">
    > <xsl:copy-of select="."/>
    > </xsl:when>
    > <xsl:eek:therwise>
    > <meta content="{.}" name="{local-name()}"/>
    > </xsl:eek:therwise>
    > </xsl:choose>
    > </xsl:for-each>


    Awful.

    > The problem is that it copies the namespace when it does
    > so (it appears to be implied, rather than declared).


    "Implied rather than declared"? What do you mean?

    > So this:
    >
    > <meta content="abc" name="dc.creator"/>
    >
    > becomes this in the final output:
    >
    > <meta xmlns="http://www.myco.com/datalist" content="yuri"
    > name="dc.creator"/>


    And what is the problem - the fact that your resulting meta
    element is in http://www.myco.com/datalist namespace, or
    the fact that this namespace is explicitly declared as the
    default namespace on this element?

    > How do I copy the element and not have the namespace in
    > the result?


    You can't. You can only create an element with the same
    name, but not in a namespace.

    I would recommend posting a minimal complete example of your
    problem, since it's not really clear to me what your
    problem is. Reading some introductory materials on XML
    Namespaces might be advisable as well.

    --
    When all you have is a transformation engine, everything
    looks like a tree.
     
    Pavel Lepin, Feb 29, 2008
    #2
    1. Advertising

  3. Pavel Lepin schrieb:
    > Andre-John Mas <> wrote in
    > <>:
    >> <xsl:for-each select="$pageMetaData/*">
    >> <xsl:choose>
    >> <xsl:when test="local-name() = 'meta'">
    >> <xsl:copy-of select="."/>
    >> </xsl:when>
    >> <xsl:eek:therwise>
    >> <meta content="{.}" name="{local-name()}"/>
    >> </xsl:eek:therwise>
    >> </xsl:choose>
    >> </xsl:for-each>

    >
    > Awful.
    >
    >> The problem is that it copies the namespace when it does
    >> so (it appears to be implied, rather than declared).

    >
    > "Implied rather than declared"? What do you mean?
    >
    >> So this:
    >>
    >> <meta content="abc" name="dc.creator"/>
    >>
    >> becomes this in the final output:
    >>
    >> <meta xmlns="http://www.myco.com/datalist" content="yuri"
    >> name="dc.creator"/>

    >
    > And what is the problem - the fact that your resulting meta
    > element is in http://www.myco.com/datalist namespace, or
    > the fact that this namespace is explicitly declared as the
    > default namespace on this element?
    >
    >> How do I copy the element and not have the namespace in
    >> the result?

    >
    > You can't. You can only create an element with the same
    > name, but not in a namespace.
    >
    > I would recommend posting a minimal complete example of your
    > problem, since it's not really clear to me what your
    > problem is.


    I guess, the problem is that DTDs are not namespace-aware and so the
    result of the transformation is not DTD-valid.

    --
    Johannes Koch
    In te domine speravi; non confundar in aeternum.
    (Te Deum, 4th cent.)
     
    Johannes Koch, Feb 29, 2008
    #3
  4. Andre-John Mas

    Pavel Lepin Guest

    Johannes Koch <> wrote in
    <47c7da46$0$580$>:
    > Pavel Lepin schrieb:
    >> Andre-John Mas <> wrote:
    >>> So this:
    >>>
    >>> <meta content="abc" name="dc.creator"/>
    >>>
    >>> becomes this in the final output:
    >>>
    >>> <meta xmlns="http://www.myco.com/datalist"
    >>> content="yuri" name="dc.creator"/>

    >>
    >> And what is the problem - the fact that your resulting
    >> meta element is in http://www.myco.com/datalist
    >> namespace, or the fact that this namespace is explicitly
    >> declared as the default namespace on this element?

    >
    > I guess, the problem is that DTDs are not namespace-aware
    > and so the result of the transformation is not DTD-valid.


    I think someone should start a "Let the DTDs go!" movement,
    would be about time. Hmm, maybe I could, but... hold on for
    a second... Yep. That's my natural laziness kicking in.
    Never mind.

    --
    When all you have is a transformation engine, everything
    looks like a tree.
     
    Pavel Lepin, Feb 29, 2008
    #4
  5. Johannes Koch wrote:
    > I guess, the problem is that DTDs are not namespace-aware and so the
    > result of the transformation is not DTD-valid.


    DTDs and namespaced XML are incompatable at some pretty basic levels.
    Namespace-aware processing assumes that prefixes are purely syntactic
    sugar and can be changed at will. DTDs will insist they be nailed down.

    The best suggestion I can give you for intermixing the two is to have
    your DTD hard-code a default namespace assignment and not use prefixes,
    and to have all the elements in the DTD likewise set up that default but
    permit it to be explicitly specified in case someone re-asserts it.

    The best suggestion I can give you generally is to stop trying to
    intermix these, and move to XML Schema.


    --
    Joe Kesselman / Beware the fury of a patient man. -- John Dryden
     
    Joseph Kesselman, Feb 29, 2008
    #5
  6. On Feb 29, 4:55 am, Pavel Lepin <> wrote:
    > Andre-John Mas <> wrote in
    > <>:
    >
    > >       <xsl:for-each select="$pageMetaData/*">
    > >         <xsl:choose>
    > >           <xsl:when test="local-name() = 'meta'">
    > >             <xsl:copy-of select="."/>
    > >           </xsl:when>
    > >           <xsl:eek:therwise>
    > >             <meta content="{.}" name="{local-name()}"/>
    > >           </xsl:eek:therwise>
    > >         </xsl:choose>
    > >       </xsl:for-each>

    >
    > Awful.


    I am not going argue this point. I am trying to convert code that
    claims it was creating xhtml pages (it was creating html), to actually
    create xhtml compliant output. There have been plenty of developers
    dipping in this code before me.

    > > The problem is that it copies the namespace when it does
    > > so (it appears to be implied, rather than declared).

    >
    > "Implied rather than declared"? What do you mean?
    >
    > > So this:

    >
    > > <meta content="abc" name="dc.creator"/>

    >
    > > becomes this in the final output:

    >
    > > <meta xmlns="http://www.myco.com/datalist" content="yuri"
    > > name="dc.creator"/>

    >
    > And what is the problem - the fact that your resulting meta
    > element is inhttp://www.myco.com/datalistnamespace, or
    > the fact that this namespace is explicitly declared as the
    > default namespace on this element?


    This name space is something used in the XSL, to refer to a
    configuration file specifying localised texts, it is not something
    that is meant to be in the output document.

    I'll see if I can cobble together an example, but basically what is
    being done is copying a block like this, declared in an XML file:

    <meta-tags>
    <meta http-equiv="expires" content="Wed, 26 Feb 1997 08:21:57
    GMT" />
    <meta name="authour" content="the author" />
    </meta-tags>

    to the output which will be used for rendering the final xhtml page.
    For example:

    <head>
    <title>mypage</title>
    <meta http-equiv="expires" content="Wed, 26 Feb 1997 08:21:57 GMT" /
    >

    <meta name="authour" content="the author" />
    </head>

    The issue is that I end up with an xhtml document that includes
    namespaces that were only meant to be used in the xsl and not the
    final document. Having the "http://www.myco.com/datalist" specified
    as part of the meta tag just breaks the page.

    Andre
     
    Andre-John Mas, Mar 5, 2008
    #6
  7. Andre-John Mas wrote:
    > The issue is that I end up with an xhtml document that includes
    > namespaces that were only meant to be used in the xsl and not the
    > final document.


    Sounds like you're looking for xsl:stylesheet's exclude-result-prefixes
    attribute.

    --
    Joe Kesselman / Beware the fury of a patient man. -- John Dryden
     
    Joseph Kesselman, Mar 5, 2008
    #7
  8. Andre-John Mas

    Pavel Lepin Guest

    Joseph Kesselman <> wrote in
    <47cef74e$1@kcnews01>:
    > Andre-John Mas wrote:
    >> The issue is that I end up with an xhtml document that
    >> includes namespaces that were only meant to be used in
    >> the xsl and not the final document.

    >
    > Sounds like you're looking for xsl:stylesheet's
    > exclude-result-prefixes attribute.


    Actually, to me it looks more like the OP wants to copy
    {meta-data-namespace}meta element as an
    {xhtml-namespace}meta element. Namespace prefix declaration
    alone shouldn't break XHTML validation I believe. meta
    element being in the wrong namespace would, on the other
    hand.

    --
    In Soviet Russia, XML documents transform *you*.
     
    Pavel Lepin, Mar 6, 2008
    #8
  9. Pavel Lepin schrieb:
    > Namespace prefix declaration
    > alone shouldn't break XHTML validation I believe.


    It breaks XHTML DTD validation, because the XHTML DTDs allow the xmlns
    "attribute" on the html element only.

    --
    Johannes Koch
    In te domine speravi; non confundar in aeternum.
    (Te Deum, 4th cent.)
     
    Johannes Koch, Mar 6, 2008
    #9
  10. Andre-John Mas

    Pavel Lepin Guest

    Johannes Koch <> wrote in
    <47cfb0a3$0$584$>:
    > Pavel Lepin schrieb:
    >> Namespace prefix declaration alone shouldn't break XHTML
    >> validation I believe.

    >
    > It breaks XHTML DTD validation, because the XHTML DTDs
    > allow the xmlns "attribute" on the html element only.


    Oops, my fault.

    Just reinforces my anti-DTD sentiment, though. I mean,
    seriously, in case of XHTML using DTD for validation is
    about as bad as using Appendix C for 'compatibility'. Both
    mean we cannot freely use XML tools for processing without
    a lot of kludgery to make sure nothing 'breaks' in the
    pipeline. Kinda defeats the whole point of XML.

    --
    In Soviet Russia, XML documents transform *you*.
     
    Pavel Lepin, Mar 6, 2008
    #10
  11. On Mar 5, 3:41 pm, Joseph Kesselman <>
    wrote:
    > Andre-John Mas wrote:
    > > The issue is that I end up with an xhtml document that includes
    > > namespaces that were only meant to be used in the xsl and not the
    > > final document.

    >
    > Sounds like you're looking for xsl:stylesheet's exclude-result-prefixes
    > attribute.


    That what I tried doing, currently I have:

    exclude-result-prefixes="i18n ota dataList iItemList math"

    which excludes non-anonymous namespaces, such as:

    xmlns:i18n=""
    xmlns:math=""

    but how do you exclude an anonymous namespace:

    xmlns=""

    Reading elsewhere I was given the suggestion of using:

    <!-- copies without the template -->
    <xsl:template match="*">
    <xsl:element name="{name()}" namespace="">
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates/>
    </xsl:element>
    </xsl:template>

    and changed the original block to:

    <xsl:for-each select="$pageMetaData/*">
    <xsl:choose>
    <xsl:when test="local-name() = 'meta'">
    <xsl:apply-templates select="."/>
    </xsl:when>
    <xsl:eek:therwise>
    <meta content="{.}" name="{local-name()}"/>
    </xsl:eek:therwise>
    </xsl:choose>
    </xsl:for-each>

    but I am not sure this is the best approach.

    Andre
     
    Andre-John Mas, Mar 10, 2008
    #11
  12. Andre-John Mas wrote:
    > but how do you exclude an anonymous namespace:
    > xmlns=""


    The default namespace declaration is generally not generated into the
    output unless it is required, meaning that the parent element is in a
    different default namespace. I'd have to take another look at your
    specific example to see whether that's what's going on, but if it is
    then the proper answer is "if you don't want to change default
    namespaces, don't output an element that's in a different default
    namespace than its parent."

    --
    Joe Kesselman / Beware the fury of a patient man. -- John Dryden
     
    Joseph Kesselman, Mar 10, 2008
    #12
    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. Èý¹â
    Replies:
    1
    Views:
    598
    William F. Robertson, Jr.
    Jul 29, 2003
  2. Replies:
    0
    Views:
    5,164
  3. Tjerk Wolterink
    Replies:
    3
    Views:
    479
    Marrow
    Oct 22, 2004
  4. Petter Reinholdtsen
    Replies:
    9
    Views:
    4,430
    Howard
    Nov 29, 2004
  5. ichi
    Replies:
    0
    Views:
    321
Loading...

Share This Page