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?To: NULL Date: 9/6/2005 8:45:00 AM Guys - thanks for the help. I'm only using this to convert small numbers like 1024 -- converting LCID Dec values to LCID Hex values. The simpler the better would be great. I'll play around with the sample code you gave me. "Marvin Smit" wrote: > Hi Dimitre, > > this is actually turning into a cool convo ;) > > > > >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... > > Which shows "true technology love and willing to share", respect for > that. > > >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. > > I agree, but i would like to take into the consideration if its an > experienced XML/XSLT'r or someone starting to learn the ropes. This > would change the approach i would take. > > >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. > > General functionality and re-use is always good. The small issue that > arrises here is that "no-one but you" knew: > >Nothing, because in my definition from a few years ago of a hex number, only > >caps were allowed. > > Meaning if used as-is, it might not work, because the Hex > rerpesentation says nothing about capitals or not. > > Anyways, > > I think we've answered the question "can you do Hex->Dec in XSLT" in > multiple ways and argumented both options. What else would a 'question > poser' want? :) > > Marvin Smit > > Ps; we both didn't show the 'other option' of using script in XSLT ;) > > > > On Fri, 2 Sep 2005 19:57:44 +1000, "Dimitre Novatchev" > <dimitren@t...> wrote: > > >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'"/> | ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
