[xsl] XSLT and XPath 2.0: finding unused namespace declarations

Subject: [xsl] XSLT and XPath 2.0: finding unused namespace declarations
From: "Timothy M. Lebo" <tlebo@xxxxxxxxx>
Date: Fri, 10 Oct 2003 12:26:51 -0500

As an exercise for adapting to the changes in XSLT and Xpath 2.0, I'm
trying to write a stylesheet to report all namespaces that are in scope
anywhere in the document, what their NCNames and expansions are, and the
status of their usage.

For Example, with an input document:

  <first:child xmlns:fourth="fourth.com">
    <second:grandchild xmlns:fifth="fifth.com/>

The stylesheet would return something like:
Xml = w3.org/XML/98/namespace (unused)
first = first.com (used)
second = second.com (used)
third = third.com (unused)
fourth = fourth.com (unused)
fifth = fifth.com (unused)

I continue to run into problems with datatypes and sequences. What is
the best way to mold one's understanding of node sets to understand how
to manipulating sequences? It would seem to me if I had a set of
in-scope nodes and I knew the namespace being used, it is a matter of
set containment.

Here is the code, which I am very ashamed to distribute. It falls short
in many respects, including style. I would much prefer to expunge the
for loops.


Timothy M. Lebo
Research Associate
Smart Information Flow Technologies

<xsl:stylesheet version="2.0"
  <xsl:output method="text"/>

  <xsl:template match="/">
    <xsl:value-of select="concat(base-uri(.),$newline)"/>
    <xsl:value-of select="concat('    declares: ',$newline,'
    <xsl:value-of select="get-in-scope-namespaces(*[1])"/>
    <xsl:value-of select="concat($newline,'    usage    : ',$newline)"/>

    <xsl:for-each-group select="//*" group-by="namespace-uri()">
      <xsl:variable name="me" select="."/>
      <xsl:variable name="myNS"
      <xsl:variable name="my_inScope_NS"
      <xsl:for-each select="get-in-scope-namespaces(.)">
        <xsl:sort select="."/>
        <xsl:if test="get-namespace-uri-for-prefix($me,.) = $myNS">
          <xsl:value-of select="concat('             ',.,' =
      <xsl:if test="some $x in $my_inScope_NS satisfies $x =
	<xsl:value-of select="concat('  (used)',$newline)"/>
      <xsl:if test="every $x in $my_inScope_NS satisfies not($x =
	<xsl:value-of select="concat('  (unused)',$newline)"/>

<xsl:variable name="newline">


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list

Current Thread