Discussion:
Best way of testing Hexadecimal value [xslt 1.0]
p***@thomsondigital.com
2010-08-26 09:30:43 UTC
Permalink
Hello all,

I just did the following test for one of the requirement in my stylesheet.
Though I am able to get the correct result but somehow I am not convinced
with the approach. There must be better/correct way of doing this.

source
=====
<label>#x2022</label><!-- HEXADECIMAL FOR BULLET ACTUALLY WITH
'AMPERSAND' AND ; ADDED TO IT -->

Test
====
<xsl:template match="label">
<xsl:copy>
<xsl:choose>
<xsl:when test=".='.'"><!-- ='.' IS LITERAL BULLET SYMBOL -->
<!-- Do Something here -->
</xsl:when>
<xsl:when test=".!='.'"><!-- ='.' IS LITERAL BULLET SYMBOL -->
<!-- Do Something different here -->
</xsl:when>
</xsl:choose>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>

<xsl:when test=".= 'BULLET SIGN'"> doesn't seems to be nice to me. Is
there any better way to test hexadecimal values.
Apart from this is there a way of testing whether string is a digit or
character <label>1.</label> <label>a.</label>.

Not tested:

I believe <xsl:when test=".='BULLET SIGN'"> will check for the literal
string instead of something like [A-Za-z0-9]. I am using XSLT 1.0.

Any thoughts will be appreciated.

TIA,
Pankaj





--~------------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-***@lists.mulberrytech.com>
--~--
Hermann Stamm-Wilbrandt
2010-08-26 10:19:11 UTC
Permalink
Post by p***@thomsondigital.com
<xsl:when test=".= 'BULLET SIGN'"> doesn't seems to be nice to me.
Perhaps you want something like 1c) from [1].
Post by p***@thomsondigital.com
Is there any better way to test hexadecimal values.
This is an old techniques (see [2] for the removal):
test="translate(.'0123456789abcdefABCDEF','')='')"
It tests the current node for consisting of hexadecimal characters only.
Post by p***@thomsondigital.com
Apart from this is there a way of testing whether string is a digit
or character <label>1.</label> <label>a.</label>.
test="translate(.'0123456789abc.....yzABC.....YZ','')='')"
tests for the current node consisting of digits or characters.
You may want to add "and string-length()=1" to that.
Post by p***@thomsondigital.com
I believe <xsl:when test=".='BULLET SIGN'"> will check for the literal
string instead of something like [A-Za-z0-9]. I am using XSLT 1.0.
Yes, it tests for the single literal value '.'


[1]
http://www.xml.org/sites/www.xml.org/files/xslt_efficient_programming_techniques.pdf
[2] http://www.w3.org/TR/xpath/#function-translate


Mit besten Gruessen / Best wishes,

Hermann Stamm-Wilbrandt
Developer, XML Compiler, L3
WebSphere DataPower SOA Appliances
----------------------------------------------------------------------
IBM Deutschland Research & Development GmbH
Vorsitzender des Aufsichtsrats: Martin Jetter
Geschaeftsfuehrung: Dirk Wittkopp
Sitz der Gesellschaft: Boeblingen
Registergericht: Amtsgericht Stuttgart, HRB 243294



From: ***@thomsondigital.com
To: xsl-***@lists.mulberrytech.com
Date: 08/26/2010 11:31 AM
Subject: [xsl] Best way of testing Hexadecimal value [xslt 1.0]



Hello all,

I just did the following test for one of the requirement in my stylesheet.
Though I am able to get the correct result but somehow I am not convinced
with the approach. There must be better/correct way of doing this.

source
=====
<label>#x2022</label><!-- HEXADECIMAL FOR BULLET ACTUALLY WITH
'AMPERSAND' AND ; ADDED TO IT -->

Test
====
<xsl:template match="label">
<xsl:copy>
<xsl:choose>
<xsl:when test=".='.'"><!-- ='.' IS LITERAL BULLET SYMBOL -->
<!-- Do Something here -->
</xsl:when>
<xsl:when test=".!='.'"><!-- ='.' IS LITERAL BULLET SYMBOL -->
<!-- Do Something different here -->
</xsl:when>
</xsl:choose>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>

