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 7:58:00 PM Hi Marvin, "Marvin Smit" <marvin.smit@g...> wrote in message news:qd3gh1t3ms5gmgdifc9166bndv189re3je@4...... > Hi Dimitril, > >>>>> 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? > > Reading this, I don't think we should do the "dig through different > templates and merge whats needed". But provide a single sollution that > shows how it might be done. Yes, but we live in the real world, where people come back tired from working a whole day and just have a spare minute, and still want to help... > >>It works without problems with quite long hexadecimal numbers -- in this >>case a 8-hexadecimal-digit number. Yep, I somehow misread your message and thought the instability happened for more than 6 digits. Sure, a 13 digit hex number is still converted correctly to a decimal -- like: x'ABCD0983ABCDE' = 3022353860312286 > > I just warned that the "single example" one i wrote goes untill 12 Hex > chars before becoming unstable. > > > Marvin Smit. > > ps: for completeness, what does the "fold version" do with non > capitilized hexadecimal chars in the value? Nothing, because in my definition from a few years ago of a hex number, only caps were allowed. Of course, not a problem: just change: <xsl:value-of select="16 * $arg1 + string-length(substring-before($hexDigits, $arg2))"/> to <xsl:value-of select= "16 * $arg1 + string-length(substring-before($hexDigits, translate($arg2,'abcdef','ABCDEF')))"/> so now we also have: x'ABCD0983abcde' = 3022353860312286 and this takes place not in the "str-foldl" template, but in the one matching "hex-converter:*" BTW, I don't agree with your statement that: I don't think we should do the "dig through different templates and merge whats needed". There are some vere generic and universally applicable functions (templates) such as str-foldl. Instead of providing this template inline in the code every time it is needed, it is much more convenient to simply import another stylesheet that contains this template. This also helps to get accustomed with the fact that we can always rely on such universal templates and to think of them as comprising a "template (or function) library". This is real power and it is the result of hiding/abstacting the functions'/templates' implementation, which is the opposite of providing their implementation. Cheers, Dimitre Novatchev. > > > On Fri, 2 Sep 2005 06:56:58 +1000, "Dimitre Novatchev" > <dimitren@t...> wrote: > >> >>"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 | |||
|
