[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

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


Subject: RE: [xsl] XSLT and XPath 2.0: finding unused namespace declarations
From: "Michael Kay" <mhk@xxxxxxxxx>
Date: Fri, 10 Oct 2003 19:54:54 +0100

I'm not exactly sure what you mean by "used" and "unused": is this a
property of the namespace prefix, or the namespace URI, or the
combination of both? And does "used" mean used in an element name, or do
you also need to look at attribute names?

I'm assuming you're trying to do this without using namespace nodes, as
they are deprecated.

Let's assume that a namespace URI is "used" if an element or attribute
is in that namespace:

<xsl:function name="is-used" as="xs:boolean">
  <xsl:param name="doc"/>
  <xsl:param name="uri" as="xs:string"/>
  <xsl:sequence select="some $n in $doc//*/(.|@*) 
                 satisfies namespace-uri($n) eq $uri"/>
</xsl:function>

Now the full set of prefix=uri pairs declared in the document is:

<xsl:variable name="doc" select="/"/>

<xsl:variable name="all-pairs" select="
  for $e in $doc//*, $p in get-in-scope-prefixes($e)
  return concat($p, '=', get-namespace-uri-for-prefix($e, $p))"/>

And those that are unique are:

<xsl:variable name="distinct-pairs" select="
  distinct-values($all-pairs)"/>

So the output is:

<xsl:for-each select="$distinct-pairs">
  <xsl:value-of select="., '(', is-used($doc, substring-after(., '=')),
')'"/>
</xsl:for-each>

Michael Kay 

> -----Original Message-----
> From: owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx 
> [mailto:owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx] On Behalf Of 
> Timothy M. Lebo
> Sent: 10 October 2003 18:27
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] XSLT and XPath 2.0: finding unused namespace 
> declarations
> 
> 
> 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
> 


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



Current Thread
Keywords