Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: data conversion in xslt?

From: Marvin Smit <marvin.smit@-----.--->
To: NULL
Date: 9/1/2005 5:58:00 PM
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.

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) &lt;= 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 
>



transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent