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