xsl comparison of dotted decimal values
Posted on 2014-08-13
I am using xsl (through lxml.etree) to transform some XML. One of the attributes I need to check is a required minimum version number, e.g. minVers="5.1.7". I pass the current version number as a parameter to the XSL, so need to check that the passed-in version number is at or later than the minVers value. As far as I know, the first component of the version number will rise slowly, and the 2nd and 3rd will reset to 0 when the one before them increments, then increment with each minor version.
I can't do a simple "less than" arithmetic comparison, because of the multiple dots. I can't do a string comparison, because 4.12.4 will come out as earlier than 4.2.9.
I can see two options:
- pass the current version as a dotted value like this, then compare the dotted versions correctly (i.e. component by component). I think this might be quite a complex comparison, and because of the way the XML is handled, I'll need to do it at least 4 times.
- change the passed-in current version to a single integer - assuming that the minor components won't exceed 99 (the highest I have seen in the past is "24"), you could do this by zero-padding the minor versions to 2 characters and combining them, so 4.7.2 -> 40702, and 6.13.1 -> 61301 (though other methods may exist). I can do this easily in the calling code. Then in the XSL, change the value stored in the attribute in the same way and do the simple arithmetic comparison.
So, unless someone comes up with a simple way to compare two dotted-decimal values in XSL, I think I'm looking for a good way to change the attribute value from dotted decimal to a single integer.
(Apparently the, to me, obvious solution of changing the values in the source XML file to be the single-integer version is not acceptable!)