How would I do "substring(variable,1,1) between a-z or A-Z" then do X else do Y using XSLT? I know that one option would be using regex but I would expect there to be something that wasn't quite so much overkill.
+3
A:
A simple XSLT 1.0 solution:
This transformation:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:variable name="vLetters"
select="'ABCDEFGHIKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'"/>
<xsl:variable name="vText" select="'1Text'"/>
<xsl:template match="/">
<xsl:choose>
<xsl:when test=
"contains($vLetters, substring($vText,1,1))">
Letter
</xsl:when>
<xsl:otherwise>
Not Letter
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
when applied on any XML document (not used), produces the wanted result:
Not Letter
Depending on the specific case one can add whatever processing is necessary to each of the two "clauses" (<xsl:when>
and <xsl:otherwise>
) of the <xsl:choose>
instruction.
Dimitre Novatchev
2009-04-15 21:18:52
+1
A:
And for XSLT 2.0, you can use the regular expression function matches
:
<xsl:choose>
<xsl:when test="matches($variable1, '^[a-zA-Z].*$')">
Match
</xsl:when>
<xsl:otherwise>
NoMatch
</xsl:otherwise>
</xsl:choose>
ee
2009-04-15 21:27:17
XSLT 2.0 has, among other disadvantages, the disadvantage that I can't do quick and dirty transform tests in whatever browser I want.
Brian
2009-04-16 15:47:25
Agreed. I tend to use XSLT 1.0 for the same reason.
ee
2009-04-16 16:40:21