如何验证 xml 架构中的电子邮件 ID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2147780/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 12:56:43  来源:igfitidea点击:

How to validate an email id in xml schema

xmlvalidationemailschema

提问by Sunil Kumar Sahoo

Hi I have created a schema to check for email id. which can validate if the email id is [email protected] and [email protected] and [email protected] But i want to validate only [email protected] and [email protected] because i think email can have maximum 2 dots after @ symbol so the third one will be invalid email id So how to validate an email id using schema Below is the schema

嗨,我创建了一个模式来检查电子邮件 ID。它可以验证电子邮件 ID 是否为 [email protected][email protected][email protected] 但我只想验证 [email protected][email protected]因为我认为电子邮件在@ 符号后最多可以有 2 个点,所以第三个将是无效的电子邮件 ID 那么如何使用架构验证电子邮件 ID 下面是架构

<xsd:element name="SSEM" minOccurs="0">
    <xsd:complexType>
        <xsd:sequence>
            <xsd:element name="CNT" minOccurs="0" maxOccurs="unbounded">
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:element name="EM" minOccurs="1" nillable="true" type ="singleEmailID"/>
                    </xsd:sequence>
                </xsd:complexType>
            </xsd:element>
        </xsd:sequence>
    </xsd:complexType>
</xsd:element>

Thanks Sunil Kumar Sahoo

感谢 Sunil Kumar Sahoo

回答by Jeff Yates

You will need to define a pattern to match against valid e-mails. Patterns are defined using regular expression syntax. Once you have defined a simple type (based on xs:string) with the appropriate pattern, you can use that for your e-mail type.

您需要定义一个模式来匹配有效的电子邮件。模式是使用正则表达式语法定义的。一旦您xs:string使用适当的模式定义了一个简单类型(基于),您就可以将其用于您的电子邮件类型。

There are several places on the Internet that provide some examples of such types and patterns. An example of an e-mail type is provided here.

Internet 上有几个地方提供了此类类型和模式的一些示例。此处提供电子邮件类型的示例。

The example given there is as follows (I've edited it slightly to make things a little clearer):

给出的示例如下(我稍微编辑了它以使事情更清楚一些):

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" > 

  <xsd:element name="A" type="emailAddress"/> 

  <xsd:simpleType name="emailAddress"> 
    <xsd:restriction base="xsd:string"> 
      <xsd:pattern value="[^@]+@[^\.]+\..+"/> 
    </xsd:restriction> 
  </xsd:simpleType> 
</xsd:schema>

回答by marc_s

You could use a <xs:simpleType>based on a string and with a regex pattern to validate your e-mail addresses:

您可以使用<xs:simpleType>基于字符串和正则表达式模式的 a 来验证您的电子邮件地址:

<xsd:simpleType name="emailAddress">
    <xsd:restriction base="xsd:string">
        <xsd:pattern value="([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})"/>
    </xsd:restriction>
</xsd:simpleType>

Use any e-mail regex you like :-), see some samples on RegexLib.Net.

使用您喜欢的任何电子邮件正则表达式 :-),请参阅RegexLib.Net上的一些示例。

Then, use that type in your basic XML schema:

然后,在您的基本 XML 模式中使用该类型:

<xsd:element name="email" type="emailAddress" />

Could by checked by online validator: https://www.corefiling.com/opensource/schemaValidate.html

可以通过在线验证器检查:https: //www.corefiling.com/opensource/schemaValidate.html

回答by Sunil Kumar Sahoo

Use the below schema validator for email id validation

使用以下架构验证器进行电子邮件 ID 验证

<xsd:simpleType name="emailAddress">
    <xsd:restriction base="xsd:string">
        <xsd:pattern value="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"/>
    </xsd:restriction>
</xsd:simpleType>

回答by Shans Muga

Use this validator for email validation with apostrophe also:

也使用此验证器进行带有撇号的电子邮件验证:

<xsd:simpleType name="emailAddress">
    <xsd:restriction base="xsd:string">
        <xsd:pattern value="^([0-9a-zA-Z_\.\'\-]+)*@[0-9a-zA-Z\-]+[a-zA-Z\.]+Dollar symbol"/>
    </xsd:restriction>
</xsd:simpleType>

It will work as email validation with apostrophe:-)

它将用作带有撇号的电子邮件验证:-)