xml 数据中的黑名单字符使用 xsd 模式

Blacklist characters in xml data using xsd pattern

我想这是一个基本问题,但模式似乎让我很困惑。 我想将一些字符列入黑名单,如 +、-、@、#、<、>。 如果字符在 xml 字段中,那么我想使 xml 无效。

在下面的示例中,如果字符串字段包含上述任何字符,我想将 return 和 xml 视为无效。 如果数据是这样的"hello"。它应该是有效的。 我应该如何写我的模式。感谢您检查此事。

XML:

<DataType>System.String</DataType>
<Value>
      <String>Data@yours</String>
</Value>

在XSD中:

<xs:element name="Value">
<xs:complexType>
<xs:sequence>
<xs:element name="String" type="xs:string" />
</xs:sequence>
<xs:restriction base="xs:string">
<xs:pattern value="[^+-@#%&()<>?]"/>
</xs:restriction>
</xs:complexType>
</xs:element>

进一步编辑这部分:

我遇到了这些错误: 解析 EntityName 时出错。 - 在我的代码中 在唯一的 xsd 验证器工具中,实体名称必须紧跟在实体引用中的 & 之后。 http://www.utilities-online.info/xsdvalidation/#.VzTwcYSDFBc

这些特殊字符后面好像没有,请问如何更改? 再次感谢。

请注意,acc。到 regular-expressions.info:

Particularly noteworthy is the complete absence of anchors like the caret and dollar, word boundaries, and lookaround. XML schema always implicitly anchors the entire regular expression. The regex must match the whole element for the element to be considered valid.

所以,要匹配不包含+@#%&(的字符串, ), <, >, ?, -(至少1个符号),需要使用

<xs:pattern value="[^+@#%&amp;()&lt;&gt;?-]+"/>

请注意,<>& 应该被实体化以在 XML 属性值中使用。

如果要允许空值,应使用 * 而不是 +。请注意,- 放在模式的末尾,以免创建这样的任何范围: