Script to identify unused elements between XSLT and XML data source

Given somefile.xslt, which calls the datafile.xml file, is there a script that will output the report from node in the datafile.xml file that somefile.xslt is not called?

Obviously, a visual check of each file can be used as a basis for analysis, but I'm looking for an automatic method.

For example, my xslt contains xpath as:

<xsl:for-each select="//somenode/somesubnode/@attribute">

And it is expected that the xml data source will contain somenode / somesubnode data structure . However, if it contains a someothernode data structure that is not the root element or child of the xpath invoked in XSLT, it must be part of the unused nodes report.

+3
source share
1 answer

If you use the push ( xsl:apply-templates) approach instead of the pull ( xsl:for-each) approach , you can have a negative priority template that "catches" any elements that don't match another template. This is more of an "unsurpassed" test than a "unused" test.

Basic example ...

XML input

<doc>
    <foo>
        <bar>bar text</bar>
    </foo>
    <foo2>
        <bar>more bar text</bar>
    </foo2>
</doc>

XSLT 1.0

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="@*|node()" name="ident" priority="-1">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="doc|foo|bar">
        <xsl:call-template name="ident"/>
    </xsl:template>

    <xsl:template match="*">
        <xsl:processing-instruction name="unused-element">
            <xsl:value-of select="name()"/>
        </xsl:processing-instruction>
        <xsl:call-template name="ident"/>
    </xsl:template>

</xsl:stylesheet>

XML output

<doc>
   <foo>
      <bar>bar text</bar>
   </foo>
   <?unused-element foo2?><foo2>
      <bar>more bar text</bar>
   </foo2>
</doc>
+1
source

All Articles