<xsl:when test=".= 'BULLET SIGN'"> doesn't seems to be nice to me. Is
there any better way to test hexadecimal values.
Apart from this is there a way of testing whether string is a digit or
character <label>1.</label> <label>a.</label>.

Not tested:

I believe <xsl:when test=".='BULLET SIGN'"> will check for the literal
string instead of something like [A-Za-z0-9]. I am using XSLT 1.0.

Any thoughts will be appreciated.

TIA,
Pankaj





--~------------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-***@lists.mulberrytech.com>
--~--




--~------------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-***@lists.mulberrytech.com>
--~--
p***@thomsondigital.com
2010-08-26 10:42:31 UTC
Permalink
[1]
http://www.xml.org/sites/www.xml.org/files/xslt_efficient_programming_techniques.pdf

[2] http://www.w3.org/TR/xpath/#function-translate

Thanks for detailed explantion Hermann. translate() is what I was too
thinking of earlier.

Pankaj

--~------------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-***@lists.mulberrytech.com>
--~--
David Carlisle
2010-08-26 11:30:44 UTC
Permalink
Post by p***@thomsondigital.com
Hello all,
I just did the following test for one of the requirement in my stylesheet.
Though I am able to get the correct result but somehow I am not convinced
with the approach. There must be better/correct way of doing this.
source
=====
<label>#x2022</label><!-- HEXADECIMAL FOR BULLET ACTUALLY WITH
'AMPERSAND' AND ; ADDED TO IT -->
Test
====
<xsl:template match="label">
<xsl:copy>
<xsl:choose>
<xsl:when test=".='.'"><!-- ='.' IS LITERAL BULLET SYMBOL -->
If you don't want to use a bullet character you can use the numeric
reference, just as your source has done and test

. = '& # x 2 0 2 2 ;'

It's not really clear if that is what you are asking?
Post by p***@thomsondigital.com
<!-- Do Something here -->
</xsl:when>
<xsl:when test=".!='.'"><!-- ='.' IS LITERAL BULLET SYMBOL -->
xsl:otherwise is of course more efficient than testing the negation
which is repeating the test done in the previous clause.
Post by p***@thomsondigital.com
<!-- Do Something different here -->
</xsl:when>
</xsl:choose>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:when test=".= 'BULLET SIGN'"> doesn't seems to be nice to me. Is
there any better way to test hexadecimal values.
Apart from this is there a way of testing whether string is a digit or
character<label>1.</label> <label>a.</label>.
you can test if number(.)=number(.) If the string represents a number,
this is true, if it is a non number it is the test NaN = NaN which is
false.
Post by p***@thomsondigital.com
I believe<xsl:when test=".='BULLET SIGN'"> will check for the literal
string instead of something like [A-Za-z0-9]. I am using XSLT 1.0.
Any thoughts will be appreciated.
TIA,
Pankaj
david

________________________________________________________________________
The Numerical Algorithms Group Ltd is a company registered in England
and Wales with company number 1249803. The registered office is:
Wilkinson House, Jordan Hill Road, Oxford OX2 8DR, United Kingdom.

This e-mail has been scanned for all viruses by Star. The service is
powered by MessageLabs.
________________________________________________________________________

--~------------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-***@lists.mulberrytech.com>
--~--
David Carlisle
2010-08-26 12:39:17 UTC
Permalink
On 26/08/2010 13:21, ***@thomsondigital.com wrote:

please don't take things off list
. = '& # x 2 0 2 2 ;'
You mean with spaces. If that is the case I didn't knew about it.
No I put in spaces to make sure it showed up (sorry, I should have said)
some mail readers try to be "clever" and replace character references by
the character.
number(.)=number(.)
Do you mean<xsl:when test="number(.)=number(.)">
could you be more clear about this?
yes.

<xsl:when test="number('12')=number('12')">
is true, because the string '12' casts to a number.

<xsl:when test="number('xy')=number('xy')">
is false as the string 'xy' casts to the special value NaN which is not
equal to anything, even itself.

so

<xsl:when test="number(.)=number(.)">

is true just if the string value of the current node is the lexical form
of a number.
Thanks in advance.
Pankaj
David
________________________________________________________________________
The Numerical Algorithms Group Ltd is a company registered in England
and Wales with company number 1249803. The registered office is:
Wilkinson House, Jordan Hill Road, Oxford OX2 8DR, United Kingdom.

