Specifying condition with count function
-
Hi I am writing a code generation app using xslt. My purpose is to check if any two nodes of the same parent node, have the same attribute value. If so I want to append a number sequence in the ouput code, so that there is no duplication. For example ------------- if xml file is like <code> <methods> <method name="first"> <method name="first"> <method name="second"> <method name="third"> </methods></code> I want the output to be ------------- void first1() {} void first2() {} void second() {} void third() {} Note: there is no number sequence attached to function name if it is not duplicated. How to write XSLT to achieve this? Thank you Fadi
-
Hi I am writing a code generation app using xslt. My purpose is to check if any two nodes of the same parent node, have the same attribute value. If so I want to append a number sequence in the ouput code, so that there is no duplication. For example ------------- if xml file is like <code> <methods> <method name="first"> <method name="first"> <method name="second"> <method name="third"> </methods></code> I want the output to be ------------- void first1() {} void first2() {} void second() {} void third() {} Note: there is no number sequence attached to function name if it is not duplicated. How to write XSLT to achieve this? Thank you Fadi
You want to use the 'preceding-sibling' and 'following-sibling' axes, like this:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:output method="text"/>
<xsl:template match="/">
<xsl:apply-templates select="//methods/method"/>
</xsl:template><xsl:template match="method">
<!-- index is set to an empty value if the current name is unique in
the current set of 'method' elements, or to the index of the
current method in the set of methods with the same name if the
name isn't unique -->
<xsl:variable name="index"><xsl:if test="count(preceding-sibling::*[@name=current()/@name]) + count(following-sibling::*[@name=current()/@name])>0"><xsl:value-of select="1+count(preceding-sibling::*[@name=current()/@name])"/></xsl:if></xsl:variable>
void <xsl:value-of select="concat(@name, $index)"/>() {}
</xsl:template></xsl:stylesheet>
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
modified on Monday, June 15, 2009 2:14 PM
-
You want to use the 'preceding-sibling' and 'following-sibling' axes, like this:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:output method="text"/>
<xsl:template match="/">
<xsl:apply-templates select="//methods/method"/>
</xsl:template><xsl:template match="method">
<!-- index is set to an empty value if the current name is unique in
the current set of 'method' elements, or to the index of the
current method in the set of methods with the same name if the
name isn't unique -->
<xsl:variable name="index"><xsl:if test="count(preceding-sibling::*[@name=current()/@name]) + count(following-sibling::*[@name=current()/@name])>0"><xsl:value-of select="1+count(preceding-sibling::*[@name=current()/@name])"/></xsl:if></xsl:variable>
void <xsl:value-of select="concat(@name, $index)"/>() {}
</xsl:template></xsl:stylesheet>
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
modified on Monday, June 15, 2009 2:14 PM
Thank you very much Stuart.. It really worked... Hats off to u