Re: [xsl] Best way of testing Hexadecimal value [xslt 1.0]

Subject: Re: [xsl] Best way of testing Hexadecimal value [xslt 1.0]
From: Mukul Gandhi <gandhi.mukul@xxxxxxxxx>
Date: Thu, 26 Aug 2010 20:16:22 +0530
Not wanting to stop the fun!

But here's a 1.0 solution (a complete stylesheet, I wrote and stopped
posting it hours ago when I saw a solution using 'translate' -- which
is far better) to test for hexadecimal values using a
recursive-template method:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                       version="1.0">
				
  <xsl:output method="xml" omit-xml-declaration="yes" />				

  <xsl:variable name="hexDigits" select="'0123456789ABCDEFabcdef'" />

  <xsl:template match="/">
        <xsl:variable name="X" select="'#x202f'" />
	<xsl:variable name="Y" select="'#x20yf'" />
        <xsl:call-template name="isHex">
	    <xsl:with-param name="value" select="substring-after($X, '#x')" />
	</xsl:call-template>
	<xsl:text>&#xa;</xsl:text>
	<xsl:call-template name="isHex">
	    <xsl:with-param name="value" select="substring-after($Y, '#x')" />
	 </xsl:call-template>
  </xsl:template>

  <xsl:template name="isHex">
       <xsl:param name="value" />

	<xsl:choose>
            <xsl:when test="not($value = '')">
	        <xsl:choose>
	            <xsl:when test="not(contains($hexDigits,
substring($value, 1, 1)))">
		         <xsl:value-of select="'false'" />
	            </xsl:when>
		    <xsl:otherwise>
		         <xsl:call-template name="isHex">
			      <xsl:with-param name="value" select="substring($value, 2)" />
			 </xsl:call-template>
		    </xsl:otherwise>
		 </xsl:choose>
  	    </xsl:when>
	    <xsl:otherwise>
	         <xsl:value-of select="'true'" />
	    </xsl:otherwise>
	 </xsl:choose>
  </xsl:template>

</xsl:stylesheet>

But in 1.0 environment, you should definitely go for the 'translate' solution.

If you can, I encourage to move to a 2.0 environment (unless you are
programming for a browser) :)


On Thu, Aug 26, 2010 at 3:00 PM,  <pankaj.c@xxxxxxxxxxxxxxxxxx> wrote:
> Is there any better way to test hexadecimal values.



-- 
Regards,
Mukul Gandhi

Current Thread