Prevent xmlns decalaration in XSLT processing

C

cyclops

I'm trying to do XML + XSLT -> Another XML. The source XML contains
multiple namespaces and XSLT will handle all possible tags under each
name space.

----source----
<document xmlns="..." xmlns:a="..." xmlns:b="">
....

----XSLT----
....
<xsl:template match="a:sometag">
....
</xsl:template>
<xsl:template match="b:sometag" />
<xsl:template match="defaultNsInSource:sometag" />
....

The problem I met is the generated xml document always contains
namespace declarations for all name spaces in source xml.

I've tried to add exclude-result-prefixes="yes" or
exclude-result-prefixes="a" in XSLT <xsl:stylesheet> tag, the later one
successfully prevent namespace "a" decalaration from generating, while
the others are still there. Even though there aren't tags under these
name spaces in result xml.

So how to prevent all namespace decalarations from generating into the
result xml?



Thanks
 
J

Joris Gillis

Tempore 17:50:37 said:
I've tried to add exclude-result-prefixes="yes" or
exclude-result-prefixes="a" in XSLT <xsl:stylesheet> tag, the later one
successfully prevent namespace "a" decalaration from generating, while
the others are still there. Even though there aren't tags under these
name spaces in result xml.

So how to prevent all namespace decalarations from generating into the
result xml?

Hi,

The value of the 'exclude-result-prefixes' attribute is a whitespace seperated list of Qnames. SO in order to prevent namespaces 'a' and 'b' to be added to the result tree, use: exclude-result-prefixes="a b"


regards,
 
D

David Carlisle

cyclops said:
I'm trying to do XML + XSLT -> Another XML. The source XML contains
multiple namespaces and XSLT will handle all possible tags under each
name space.

----source----
<document xmlns="..." xmlns:a="..." xmlns:b="">
...

----XSLT----
...
<xsl:template match="a:sometag">
...
</xsl:template>
<xsl:template match="b:sometag" />
<xsl:template match="defaultNsInSource:sometag" />
...

The problem I met is the generated xml document always contains
namespace declarations for all name spaces in source xml.

I've tried to add exclude-result-prefixes="yes" or
exclude-result-prefixes="a" in XSLT <xsl:stylesheet> tag, the later one
successfully prevent namespace "a" decalaration from generating, while
the others are still there. Even though there aren't tags under these
name spaces in result xml.

So how to prevent all namespace decalarations from generating into the
result xml?



Thanks

exclude-result-prefixes only affects namespaces that are used on literal
result elements in the stylesheet it has no effect on nodes copied from
the source.

The namespace nodes are being copied from your source tree, this will
not happen by default (only text nodes are copied by default) so it is a
template that you have not shown that is copying the nodes.
if you copy an element node then you always get all its namespaces, if
you don't want that, don't copy it, ie don't use xsl:copy or xsl:copy-of
use <xsl:element name="{name()}"> instead as this generates a fresh
element node with only the namespace nodes for its own namespace (if it
has one) and the xml namespace.

David
 
M

Martin Honnen

cyclops wrote:

So how to prevent all namespace decalarations from generating into the
result xml?

List all the prefixes of namespaces you want to exclude e.g.
<xsl:stylesheet
exclude-result-prefixes="a b c"
 
C

cyclops

This works fine for me now.
Once there are more and more name spaces, David's solution
(<xsl:element> tag) might be a better one.


Thank you all for quick response
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,244
Members
46,838
Latest member
KandiceChi

Latest Threads

Top