Altova Mailing List Archives>Archive Index >microsoft.public.xml Archive Home >Recent entries >Thread Prev - Re: data conversion in xslt? >Thread Next - Re: data conversion in xslt? Re: data conversion in xslt?To: NULL Date: 9/2/2005 6:57:00 AM "Marvin Smit" <marvin.smit@g...> wrote in message news:rr8eh19rp5s4j02dek6h990eaf7grhqj7c@4...... > Hi guys, > > being interrested int he topic, i was reading this and checking the > solution found on the mentioned link. > > After seeying that i had something like... MMhhh, don't think 4 or 5 > XSLT's are really needed... Lets see if I can figure something out > here. C'mon Marvin, the hex-to-decimal template uses only one additional template -- str-foldl. Just for your convenience I have packed in one stylesheet module all the code and a test of it -- no stylesheets are imported. The code, without the test is 55 lines long. No stacks or whatever need be modelled. It works without problems with quite long hexadecimal numbers -- in this case a 8-hexadecimal-digit number. In case it is rewritten in XSLT 2.0 and xs:integer is used, the results will always be correct (as Saxon 8.x implements "Big Arithmetic"). This transformation: <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:hex-converter="f:hex-converter" > <xsl:output method="text"/> <xsl:template match="/"> <xsl:call-template name="hex-to-decimal"> <xsl:with-param name="pxNumber" select="'ABCD0983'"/> </xsl:call-template> </xsl:template> <hex-converter:hex-converter/> <xsl:variable name="hexDigits" select="'0123456789ABCDEF'"/> <xsl:template name="hex-to-decimal"> <xsl:param name="pxNumber"/> <xsl:variable name="vFunXConvert" select="document('')/*/hex-converter:*[1]"/> <xsl:call-template name="str-foldl"> <xsl:with-param name="pFunc" select="$vFunXConvert"/> <xsl:with-param name="pA0" select="0"/> <xsl:with-param name="pStr" select="$pxNumber"/> </xsl:call-template> </xsl:template> <xsl:template match="hex-converter:*"> <xsl:param name="arg1"/> <!-- $pA0 --> <xsl:param name="arg2"/> <!-- a char (digit) --> <xsl:value-of select="16 * $arg1 + string-length(substring-before($hexDigits, $arg2))"/> </xsl:template> <xsl:template name="str-foldl"> <xsl:param name="pFunc" select="/.."/> <xsl:param name="pA0"/> <xsl:param name="pStr"/> <xsl:choose> <xsl:when test="not(string($pStr))"> <xsl:copy-of select="$pA0"/> </xsl:when> <xsl:otherwise> <xsl:variable name="vFunResult"> <xsl:apply-templates select="$pFunc[1]"> <xsl:with-param name="arg0" select="$pFunc[position() > 1]"/> <xsl:with-param name="arg1" select="$pA0"/> <xsl:with-param name="arg2" select="substring($pStr,1,1)"/> </xsl:apply-templates> </xsl:variable> <xsl:call-template name="str-foldl"> <xsl:with-param name="pFunc" select="$pFunc"/> <xsl:with-param name="pStr" select="substring($pStr,2)"/> <xsl:with-param name="pA0" select="$vFunResult"/> </xsl:call-template> </xsl:otherwise> </xsl:choose> </xsl:template> </xsl:stylesheet> when applied on any source xml (ignored), correctly converts the 8-digit hexadecimal number x'ABCD0983' to the decimal number 2882341251 Hope this would be useful. :o) Cheers, Dimitre Novatchev > > So, this is the resulting XSLT i made. It works using a stack > mechanism, so to high a values for the "Hex" will eventually break the > XSLT transformation with a Stack overflow (1 stack level per > character). > > I also saw that moving beyond the "12 character hexadecimal figure" > makes the result unreliable (rounding issues on large numbers) > > 488df73faa9d = 79774575733405 still worked correctly. > > ;============== XSLT STARTS HERE ================ > <?xml version="1.0" encoding="UTF-8"?> > <!-- Hex 2 Dec converter in XSLT without extensions --> > <!-- The following XSLT has been written for educational purposes only > --> > <!-- If you would like to use this template as-is in a commercial > product, please contact me first --> > <!-- Marvin Smit, marvin.smit@g... --> > > <xsl:stylesheet version="1.0" > xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> > <xsl:output method="xml" version="1.0" encoding="UTF-8" > indent="yes"/> > > <xsl:template match="hex"> > <xsl:variable name="HexStr" > select="translate(text(),'abcdef','ABCDEF')"/> > > <xsl:call-template name="Hex2Dec"> > <xsl:with-param name="HexVal" > select="$HexStr"/> > <xsl:with-param name="Multiplier" > select="'1'"/> > </xsl:call-template> > </xsl:template> > > <xsl:template name="Hex2Dec"> > <xsl:param name="HexVal"/> > <xsl:param name="Multiplier"/> > > <xsl:variable name="HexCharVal"> > <xsl:call-template name="HexChar2Dec"> > <xsl:with-param name="HexChar" > select="substring($HexVal, string-length($HexVal), 1)"/> > </xsl:call-template> > </xsl:variable> > > <xsl:variable name="HexRest"> > <xsl:if test="string-length($HexVal) > 1"> > <xsl:call-template name="Hex2Dec"> > <xsl:with-param name="HexVal" > select="substring($HexVal, 1, string-length($HexVal) - 1)"/> > <xsl:with-param > name="Multiplier" select="$Multiplier * 16"/> > </xsl:call-template> > </xsl:if> > <xsl:if test="string-length($HexVal) <= 1"> > <xsl:value-of select="0"/> > </xsl:if> > </xsl:variable> > > <xsl:value-of select="($HexCharVal * $Multiplier) + > $HexRest"/> > </xsl:template> > > <xsl:template name="HexChar2Dec"> > <xsl:param name="HexChar"/> > <xsl:choose> > <xsl:when test="$HexChar = 'A'">10</xsl:when> > <xsl:when test="$HexChar = 'B'">11</xsl:when> > <xsl:when test="$HexChar = 'C'">12</xsl:when> > <xsl:when test="$HexChar = 'D'">13</xsl:when> > <xsl:when test="$HexChar = 'E'">14</xsl:when> > <xsl:when test="$HexChar = 'F'">15</xsl:when> > <xsl:otherwise><xsl:value-of > select="$HexChar"/></xsl:otherwise> > </xsl:choose> > </xsl:template> > </xsl:stylesheet> > ;============== XSLT ENDS HERE ================ > > ;============== XML SAMPLE STARTS HERE ================ > <?xml version="1.0" encoding="UTF-8"?> > <hex>488df73faa9d</hex> > ;============== XML SAMPLE ENDS HERE ================ > > > Hope this helps, > > Marvin Smit > > > On Thu, 1 Sep 2005 06:58:15 +1000, "Dimitre Novatchev" <x@y...> > wrote: > >> >>"Illustris" <Illustris@d...> wrote in message >>news:F173103B-2B85-4C78-B7A9-B5A9D10F2BDC@m...... >>> Hello, >>> >>> I am attempting to convert a hexidecimal value to decimal value from an >>> XML >>> file. Can this be done in XSLT for output to HTML? >> >>Yes. >> >> >> http://www.biglist.com/lists/xsl-list/archives/200111/msg00835.html >> >> >>Cheers, >>Dimitre Novatchev >> > | ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
