Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. XML / XSL
  4. Specifying condition with count function

Specifying condition with count function

Scheduled Pinned Locked Moved XML / XSL
xmltutorialai-codingquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Fadi Yoosuf
    wrote on last edited by
    #1

    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

    S 1 Reply Last reply
    0
    • F Fadi Yoosuf

      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

      S Offline
      S Offline
      Stuart Dootson
      wrote on last edited by
      #2

      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

      F 1 Reply Last reply
      0
      • S Stuart Dootson

        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

        F Offline
        F Offline
        Fadi Yoosuf
        wrote on last edited by
        #3

        Thank you very much Stuart.. It really worked... Hats off to u

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups