472,100 Members | 2,200 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,100 software developers and data experts.

removing spurious namespace declarations on XSLT output

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
Apr 30 '07 #1
10 3452
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:copywhich 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" <aj****@blueyonder.co.ukwrote in message
news:1k*****************@text.news.blueyonder.co.u k...
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


Apr 30 '07 #2
In article <nt****************@text.news.blueyonder.co.uk>,
Andy Fish <aj****@blueyonder.co.ukwrote:
>it appears the spurious namespace declarations are due to me using
<xsl:copywhich 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.
Apr 30 '07 #3
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/
Apr 30 '07 #4
In article <46***********************@newsspool3.arcor-online.net>,
Martin Honnen <Ma***********@gmx.dewrote:
>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.
Apr 30 '07 #5
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" <ri*****@cogsci.ed.ac.ukwrote in message
news:f1***********@pc-news.cogsci.ed.ac.uk...
In article <nt****************@text.news.blueyonder.co.uk>,
Andy Fish <aj****@blueyonder.co.ukwrote:
>>it appears the spurious namespace declarations are due to me using
<xsl:copywhich 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.

Apr 30 '07 #6
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-1...amespace-alias
--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
Apr 30 '07 #7
Andy Fish wrote:
it appears the spurious namespace declarations are due to me using
<xsl:copywhich 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
Apr 30 '07 #8
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-aliasbut I can't see how this would help.
"Joe Kesselman" <ke************@comcast.netwrote in message
news:kd******************************@comcast.com. ..
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-1...amespace-alias
--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry

May 4 '07 #9
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
May 4 '07 #10
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
May 4 '07 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

20 posts views Thread by Bernd Fuhrmann | last post: by
reply views Thread by Mads Orbesen Troest | last post: by
1 post views Thread by Christian Hoertnagl | last post: by
1 post views Thread by Howard | last post: by
2 posts views Thread by Manoj G | last post: by
7 posts views Thread by Simon Hart | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.