Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml ';仅限字符';签入xsl字符串?_Xml_Xslt_Xpath_Character - Fatal编程技术网

Xml ';仅限字符';签入xsl字符串?

Xml ';仅限字符';签入xsl字符串?,xml,xslt,xpath,character,Xml,Xslt,Xpath,Character,如何检查字符串是否仅包含字符(XSLT文件) 在XPath 1.0(XSLT 1.0)中,可以使用contains()。在XPath2.0(XSLT2.0)中,使用matches() 例如,您可能只需要检查字母字符(无数字、无其他符号、无空格): 或字母数字 matches(//FirstName, '^[a-zA-Z0-9]+$') 在XPath 1.0中,我建议使用此方法只允许FirstName 已更新: <xsl:variable name="not-allowed-charac

如何检查字符串是否仅包含字符(XSLT文件)


在XPath 1.0(XSLT 1.0)中,可以使用
contains()
。在XPath2.0(XSLT2.0)中,使用
matches()

例如,您可能只需要检查字母字符(无数字、无其他符号、无空格):

或字母数字

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

在XPath 1.0中,我建议使用此方法只允许
FirstName

已更新

<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>
<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>
string-length(translate($yourString, $allValidChars, '')) = 0
<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>
<s>abcDПЖЗ</s>
<s>abcd</s>

此XPath表达式

<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>
<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>
string-length(translate($yourString, $allValidChars, '')) = 0
<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>
<s>abcDПЖЗ</s>
<s>abcd</s>
$yourString
中的所有字符都包含在字符串
$allValidChars
中时,计算结果正好为
true()

否则,其计算结果为
false()

II。XPath 2.0解决方案--功能更强大

只要“有效”字符或“无效字符”都没有方便紧凑的表达式,就可以使用XPath 2.0的正则表达式功能。使用正则表达式和字符类可以编写以下代码:

matches($str,'^\p{L}+$')
只有当它完全由字母组成(所有unicode字符都是任何支持unicode的字母表中的字母)时,才匹配
$str

这里是一个基于XSLT 2.0的小型验证

<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>
<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>
string-length(translate($yourString, $allValidChars, '')) = 0
<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>
<s>abcDПЖЗ</s>
<s>abcd</s>

我既不使用XSLT 2.0,也不使用XPath 2.0,但我认为这是最合适的解决方案+1@polishchuk,在XSLT1.0中,您可以使用EXSLT regexp:match实现类似的方法。此外,我可以将函数嵌入XSLT中的C#(MSXML支持该功能)。但我喜欢XSLT“禁欲主义”和魔术:-)@polishchuk它不会真的可移植。@empo和@polishchuk:你们需要了解更多。XPath(1.0)提供了比您目前所知道的更多的功能。顺便说一句,我发布了一个答案,证明了这一点。:)好问题,+1。请参阅我的答案,了解一个完整、非常简单和简短的一行XPath 1.0表达式解决方案。这比使用正则表达式或多行XSLT要简单得多…@Dimitre,检测到的智能复制粘贴:-)@polishchuk:我们同意“智能”。至于复制/粘贴,目前还没有一种方法可以使解决方案缩短三倍,速度加快两倍。@polishchuk:FYI,我甚至还没有开始阅读您的多行、复杂的逻辑XSLT解决方案。。。至于谁复制了谁,你可以通过互联网搜索来验证这个解决方案是我知道的,并且我已经使用了很多年:)@Dimitre,我是指这个主题:-)@polishchuk:一个表达式怎么能与另一个表达式“完全相同”,它比另一个表达式长三倍,效率低两倍?快两倍?您的意思是
字符串长度
操作?:-)@是的,您已经正确地注意到,在建议的表达式中,
string-length()
出现了两次。但是你的解决方案最大的问题是根本无法使用。包含
$不允许字符的字符通常有数千个,在编写表达式时是未知的。我的解决方案的主要区别是只使用已知的有效字符。在不知道整个问题的情况下,断然说这是鲁莽的。@polishchuk:你误解了我的评论。它没有说明已知有效字符集很小。它只是说,当两个相互补充的集合中的一个很小并且可以方便地表达,但是我们需要使用它的补充(第二个集合),在某些情况下,有一种方便而有用的方法可以做到这一点,如“双翻译”方法所示。