473,394 Members | 1,751 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 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 3556
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Bernd Fuhrmann | last post by:
Hi! I have some trouble with some simple stupid XSLT-stuff. My stylesheet: ------------- <?xml version="1.0" encoding="iso-8859-1"?> <xsl:stylesheet version="1.0"...
0
by: Mads Orbesen Troest | last post by:
Hi, If I have an XSLT sheet transforming elemens from the "myns" namespace into xhtml, the "myns" namespace is declared automatically in the output xhtml, even if not used. I can't seem to...
1
by: Christian Hoertnagl | last post by:
I have to create an XML document with repeating namespace definitions such as: <a xmlns:ns1="..."> <!-- ... --> <b xmlns:ns1="..."> <!-- ... --> </b> </a>
1
by: Howard | last post by:
Hi, I am using a very simple xslt file to get info from an xml document. The problem seems to me to be that the xml doc uses a namespace, and I don't know how to set up my xslt to recognize it...
2
by: Manoj G | last post by:
Hello, I believe there is no way to remove the default namespace declaration (For eg <DataSet xmlns="something">.... ) on an XmlNode object directly through DOM. So, what is the best way to...
2
by: Rick | last post by:
I have an XML document that is generated from Infopath, I need to change the value of a namespace that is defined in a node in the form: <xsf:xDocumentClass "xmlns:my=valuehere">. when i navigate...
7
by: Simon Hart | last post by:
Hi, I have a requirement to remove the xmlns from the DOM in order to pass over to MS CRM 3.0 Fetch method.It seems the fetch method blows up if there is a xmlns present!?! The reason I have a...
4
by: Samuel R. Neff | last post by:
I'm writing an xslt in vs.net 2003 and in order to get intellisense on the html content I added the default namespace declaration xmlns="http://schemas.microsoft.com/intellisense/ie5". However,...
6
by: Olagato | last post by:
I need to transform this: <urlset xmlns="http://www.google.com/schemas/sitemap/0.84"> <url> <loc>http://localhost/index.php/index./Paths-for-the-extreme-player</ loc> </url> <url>...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.