welcome to the world of…

Looks like a small bulb used to indicate something unusual, like a malfunction.

Filtering duplicities in XSLT

Filed under: Uncategorized — Tags: , , , — admin @ 2009-04-29 13:27

I had a problem: I needed to find some tags in a subtree without duplicities. The duplicity was determined by a tag’s attribute value. XPath has a function distinct-values but it doesn’t work in xsltproc nor 4xslt. I found a few forums where the problem was discussed. The common solution is to use <xsl:key> tag and functions key() and generate-id() (e.g. here). However this works only for an in advance defined subtree. I didn’t find any constraint for the <xsl:key> tag to focus only to a nonparticular subtree.

I needed to work with tags <cost> with an unique attribute role in a current section. My solution works with a combination of <xsl:for-each> and <xsl:if>:

<xsl:variable name="cursection" select="."/>
<xsl:for-each select=".//cost"> <!-- get all of those -->
   <xsl:variable name="role" select="@role"/>
   <!-- id(current cost) == id(first cost in the section with the given role) -->
   <xsl:if test="generate-id()=generate-id($cursection//cost[@role=$role][1])">
     ....
   </xsl:if>
</xsl:for-each>

No Comments

No comments yet.

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.