tags:

views:

53

answers:

1

Hi!

I am wondering if it is possible to use XPath or XSL to determine if a XML node value is numerical or alpha/numerical. (It is user entered data.. so it could be either..)

I was searching for some sort of function in XPath to do this.. but I couldn't find any.

for example:

<example>
    <test1 attribute="123">12dfffg23</test1>
    <test2 attribute="a34">123456</test2>
</example>

I need to test node content and attibutes:

/example/test1
/example/test1/@attribute
/example/test2
/example/test2/@attribute

And I would expect the following to be numerical:

/example/test1/@attribute
/example/test2

And I would expect the following to be alpha/numerical:

/example/test1
/example/test2/@attribute

Is it possible to do this with a function in XPath or XSL?

Thanks! :D

+2  A: 

In XPath 1.0 use this to test whether a given string contains a number:

number(someString) = number(someString)

Use this to test whether a given string consists only of alphanumeric characters (letters or digits):

not(translate(someString, $alphabetLowerAndUpperAndDigits, ''))

where $alphabetLowerAndUpperAndDigitscan be substituted with all lowercase and uppercase letters and the digits (0-9) from the alphabet (for the latin alphabet this is:

'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'

In XPath 2.0: (all of the above plus)

matches(someString, '^[\c|\d]+$')
Dimitre Novatchev
For the `matches` function, don't you need to anchor the regular expression (`'^\c+$'`) - otherwise won't it always match if there's *at least one* alphanumeric character present?
psmears
@psmears: Thank you, this is right -- I have updated the answer.
Dimitre Novatchev
I don't think this addresses the problem.. the value can contain the alphabet and numbers and be considered 'alpha', but 'numerical' should mean only numbers. these here only check if a string contains *a* number or if a string is *only* alpha. do you know what i mean?
developer
@iHeartGreek: No, in your question you are asking how to verify that a value is a number and how to verify that a value is "alpha". I answered these questions. In case you are interested in something else, please either modify the question or ask a new question.
Dimitre Novatchev
Why does `number(someString) = number(someString)` work? Seems like 'number(someString) = someString` is more likely to do what you want? Or am I misunderstanding something?
geoffc
@iHeartGreek: It seems you are asking for alha-numeric, not just for "alpha". If so, please update your question.
Dimitre Novatchev
@geoffc: You are misunderstanding. `number(someString) = someString` is false for someString = ' 3'. However the test `number(someString) = number(someString)` is true. It is true exactly when someString is a number. Here is how it works: if someString is not a number, `number(someString)` is NaN. By definition NaN is not equal to anything, even to NaN, so in this case the result is false.
Dimitre Novatchev
@Dimitre - I thought my example, and what i expected showed what I really meant. I updated it to alpha/numerical for you though. But please look at my example and what I expect.
developer
@Dimitre: Your last comment brings me to believe if I use that I will know if it *entirely* a number, not just *contains* a number like you use in your original explanation: "use this to test whether a given string contains a number:" This explanation might need to be reworded then? Or did I misunderstand your last comment?
developer
@iHeartGreek: By "contains a number" I mean "is a number. For example ' 30 ' and '30' both contain a number.
Dimitre Novatchev
@iHeartGreek: I modified my answer to the changes in the question,
Dimitre Novatchev
@Dimitre: +1 for brilliant response and patience!
Alejandro
@Dimitre: Ah, I see. Did not realize NaN is not equal to NaN. Thanks!
geoffc