I am using XSLT 1.0. My input information may contain these values
<!--case 1-->
<attribute>123-00</attribute>
<!--case 2-->
<attribute>Abc-01</attribute>
<!--case 3-->
<attribute>--</attribute>
<!--case 4-->
<attribute>Z2-p01</attribute>
I want to find out those string that match the criteria:
if string has at least 1 alphabet AND has at least 1 number,
then
do X processing
else
do Y processing
In example above, for case 1,2,4 I should be able to do X
processing. For case 3, I should be able to do Y
processing.
I aim to use a regular expression (in XSLT 1.0).
For all the cases, the attribute can take any value of any length.
I tried use of match
, but the processor returned an error.
I tried use of translate
function, but not sure if used the right way.
I am thinking about.
if String matches [a-zA-Z0-9]*
then do X processing
else
do y processing.
How do I implement that using XSLT 1.0 syntax?
XSLT does not support regular expressions, but you can fake it.
The following stylesheet prints an
X processing
message for allattribute
elements having a string value containing at least one number and at least one letter (andY processing
for those that do not):Note: You said this:
But that conflicts with your requirement, because case 1 does not contain a letter. If, on the other hand, you really want to match the equivalent of
[a-zA-Z0-9]
, then use this:...which matches any
attribute
having at least one letter or number.See the following question for more information on using
translate
in this way:If you found this question because you're looking for a way to use regular expressions in XSLT 1.0, and you're writing an application using Microsoft's XSLT processor, you can solve this problem by using an inline C# script.
I've written out an example and a few tips in this thread, where someone was seeking out similar functionality. It's super simple, though it may or may not be appropriate for your purposes.
This solution really works in XSLT 1.0 (and is simpler, because it doesn't and needn't use the double-translate method.):
when applied on the provided XML fragment -- made a well-formed XML document:
the wanted, correct result is produced:
Do Note: Any attempt to use with a true XSLT 1.0 processor code like this (borrowed from another answer to this question) will fail with error:
because in XSLT 1.0 it is forbidden for a match pattern to contain a variable reference.