Another without extension functions:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="*">
<xsl:param name="pNamespaces" select="'
'"/>
<xsl:variable name="vNamespaces">
<xsl:variable name="vMyNamespaces">
<xsl:value-of select="$pNamespaces"/>
<xsl:for-each select="namespace::*
[not(contains(
$pNamespaces,
concat('
',.,'
')))]">
<xsl:value-of select="concat(.,'
')"/>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="vChildsNamespaces">
<xsl:apply-templates select="*[1]">
<xsl:with-param name="pNamespaces"
select="$vMyNamespaces"/>
</xsl:apply-templates>
</xsl:variable>
<xsl:value-of select="concat(substring($vMyNamespaces,
1 div not(*)),
substring($vChildsNamespaces,
1 div boolean(*)))"/>
</xsl:variable>
<xsl:variable name="vFollowNamespaces">
<xsl:apply-templates select="following-sibling::*[1]">
<xsl:with-param name="pNamespaces" select="$vNamespaces"/>
</xsl:apply-templates>
</xsl:variable>
<xsl:value-of
select="concat(substring($vNamespaces,
1 div not(following-sibling::*)),
substring($vFollowNamespaces,
1 div boolean(following-sibling::*)))"/>
</xsl:template>
</xsl:stylesheet>
Output (With Dimitre's input sample):
http://www.w3.org/XML/1998/namespace
mynamespace
mynamespace2
mynamespace3
EDIT: Also this XPath expression:
//*/namespace::*[not(. = ../../namespace::*|preceding::*/namespace::*)]
As proof, this stylesheet:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:for-each select="//*/namespace::*
[not(. = ../../namespace::*|
preceding::*/namespace::*)]">
<xsl:value-of select="concat(.,'
')"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Output:
http://www.w3.org/XML/1998/namespace
mynamespace
mynamespace2
mynamespace3
EDIT 4: Same efficient as two pass transformation.
This stylesheet:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:key name="kElemByNSURI"
match="*[namespace::*[not(. = ../../namespace::*)]]"
use="namespace::*[not(. = ../../namespace::*)]"/>
<xsl:template match="/">
<xsl:for-each select=
"//namespace::*[not(. = ../../namespace::*)]
[count(..|key('kElemByNSURI',.)[1])=1]">
<xsl:value-of select="concat(.,'
')"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Output:
http://www.w3.org/XML/1998/namespace
mynamespace
mynamespace2
mynamespace3
EDIT 5: When you are dealing with a XSLT processor without namespace
axe implementation (Like TransforMiix), you can only extract namespaces actually used with this stylesheet:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:key name="kElemByNSURI" match="*|@*" use="namespace-uri()"/>
<xsl:template match="/">
<xsl:for-each select=
"(//*|//@*)[namespace-uri()!='']
[count(.|key('kElemByNSURI',namespace-uri())[1])=1]">
<xsl:value-of select="concat(namespace-uri(),'
')"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
TransforMiix output:
mynamespace2