This e-mail has been scanned for all viruses by Star. The service is
powered by MessageLabs.
________________________________________________________________________

--~------------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-***@lists.mulberrytech.com>
--~--
p***@thomsondigital.com
2010-08-26 13:33:11 UTC
Permalink
<xsl:when test="number('12')=number('12')">
is true, because the string '12' casts to a number.

<xsl:when test="number('xy')=number('xy')">
is false as the string 'xy' casts to the special value NaN which is not
equal to anything, even itself.


Thanks David, learned something today, which I never tried earlier. Though
I still need to test it.
Hope it works in 1.0. :-)
cheers
Pankaj






--~------------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-***@lists.mulberrytech.com>
--~--
David Carlisle
2010-08-26 13:45:20 UTC
Permalink
Post by p***@thomsondigital.com
Thanks David, learned something today, which I never tried earlier.
I thought I picked that up from Jeni, although the earliest mention I
could see just now was from Michael,

http://markmail.org/message/oj3xxs7uizwzmlp5

David


________________________________________________________________________
The Numerical Algorithms Group Ltd is a company registered in England
and Wales with company number 1249803. The registered office is:
Wilkinson House, Jordan Hill Road, Oxford OX2 8DR, United Kingdom.

This e-mail has been scanned for all viruses by Star. The service is
powered by MessageLabs.
________________________________________________________________________

--~------------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-***@lists.mulberrytech.com>
--~--
p***@thomsondigital.com
2010-08-26 12:29:49 UTC
Permalink
. = '& # x 2 0 2 2 ;'

You mean with spaces. If that is the case I didn't knew about it.

number(.)=number(.)

Do you mean <xsl:when test="number(.)=number(.)">
could you be more clear about this?

Thanks in advance.

Pankaj




--~------------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-***@lists.mulberrytech.com>
--~--
Mukul Gandhi
2010-08-26 14:46:22 UTC
Permalink
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) :)
Post by p***@thomsondigital.com
Is there any better way to test hexadecimal values.
--
Regards,
Mukul Gandhi

--~------------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-***@lists.mulberrytech.com>
--~--
p***@thomsondigital.com
2010-08-27 03:01:31 UTC
Permalink
Yet another fine sample.
Post by Mukul Gandhi
If you can, I encourage to move to a 2.0 environment (unless you are
programming for a browser) :)
I personally always use and prefer 2.0 but for this particular case I've
been asked to use 1.0. Some people still prefer old technologies over
latest ones :-)


Thanks anyway.

Pankaj




--~------------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-***@lists.mulberrytech.com>
--~--
Mukul Gandhi
2010-08-27 06:27:24 UTC
Permalink
in the code fragment I posted, I would propose the following change,

you may use:

<xsl:value-of select="false()" /> &
<xsl:value-of select="true()" />

instead of
<xsl:value-of select="'false'" />
<xsl:value-of select="'true'" />

This didn't struck me earlier :)

Regarding XSLT 2.0 vs 1.0, IMHO I would say the 2.0 version is much
better by design and increases programmer productivity considerably.

Here's a little anecdotal evidence:

I like this style of writing in 2.0

<xsl:variable name="x" select="fn:findSomething()" />

over

<xsl:variable name="x">
<xsl:call-template name="findSomething" />
</xsl:variable>

in 1.0
Post by p***@thomsondigital.com
Yet another fine sample.
Post by Mukul Gandhi
If you can, I encourage to move to a 2.0 environment (unless you are
programming for a browser) :)
I personally always use and prefer 2.0 but for this particular case I've
been asked to use 1.0. Some people still prefer old technologies over
latest ones :-)
Thanks anyway.
Pankaj
--
Regards,
Mukul Gandhi

--~------------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-***@lists.mulberrytech.com>
--~--
Michael Kay
2010-08-27 08:22:04 UTC
Permalink
Some people still prefer old technologies over latest ones :-)
If someone asks you to make a business trip of 100 miles by bicycle,
it's probably a while since he cycled anywhere.

Michael Kay
Saxonica

--~------------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-***@lists.mulberrytech.com>
--~--

Loading...