Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
448 views
in Technique[技术] by (71.8m points)

xslt - Is there an "elegant" way to test that an attribute value starts with a letter?

I need to test whether an attibute value starts with a letter. If it doesn't I'll prefix it with "ID_" so it will be a valid id type of attribue value. I currently have the following (testing that the value does not start with a number - I know these attribute values will only start with a letter or number), but I am hoping there is an more elegant way:

<xsl:if test="not(starts-with(@value, '1')) and not(starts-with(@value, '2')) and not(starts-with(@value, '3')) and not(starts-with(@value, '4')) and not(starts-with(@value, '5')) and not(starts-with(@value, '6')) and not(starts-with(@value, '7')) and not(starts-with(@value, '8')) and not(starts-with(@value, '9')) and not(starts-with(@value, '0')) ">

I'm using XSLT 1.0. Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use:

not(number(substring(@value,1,1)) = number(substring(@value,1,1)) )

Or use:

not(contains('0123456789', substring(@value,1,1)))

Finally, this may be the shortest XPath 1.0 expression to verify your condition:

not(number(substring(@value, 1, 1)+1))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...