removing spurious namespace declarations on XSLT output

Discussion in 'XML' started by Andy Fish, Apr 30, 2007.

  1. Andy Fish

    Andy Fish Guest

    hi,

    I have an XSLT which is producing XML output.

    many of the nodes in the output tree contain namespace declarations for
    namespaces that are used in the source document even though they are not
    used in the result document or the stylesheet

    also I find that (for namespaces that are referenced in the stylesheet) even
    if I put an explicit namespace declaration on the root element of the result
    tree, namespaces declarations are repeated on individual nodes of the result
    tree but with a different prefix

    all I want is that for namespaces used in the result tree, they are declared
    on the root element, and other namespaces shouldn't be output at all. why is
    this so difficult to achieve or am I missing something big here?

    TIA

    Andy
     
    Andy Fish, Apr 30, 2007
    #1
    1. Advertising

  2. Andy Fish

    Andy Fish Guest

    typical - after tearing my hair out on it for hours, I made some progress
    just after posting the question

    it appears the spurious namespace declarations are due to me using
    <xsl:copy> which copies all namespace declarations from the source node,
    regardless of whether they are used

    so my new question is, how can I replicate the behaviour of <xsl:copy>
    without copying all the namespace declarations?

    Andy


    "Andy Fish" <> wrote in message
    news:1kjZh.9063$...
    > hi,
    >
    > I have an XSLT which is producing XML output.
    >
    > many of the nodes in the output tree contain namespace declarations for
    > namespaces that are used in the source document even though they are not
    > used in the result document or the stylesheet
    >
    > also I find that (for namespaces that are referenced in the stylesheet)
    > even if I put an explicit namespace declaration on the root element of the
    > result tree, namespaces declarations are repeated on individual nodes of
    > the result tree but with a different prefix
    >
    > all I want is that for namespaces used in the result tree, they are
    > declared on the root element, and other namespaces shouldn't be output at
    > all. why is this so difficult to achieve or am I missing something big
    > here?
    >
    > TIA
    >
    > Andy
    >
    >
     
    Andy Fish, Apr 30, 2007
    #2
    1. Advertising

  3. In article <ntjZh.9069$>,
    Andy Fish <> wrote:

    >it appears the spurious namespace declarations are due to me using
    ><xsl:copy> which copies all namespace declarations from the source node,
    >regardless of whether they are used
    >
    >so my new question is, how can I replicate the behaviour of <xsl:copy>
    >without copying all the namespace declarations?


    You could copy elements by creating new elements:

    <xsl:element name="local-name()" namespace="namespace-uri()">
    <xsl:copy-of select="@*"/>
    ...
    </xsl:element>

    -- Richard
    --
    "Consideration shall be given to the need for as many as 32 characters
    in some alphabets" - X3.4, 1963.
     
    Richard Tobin, Apr 30, 2007
    #3
  4. Andy Fish wrote:

    > I have an XSLT which is producing XML output.
    >
    > many of the nodes in the output tree contain namespace declarations for
    > namespaces that are used in the source document even though they are not
    > used in the result document or the stylesheet


    Are you using exclude-result-prefixes on the xsl:stylesheet element
    <http://www.w3.org/TR/xslt#stylesheet-element>
    That way you should get rid of namespace declarations for namespaces
    that are not used in the result document.

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Apr 30, 2007
    #4
  5. In article <4635d5c5$0$20290$-online.net>,
    Martin Honnen <> wrote:

    >> many of the nodes in the output tree contain namespace declarations for
    >> namespaces that are used in the source document even though they are not
    >> used in the result document or the stylesheet


    >Are you using exclude-result-prefixes on the xsl:stylesheet element
    > <http://www.w3.org/TR/xslt#stylesheet-element>
    >That way you should get rid of namespace declarations for namespaces
    >that are not used in the result document.


    That only suppresses namespaces appearing in the stylesheet, not ones
    copied from the source document.

    -- Richard
    --
    "Consideration shall be given to the need for as many as 32 characters
    in some alphabets" - X3.4, 1963.
     
    Richard Tobin, Apr 30, 2007
    #5
  6. Andy Fish

    Andy Fish Guest

    thanks richard

    I have used something similar to this technique. unfortunately yours creates
    a namespace node on the copied node of the result tree and I wanted to just
    use the prefix.

    I tried this

    <xsl:element name="{name()}" >

    but it fails if the namespace prefix in the source document is different to
    that used in the stylesheet, so I ended up with this instead

    <xsl:template match="foo:*">
    <xsl:element name="foo:{local-name()}" >
    <xsl:for-each select="@*">
    <xsl:attribute name="foo:{local-name()}">
    <xsl:value-of select="." />
    </xsl:attribute>
    </xsl:for-each>
    </xsl:element>
    </xsl:template>


    "Richard Tobin" <> wrote in message
    news:f14j26$1bvn$...
    > In article <ntjZh.9069$>,
    > Andy Fish <> wrote:
    >
    >>it appears the spurious namespace declarations are due to me using
    >><xsl:copy> which copies all namespace declarations from the source node,
    >>regardless of whether they are used
    >>
    >>so my new question is, how can I replicate the behaviour of <xsl:copy>
    >>without copying all the namespace declarations?

    >
    > You could copy elements by creating new elements:
    >
    > <xsl:element name="local-name()" namespace="namespace-uri()">
    > <xsl:copy-of select="@*"/>
    > ...
    > </xsl:element>
    >
    > -- Richard
    > --
    > "Consideration shall be given to the need for as many as 32 characters
    > in some alphabets" - X3.4, 1963.
     
    Andy Fish, Apr 30, 2007
    #6
  7. Andy Fish wrote:
    > I have used something similar to this technique. unfortunately yours creates
    > a namespace node on the copied node of the result tree and I wanted to just
    > use the prefix.


    XSLT is namespace-aware. When you copy a node, you copy it with its
    namespace context, because that's part of the meaning of the node.

    If you really want to break namespace-aware behavior, then yes,
    hand-constructing a new node is one solution. But I strongly suspect
    that what you're really looking for is
    http://www.w3.org/TR/1999/REC-xslt-19991116#element-namespace-alias


    --
    () ASCII Ribbon Campaign | Joe Kesselman
    /\ Stamp out HTML e-mail! | System architexture and kinetic poetry
     
    Joe Kesselman, Apr 30, 2007
    #7
  8. Andy Fish wrote:
    > it appears the spurious namespace declarations are due to me using
    > <xsl:copy> which copies all namespace declarations from the source node,
    > regardless of whether they are used


    BTW, the reason it does this is because there's no good way for XSLT to
    be sure whether those declarations are or aren't used. Consider, for
    example, a document which contains an XPath; that path is often
    dependant upon prefixes in scope around it, but there's no guaranteed
    way for XSLT to determine that those dependencies exist. Much, much,
    MUCH safer to copy all the declarations (which should be harmless at
    worst if they aren't used).

    The only argument for not copying them is that DTDs have trouble with
    unexpected namespace declarations. But DTDs really don't play nicely
    with namespaces anyway, which is why I keep telling folks to switch to
    schemas.

    --
    () ASCII Ribbon Campaign | Joe Kesselman
    /\ Stamp out HTML e-mail! | System architexture and kinetic poetry
     
    Joe Kesselman, Apr 30, 2007
    #8
  9. Andy Fish

    Andy Fish Guest

    the issue is that I don't know what namespace prefix might have been used in
    the source document. in the output document I want to use the same namespace
    I'm using in the stylesheet

    I did investigate <xsl:namespace-alias> but I can't see how this would help.


    "Joe Kesselman" <> wrote in message
    news:...
    > Andy Fish wrote:
    >> I have used something similar to this technique. unfortunately yours
    >> creates a namespace node on the copied node of the result tree and I
    >> wanted to just use the prefix.

    >
    > XSLT is namespace-aware. When you copy a node, you copy it with its
    > namespace context, because that's part of the meaning of the node.
    >
    > If you really want to break namespace-aware behavior, then yes,
    > hand-constructing a new node is one solution. But I strongly suspect that
    > what you're really looking for is
    > http://www.w3.org/TR/1999/REC-xslt-19991116#element-namespace-alias
    >
    >
    > --
    > () ASCII Ribbon Campaign | Joe Kesselman
    > /\ Stamp out HTML e-mail! | System architexture and kinetic poetry
     
    Andy Fish, May 4, 2007
    #9
  10. Andy Fish wrote:
    > the issue is that I don't know what namespace prefix might have been used in
    > the source document. in the output document I want to use the same namespace
    > I'm using in the stylesheet


    If you copy a node from the stylesheet to the output, it will have the
    same namespace, and namespace bindings, as in the stylesheet.

    If you copy a node from a source document to the output, it will have
    the same namespace, and namespace bindings, as in the source document.

    Appropriate declarations will be generated to make it so, if the
    relevant binding from prefix to namespace URI isn't already in scope at
    that point in the output. They aren't "spurious"; they're required to
    achieve the above semantic results.


    If you're having trouble, it's because you either aren't trusting
    xsl:copy to do the right thing, or because you're trying to override
    those semantics, or because you're not using namespaces properly... or
    because you're worried about validating against a DTD which doesn't
    allow the relevant namespace declarations at that point in the document.
    If it's the last, the best advice I can give you is to abandon DTDs; the
    namespace spec deliberately chose not to worry about "playing nice" with
    DTDs, and its authors advised everyone to switch to XML Schema -- which,
    unlike DTDs, is fully namespace-aware.



    It might help if you gave us a more explicit example of what you're
    trying to do. You've given us a request out of context, and a fragment
    of code that attempts to meet that request... but both presuppose a
    particular solution, and it may not be the right solution. Consider
    posting a minimal example that demonstrates what you think is a problem,
    and explaining exactly why you consider it a problem. You'll get better
    answers if you ask the real question rather than asking about a detail.



    --
    () ASCII Ribbon Campaign | Joe Kesselman
    /\ Stamp out HTML e-mail! | System architexture and kinetic poetry
     
    Joe Kesselman, May 4, 2007
    #10
  11. Andy Fish wrote:
    > the issue is that I don't know what namespace prefix might have been used in
    > the source document.


    Don't think in terms of prefixes. A prefix is just shorthand for a
    namespace URI, and nothing namespace-aware should care which prefix is
    used, only which namespace.

    --
    Joe Kesselman / Beware the fury of a patient man. -- John Dryden
     
    Joseph Kesselman, May 4, 2007
    #11
    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. Mads Orbesen Troest

    Namespace declarations appearing in output.

    Mads Orbesen Troest, Apr 19, 2004, in forum: XML
    Replies:
    0
    Views:
    463
    Mads Orbesen Troest
    Apr 19, 2004
  2. Christian Hoertnagl
    Replies:
    1
    Views:
    389
    Martin Honnen
    May 22, 2004
  3. Keith Davies
    Replies:
    2
    Views:
    346
    Keith Davies
    Aug 15, 2004
  4. cmay
    Replies:
    11
    Views:
    2,359
    George Bina
    Aug 2, 2006
  5. Chris  Chiasson
    Replies:
    6
    Views:
    635
    Richard Tobin
    Nov 14, 2006
Loading...

Share This Page