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. How to use XSL to get the most common value in XML

How to use XSL to get the most common value in XML

Scheduled Pinned Locked Moved XML / XSL
xmlquestionjavascriptalgorithmstutorial
7 Posts 3 Posters 1 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.
  • D Offline
    D Offline
    darasd
    wrote on last edited by
    #1

    Hello. How can I use XSL to get the most common value of a given attribute in some unsorted XML? For instance, here is some XML: I would like to end up with , therefore producing an output of "0930" - the most common value of the time attribute. Just to complicate things, I need to do further validation on each fixture (using the date attribute), to see whether it is counted or not. This validation is done using Javascript, since it involves date functions, and we are using (and due to 3rd party restrictions, can only use) XSL 1.0. So, each fixture needs to be examined in a separate template, rather like this: , and is discounted or counted depending on whether this output is 'true' or 'false'. I've tried a recursive template, but do not see a way of sorting the Fixtures node that has to be passed into it; if I try a for-each loop, there is no way of keeping track of the most common time. Thanks.

    L G 2 Replies Last reply
    0
    • D darasd

      Hello. How can I use XSL to get the most common value of a given attribute in some unsorted XML? For instance, here is some XML: I would like to end up with , therefore producing an output of "0930" - the most common value of the time attribute. Just to complicate things, I need to do further validation on each fixture (using the date attribute), to see whether it is counted or not. This validation is done using Javascript, since it involves date functions, and we are using (and due to 3rd party restrictions, can only use) XSL 1.0. So, each fixture needs to be examined in a separate template, rather like this: , and is discounted or counted depending on whether this output is 'true' or 'false'. I've tried a recursive template, but do not see a way of sorting the Fixtures node that has to be passed into it; if I try a for-each loop, there is no way of keeping track of the most common time. Thanks.

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      This does not seem like an appropriate use of XSLT. It is a data transformation mechanism not a data analysis mechanism. Therefore even if you could figure out how to do it, it doesn't mean it's a good idea.

      led mike

      D 1 Reply Last reply
      0
      • L led mike

        This does not seem like an appropriate use of XSLT. It is a data transformation mechanism not a data analysis mechanism. Therefore even if you could figure out how to do it, it doesn't mean it's a good idea.

        led mike

        D Offline
        D Offline
        darasd
        wrote on last edited by
        #3

        This is part of a data transformation. I want to place an element around the fixtures, stating "time is hh:mm unless stated", where hh:mm is the most common value. The purpose of the stylesheet is certainly not analysis. I think that this is a grouping problem, requiring Muenchian grouping, but I've got no further than that.

        L 1 Reply Last reply
        0
        • D darasd

          This is part of a data transformation. I want to place an element around the fixtures, stating "time is hh:mm unless stated", where hh:mm is the most common value. The purpose of the stylesheet is certainly not analysis. I think that this is a grouping problem, requiring Muenchian grouping, but I've got no further than that.

          L Offline
          L Offline
          led mike
          wrote on last edited by
          #4

          darasd wrote:

          This is part of a data transformation.

          That's the way you are looking and perhaps implementing it but that is not what I am referring to.

          darasd wrote:

          to get the most common value

          That is an analysis activity, not a transformation activity. Do you understand my point?

          led mike

          D 1 Reply Last reply
          0
          • L led mike

            darasd wrote:

            This is part of a data transformation.

            That's the way you are looking and perhaps implementing it but that is not what I am referring to.

            darasd wrote:

            to get the most common value

            That is an analysis activity, not a transformation activity. Do you understand my point?

            led mike

            D Offline
            D Offline
            darasd
            wrote on last edited by
            #5

            I'm afraid I'm not convinced that because it is analysis, then it has no place in XSL. You could argue that any transformation is, when it comes down to it, an analysis activity. Why is any transformation performed? To allow the data to be output and therefore interpreted in a different way, which is simply another way of saying Analysis. For example, a common activity is to list all the elements with a given attribute value. This in itself is a form of analysis.

            L 1 Reply Last reply
            0
            • D darasd

              I'm afraid I'm not convinced that because it is analysis, then it has no place in XSL. You could argue that any transformation is, when it comes down to it, an analysis activity. Why is any transformation performed? To allow the data to be output and therefore interpreted in a different way, which is simply another way of saying Analysis. For example, a common activity is to list all the elements with a given attribute value. This in itself is a form of analysis.

              L Offline
              L Offline
              led mike
              wrote on last edited by
              #6

              Ok, good luck

              led mike

              1 Reply Last reply
              0
              • D darasd

                Hello. How can I use XSL to get the most common value of a given attribute in some unsorted XML? For instance, here is some XML: I would like to end up with , therefore producing an output of "0930" - the most common value of the time attribute. Just to complicate things, I need to do further validation on each fixture (using the date attribute), to see whether it is counted or not. This validation is done using Javascript, since it involves date functions, and we are using (and due to 3rd party restrictions, can only use) XSL 1.0. So, each fixture needs to be examined in a separate template, rather like this: , and is discounted or counted depending on whether this output is 'true' or 'false'. I've tried a recursive template, but do not see a way of sorting the Fixtures node that has to be passed into it; if I try a for-each loop, there is no way of keeping track of the most common time. Thanks.

                G Offline
                G Offline
                Gerald Schwab
                wrote on last edited by
                #7

                How about something like this?

                <xsl:variable name="mostCommonTime">
                <xsl:for-each select="//fixtures/fixture">
                <xsl:sort select="count(time)" order="descending"/>
                <xsl:if test="position() = 1">
                <xsl:value-of select="count(time)"/>
                </xsl:if>
                </xsl:for-each>
                </xsl:variable>

                modified on Wednesday, October 15, 2008 11:31 PM

                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