[英]XSLT regular expression to accept only few characters in a string
I am writing a regular expression which will allow only below special characters: 我正在写一个正则表达式,该表达式仅允许以下特殊字符:
- _ * & . , #
I wrote below function which will avoid all the characters except mentioned in below pattern: 我写了下面的函数,它将避免除以下模式中提到的所有字符:
<xsl:function name="wd:allowed_characters">
<xsl:param name="input_param" />
<xsl:if test="$input_param !=' '" >
<xsl:value-of select="normalize-space(replace($input_param,'[^.#, \- _ * a-zA-Z0-9]',''))" />
</xsl:if>
</xsl:function>
My problem is whenever I try to add &
in the pattern at any place, I got below errors: 我的问题是,每当我尝试在任意位置添加
&
时,都会出现以下错误:
Severity: fatal
严重程度:致命
Description: The entity name must immediately follow the '&' in the entity描述:实体名称必须紧跟在实体中的“&”之后
Severity: error
严重性:错误
Description: Failed to compile stylesheet.说明:无法编译样式表。 1 error detected.
检测到1个错误。
I want to know about how can I add &
in the pattern like other special characters? 我想知道如何像其他特殊字符一样在样式中添加
&
?
XSLT is written in XML, so the source code of your XSLT stylesheet must be well-formed (and generally also valid) XML. XSLT是用XML编写的,因此XSLT样式表的源代码必须是格式正确的XML(并且通常也是有效的)。 In XML there are five special characters:
<
, >
, &
, "
, '
which roughly can only be used as follows: 在XML中,有五个特殊字符:
<
, >
, &
, "
, '
,大致只能按如下方式使用:
test=" "foo" "
. test=" "foo" "
。 Often you can write an attribute value by surrounding it with the other quote: test=' "foo" '
or test=" 'foo' "
are both valid. test=' "foo" '
或test=" 'foo' "
都是有效的。 In XPath, which is typically written in XSLT inside an attribute value, this is a common way to write string literals (this is what you are already doing in your code above). <
and &
in <
<
和&
在<
and &
&
respectively. >
never needs to be escaped, but many people do. >
永远不需要逃脱,但是很多人都需要逃脱。 <
<
, >
>
, &
&
, "
"
and '
'
, other named entity references first need to be declared in a DTD (as is often done for
).
所做的)。 CDATA
section (and in a comment) you do not need to escape any of these characters: <![CDATA[<hello>&]]>
is exactly the same as <hello>&
CDATA
部分内(并在注释中),您无需转义以下任何字符: <![CDATA[<hello>&]]>
与<hello>&
完全相同<hello>&
. It is often confusing. 这常常令人困惑。 If the source document contains
<
如果源文档包含
<
in the XML, you won't be able to find it by comparing it to a string <
在XML中,您将无法通过将其与字符串
<
, because essentially it's just the <
character. ,因为本质上它只是
<
字符。 Instead you must search for <
. 相反,您必须搜索
<
。 However, since XSLT is written in XML, writing <xsl:if test="contains(.,
< )"
will search for the <
character, not the four-character string <
但是,由于XSLT是用XML编写的,因此编写
<xsl:if test="contains(.,
< )"
将搜索<
字符,而不是四字符字符串<
. 。
In regard to your question, you can write your expression simply as follows: 关于您的问题,您可以简单地按以下方式编写表达式:
replace($input_param,'[^&.#,_*a-zA-Z0-9-]','')
-
at the end, where it does not require escaping -
放在不需要转义的结尾处 xsl:if
is redundant: the normalize-space
will make a string consisting of only spaces the same as the empty string. xsl:if
是多余的: normalize-space
将创建一个仅包含与空字符串相同的空格的字符串。 With or without the xsl:if
will have the same results xsl:if
的结果相同 Note: because of the complexities of escaping, quote-issues and other things, it is common to write a regular expression in a variable's sequence constructor, to prevent those issues to happen in the first place (added the x-modifier to allow whitespace in the regex): 注意:由于转义,引号引起的问题和其他因素的复杂性,通常在变量的序列构造函数中编写一个正则表达式,以防止这些问题首先发生(添加了x-修饰符以允许空格正则表达式):
<xsl:variable name='regex' as='xs:string'>
[^&.#,_*a-zA-Z0-9-]
</xsl:variable>
<xsl:function name="wd:allowed_characters" as="xs:string">
<xsl:param name="input_param" as="xs:string" />
<xsl:value-of select="
normalize-space(
replace($input_param, $regex, '', 'x'))" />
</xsl:function>
According to my knowledge, in XML, we can't use direct key of '&', which should be combination of '&[A-z0-9]+;'. 据我所知,在XML中,我们不能使用'&'的直接键,它应该是'&[A-z0-9] +;'的组合。 In XML we can use either '&'
在XML中,我们可以使用'&amp;' or '&'
或“&#x0026;” entity formats.
实体格式。 In regex '[^.#, &&- _ * a-zA-Z0-9]' can be used.
在正则表达式中可以使用'[^。#,&amp;&#x0026;-_ * a-zA-Z0-9]。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.