RE: [xsl] Generate a list of declared namespaces

Subject: RE: [xsl] Generate a list of declared namespaces
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Thu, 12 Nov 2009 09:05:05 -0000
This works, but is likely to be horribly inefficient: if you have 10,000
elements, and declare 10 namespaces on the outermost element, then the
document will contain 100,000 namespace nodes, and you are then doing
10,000,000,000 comparisons to eliminate the duplicates. (Moreover, you
aren't just accessing the nodes, you are creating them: because namespace
nodes represent information in a highly redundant way, it's likely that most
implementations will only create them when they are referenced.)

I would suggest using keys: but you can't, because there is no XSLT match
pattern that matches a namespace node. In 2.0, using xsl:for-each-group() or
distinct-values() to eliminate the duplicates is likely to work much better.

Something like:

<xsl:function name="f:namespace-pair">
  <xsl:param name="e" as="node()"/> <!-- a namespace node -->
  <xsl:sequence select="concat($e/name(), '=', $e/string())"/>
</xsl:function>

and then the result is

distinct-values(//*/namespace::*/f:namespace-pair(.))

A decent implementation will stream this so it never allocates the whole
list of namespace nodes in memory.

Regards,

Michael Kay
http://www.saxonica.com/
http://twitter.com/michaelhkay 


> >
> >Use the namespace axis.  You have to search the entire 
> document in case 
> >you have namespaces declared below the document element.
> >
> >I hope the example below helps.
> >
> >. . . . . . . Ken
> 
> I had second thoughts about my first answer, which was 
> looking for uniqueness in the name of the prefix, when it 
> should have been looking for uniqueness in the namespace URI.

Current Thread