Solution for avoiding duplicates?

B

Brian J. Sayatovic

I think I've come up with a solution for avoid duplicates in a
nodeset. The sticky part was that with a nodeset, you can't use axes
(such as preceding-sibling) because those only refer to the source
tree. However, I was able to exploit the fact that generate-id
returns a unique id.

Consider the following XML document:

<?xml version="1.0" encoding="utf-8"?>
<root>

<RiskCodes id="1">
<RiskCode id="1.1">A</RiskCode>
<RiskCode id="1.2">B</RiskCode>
<RiskCode id="1.3">A</RiskCode>
</RiskCodes>
<RiskCodes id="2">
<RiskCode id="2.1">A</RiskCode>
<RiskCode id="2.2">C</RiskCode>
</RiskCodes>

</root>

And here is the stylesheet I use to print out non-duplicate RiskCodes:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xalan="http://xml.apache.org/xslt">
<xsl:eek:utput method="xml" indent="yes" xalan:indent-amount="3"/>

<xsl:template match="/">
<xsl:variable name="vNodes" select="/root/RiskCodes"/>

<xsl:for-each select="$vNodes/RiskCode">
<xsl:variable name="vNode" select="."/>

<xsl:if test="generate-id($vNode)=generate-id($vNodes/RiskCode[.=$vNode][1])">
<xsl:copy-of select="$vNode"/>
</xsl:if>

</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

The resulting output is shown here:

<?xml version="1.0" encoding="UTF-8"?>
<RiskCode id="1.1">A</RiskCode>
<RiskCode id="1.2">B</RiskCode>
<RiskCode id="2.2">C</RiskCode>

I'm posting this because when I searched for a soution online, I found
none. Hopefully this will help others. Mind you, this is probably
pretty slow. If anyone has any better ideas, I'd be glad to hear
them.

Regards,
Brian.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,828
Latest member
LauraCastr

Latest Threads

Top