Wednesday, 25 June 2014

Find Duplicate IDs with XSLT

This little snippet of XSLT is a useful tool to find all duplicate ids within an XML source document and generate a report with the count of duplicates and xpath to each element that has a duplciate id attribute.

This relies upon the attribute in question being names @id but it is simple enough to change this to whatever attribute you need to interrogate

Note that this is XSLT 2 and has been used with the Saxon transformation engine

<xsl:stylesheet version="2.0" 

  <xsl:output indent="yes"/>
  <xsl:key name="ids" match="*[@id]" use="@id"/> 

  <xsl:template match="/">
      <xsl:apply-templates select="//*[@id]"/>

  <xsl:template match="*[@id]">
    <xsl:if test="count(key('ids', @id)) &gt; 1">
        dup-count="{count(key('ids', @id))}" 
        node-xpath="{string-join((for $node in ancestor::* return concat($node/name(),'[', count($node/preceding-sibling::*[name() = $node/name()])+1, ']'),concat(name(),'[', count(preceding-sibling::*[name() = current()/name()]) + 1, ']')


No comments:

Post a Comment