Project

Profile

Help

Bug #2929 » namespace-unused.xsl

Thomas Åkesson, 2016-09-09 20:05

 
<?xml version="1.0" encoding="UTF-8"?>
<!--

Copyright (C) 2009-2013 Simonsoft Nordic AB

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-->
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:cms="http://www.simonsoft.se/namespace/cms"
>


<!-- Experimental support for determining which declared namespaces are not actually used. -->
<xsl:key name="ns_elem" match="*[namespace-uri() != '']" use="namespace-uri()"/>
<xsl:key name="ns_attr" match="*/@*[namespace-uri() != '']" use="namespace-uri()"/>

<xsl:template match="*" mode="#default">
<xsl:element name="doc">
<xsl:apply-templates select="." mode="i-ns-unused"/>
</xsl:element>
</xsl:template>
<!-- Experimental support for determining which declared namespaces are not actually used. -->
<!-- Investigates all namespaces declared on parent, which means they are inherited by this element. -->
<xsl:template match="*" mode="i-ns-unused">
<xsl:variable name="namespace-parent" select="parent::node()/namespace::*[string() != 'http://www.w3.org/XML/1998/namespace']"/>
<xsl:for-each select="$namespace-parent">
<xsl:variable name="nsuri" select="."></xsl:variable>
<xsl:if test="count(key('ns_attr', $nsuri)) = 0 and count(key('ns_elem', $nsuri)) = 0">
<xsl:value-of select="$nsuri"></xsl:value-of>
<xsl:value-of select="' '"></xsl:value-of>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
(5-5/5)