In this case, the best fit was to add a new check to the QA plugin.
The check should:
• Identify duplicate @id values on specific elements
• Return only distinct values (i.e. if 123 appears several times, then return 123 only once)
After some forum research, my first thought was to use a key match.
<xsl:key name="duplicateIds" match="elementName" use="@id" />
And reference it with
key('duplicateIds',@id)[2] and count(.|key('duplicateIds', @id)[1]) = 1
So the if statement for the check looked like
<xsl:for-each select="//parentElement"> <xsl:if test="key('duplicateIds',@id)[2] and count(.|key('duplicateIds', @id)[1]) = 1"> Remove duplicate elementName @id=<xsl:value-of select="@id"/> </xsl:if> </xsl:for-each>
Even though it met my requirements, it’s still an outdated approach that didn’t leave me with a job-well-done sense of completion. I wanted a cleaner solution that fit somewhere closer to the 2.0 realm.
I decided to use grouping to identify each distinct @id value. Then I could wrap an if statement to test for any groups that contained a second (duplicate) item. The result:
<xsl:for-each-group select="//elementName" group-by="@id"> <xsl:if test="current-group()[2]"> Remove duplicate elementName @id=<xsl:value-of select="@id"/> </xsl:if> </xsl:for-each-group>
Have a better approach? Leave a comment.
Very nice. I’m hoping to replace my old PowerShell script with the QA plugin and this was one capability I hadn’t figured out.
Worked beautifully! Thank you!