Here's how to find all namespace bindings in an xml document with XSLT.
The following XSLT stylesheet:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
exclude-result-prefixes="msxsl"
>
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:key name="kNS" match="namespace"
use="
"/>
<xsl:template match="/">
<xsl:variable name="vrtfNamespaces">
<xsl:for-each select="//namespace::*">
<namespace pref="{name()}" uri="{.}"/>
</xsl:for-each>
</xsl:variable>
<xsl:for-each select="msxsl:node-set($vrtfNamespaces)">
<xsl:for-each select=
"namespace[generate-id()
=
generate-id(key('kNS',
)[1]
)
]"
>
<xsl:copy-of select="
"/>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
when applied against this xml document
< xml version="1.0" >
<!-- This is a sample XML document -->
< x PI="5" >
<!DOCTYPE Items [
<!ENTITY number "123">
]>
<ttt:Items xmlns="sfsfsf" xmlns:ttt="ddzdzdf">
<Item a1 = "1" a2="2" ax="x">Test with an entity:
&number;</Item>
&number;
<Item>
test with a child element <more/> stuff
</Item>A
<Item a3 = "3" /><Item a4 = "4" />
<x/>
<Item>test with a CDATA section <![CDATA[<456>]]> def</Item>
<Item>Test with an char entity: A</Item>
<!-- Four**** chars in this element.-->
<Item>1234567890ABCD</Item>
</ttt:Items>
< x PI="5" >
<!-- This is the end of the sample XML document -->
< x PI="5" >
produces the wanted result:
<
namespace pref="" uri="sfsfsf" />
<namespace pref="ttt" uri="ddzdzdf" />
Cheers,
Dimitre Novatchev