Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write xslt if element contains letters?

Tags:

regex

xslt

Sth should be all letters or in regular expressions [A-Z]. How to combine xslt with regular expressions?

 <xsl:if test="string-contains(//ns0:elem/value, 'sth')">


        </xsl:if>
like image 802
marko Avatar asked Dec 05 '11 14:12

marko


2 Answers

XPath/XSLT 1.0 does not support regular expressions, but simple validation can be performed using the basic string functions.

Whitelisting

The XPath 1.0 translate function can be used to simulate a whitelist:

<xsl:variable name="alpha" 
              select="'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
<xsl:if test="string-length(translate(., $alpha, '')) &gt; 0">
    <!-- context node contains non-alpha characters -->
</xsl:if>

The test uses translate to first remove all upper- and lower-case letters. If the resulting string's length is non-zero, then the original string must have contained additional characters.

Note that the expression above could be simplified to:

<xsl:if test="translate(., $alpha, '')">

... because any non-empty string evaluates to true.

Blacklisting

Use the double-translate method to treat $alpha as a blacklist:

<xsl:if test="translate(., translate(., $alpha, ''), '')">
    <!-- context-node contains characters on the blacklist (in $alpha) -->
</xsl:if>

The inner translate returns a string with all its alpha characters removed, which is then used as the template to the second translate call, resulting in a string containing only the alpha characters. If this string is non-zero, then we found a character on the blacklist. This is a classic approach. See, for example, this previous question on SO:

  • Replace special characters in XSLT

A blacklist test could also be performed like this:

not(string-length(translate(., $alpha, ''))=string-length())

If the length of the string after removing all of the blacklisted characters is not equal to the length of the original string, then the string must have contained a character on the blacklist.

Summary

Blacklists and whitelists are really two sides of the same coin. The following demonstrates their usage together:

<xsl:if test="translate(., $alpha, '')">
    [contains some characters not on the list]
</xsl:if> 
<xsl:if test="not(translate(., $alpha, ''))">
    [contains only characters on the list]
</xsl:if> 
<xsl:if test="translate(., translate(., $alpha, ''), '')">
    [contains some characters on the list]
</xsl:if>
<xsl:if test="not(translate(., translate(., $alpha, ''), ''))">
    [contains only characters not on the list]
</xsl:if>
like image 107
Wayne Avatar answered Nov 05 '22 02:11

Wayne


In XPath 1.0 (works with XSLT 1.0), you don't have tool for regex (you just have functions like contains or starts-with, see link below for more information)

In XPATH 2.0 (works with XSLT 2.0), you've got the matches function and the replace function (see link below).

URL to see :

XPath 1.0 string functions : http://www.w3.org/TR/xpath/#section-String-Functions

XPath 2.0 regex functions : http://www.w3.org/TR/xpath-functions/#string.match

like image 21
Vincent Biragnet Avatar answered Nov 05 '22 02:11

Vincent Biragnet