[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
Hello,

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:
<root
  xmlns:first="first.com"
  xmlns:second="second.com"
  xmlns:third="third.com">

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

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.

Regards,

Timothy M. Lebo
Research Associate
Smart Information Flow Technologies

<xsl:stylesheet version="2.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
>
  <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"
select="get-namespace-from-QName(node-name($me))"/>
      <xsl:variable name="my_inScope_NS"
select="get-in-scope-namespaces(.)"/>
      <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('             ',.,' =
',get-namespace-uri-for-prefix($me,.))"/>
        </xsl:if>
      </xsl:for-each>
      <xsl:if test="some $x in $my_inScope_NS satisfies $x =
get-namespace-uri-for-prefix($me,$x)">
	<xsl:value-of select="concat('  (used)',$newline)"/>
      </xsl:if>
      <xsl:if test="every $x in $my_inScope_NS satisfies not($x =
get-namespace-uri-for-prefix($me,$x))">
	<xsl:value-of select="concat('  (unused)',$newline)"/>
      </xsl:if>
    </xsl:for-each-group>
  </xsl:template>

<xsl:variable name="newline">
<xsl:text>
</xsl:text>
</xsl:variable>

</xsl:stylesheet>


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


Current Thread