'Characters only' check in xsl string?

一笑奈何 提交于 2020-01-15 05:33:16

问题


How can i check if a string include only characters (XSLT File)?

<xsl:variable name="IsValid1">
  <xsl:choose>
    <xsl:when test="string-length(//FirstName) &gt; 0 and string-length(//LastName) &gt; 0 and substring(//FirstName, 1, 3) != 'TST' and XXXX//FirtName only charactersXXXXX ">
    </xsl:when>
    <xsl:otherwise>
    </xsl:otherwise>
  </xsl:choose>
</xsl:variable>

回答1:


In XPath 1.0 (XSLT 1.0) you can use contains(). In XPath 2.0 (XSLT 2.0) you use matches().

You may want for example check for alphabetic characters only (no numerics, no other signs, no spaces):

matches(//FirstName, '^[a-zA-Z]+$')

or alphanumeric,

matches(//FirstName, '^[a-zA-Z0-9]+$')



回答2:


This XPath expression:

string-length(translate($yourString, $allValidChars, '')) = 0

evaluates to true() exactly when all characters in $yourString are contained in the string $allValidChars.

Otherwise it evaluates to false().

II. XPath 2.0 solution -- even more powerful

Whenever neither the "valid" characters or the "invalid characters" have a convenient and compact expression, one may use the RegEx capabilities of XPath 2.0. Using RegEx and character classes one can write this:

matches($str,'^\p{L}+$')

this matches $str only when it consists entirely of letters (all unicode characters that are letters in any Unicode-supported alphabet).

Here is a small XSLT 2.0 - based verification:

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="s[matches(.,'^\p{L}+$')]">
  <xsl:copy-of select="."/>
 </xsl:template>

 <xsl:template match="text()"/>
</xsl:stylesheet>

when this transformation is applied on the following XML document:

<t>
 <s>abcDПЖЗ</s>
 <s>abcd</s>
 <s>abcd123</s>
</t>

the wanted correct result is produced:

<s>abcDПЖЗ</s>
<s>abcd</s>

Explanation:

As per spec, \p{L} matches any letter.

Related to this problem:

In XPath 1.0, how would you remove all non-alpha characters from a string?

Here the difficulty is that you don't know what are all the non-apha characters.

The solution is also known as the "double-translate method", first shown by Michael Kay (@Michael Kay):

translate($s, translate($s, $alpha, ''), '')



回答3:


In XPath 1.0 I propose use this method to allow only letters in FirstName

Updated:

<xsl:variable name="not-allowed-characters">0123456789</xsl:variable>

<xsl:choose>
  <xsl:when test="string-length(translate(//FirstName, $not-allowed-characters, '')) = string-length(//FirstName)">
    <xsl:value-of select="true()"/>
  </xsl:when>

  <xsl:otherwise>
    <xsl:value-of select="false()"/>
  </xsl:otherwise>

</xsl:choose>

To extend this, simply add not allowed characters inside not-allowed-characters variable.


Old version:

<xsl:variable name="not-allowed-characters">0123456789</xsl:variable>

<xsl:variable name="mock">$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$</xsl:variable>

<xsl:variable name="replacement">
  <xsl:value-of select="substring($mock, 1, string-length($not-allowed-characters))"/>
</xsl:variable>

<xsl:choose>
  <xsl:when test="not(contains(translate(//FirstName, $not-allowed-characters, $replacement), '$'))">
    <xsl:value-of select="true()"/>
  </xsl:when>

  <xsl:otherwise>
    <xsl:value-of select="false()"/>
  </xsl:otherwise>

</xsl:choose>


来源:https://stackoverflow.com/questions/6555639/characters-only-check-in-xsl-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!