验证填充元素但忽略空元素
Yaneeve 很友善地为我修复了我的架构,这导致了很多问题
这并不是什么大问题,但我很高兴能完成这项工作。
我的一些元素是空的,例如手机号码或传真号码。
我需要验证元素的长度以确保它们具有正确的位数,但我不想验证空元素
cvc-minLength-valid:值 '' 与 length = '0' 无效 尊重类型的 minLength '11' “移动”。
cvc-type.3.1.3:值 '' 元素“conmobile”无效。
有没有一种方法可以跳过空元素并仅在它不为空时验证它?
我的示例用户在 xml
<member>
<user id="4">
<personal>
<name>Connor Lawson</name>
<sex>Male</sex>
<address1>12 Ash Way</address1>
<address2></address2>
<city>Swindon</city>
<county>Wiltshire</county>
<postcode>SN3 6GS</postcode>
<telephone>01791928119</telephone>
<mobile>07338695664</mobile>
<email>[email protected]</email>
</personal>
<account>
<username>iTuneStinker</username>
<password>3a1f5fda21a07bfff20c41272bae7192</password>
<userlevel>3</userlevel>
<signupdate>2010-03-26T09:23:50</signupdate>
</account>
</user>
<festival id="1">
<event>
<eventname>Oxford Folk Festival</eventname>
<url>http://www.oxfordfolkfestival.com/</url>
<datefrom>2010-04-07</datefrom>
<dateto>2010-04-09</dateto>
<location>Oxford</location>
<eventpostcode>OX19BE</eventpostcode>
<coords>
<lat>51.735640</lat>
<lng>-1.276136</lng>
</coords>
</event>
<contact>
<conname>Stuart Vincent</conname>
<conaddress1>P.O. Box 642</conaddress1>
<conaddress2></conaddress2>
<concity>Oxford</concity>
<concounty>Bedfordshire</concounty>
<conpostcode>OX13BY</conpostcode>
<contelephone>01865 79073</contelephone>
<conmobile></conmobile>
<fax></fax>
<conemail>[email protected]</conemail>
</contact>
</festival>
</member>
和我的新架构中
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:simpleType name="postcode">
<xs:restriction base="xs:string">
<xs:minLength value="5"/>
<xs:maxLength value="8"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="telephone">
<xs:restriction base="xs:string">
<xs:minLength value="10"/>
<xs:maxLength value="13"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="mobile">
<xs:restriction base="xs:string">
<xs:minLength value="11"/>
<xs:maxLength value="11"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="password">
<xs:restriction base="xs:string">
<xs:minLength value="32"/>
<xs:maxLength value="32"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="userlevel">
<xs:restriction base="xs:integer">
<xs:enumeration value="1"/>
<xs:enumeration value="2"/>
<xs:enumeration value="3"/>
<xs:enumeration value="4"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="county">
<xs:restriction base="xs:string">
<xs:enumeration value="Bedfordshire"/>
<xs:enumeration value="Berkshire"/>
<xs:enumeration value="Bristol"/>
<xs:enumeration value="Buckinghamshire"/>
<xs:enumeration value="Cambridgeshire"/>
<xs:enumeration value="Cheshire"/>
<xs:enumeration value="Cleveland"/>
<xs:enumeration value="Cornwall"/>
<xs:enumeration value="Cumberland"/>
<xs:enumeration value="Derbyshire"/>
<xs:enumeration value="Devon"/>
<xs:enumeration value="Dorset"/>
<xs:enumeration value="Durham"/>
<xs:enumeration value="East Ridings Of Yorkshire"/>
<xs:enumeration value="Essex"/>
<xs:enumeration value="Gloucestershire"/>
<xs:enumeration value="Hampshire"/>
<xs:enumeration value="Herefordshire"/>
<xs:enumeration value="Hertfordshire"/>
<xs:enumeration value="Huntingdonshire"/>
<xs:enumeration value="Isle Of Man"/>
<xs:enumeration value="Kent"/>
<xs:enumeration value="Lancashire"/>
<xs:enumeration value="Leicestershire"/>
<xs:enumeration value="Lincolnshire"/>
<xs:enumeration value="London"/>
<xs:enumeration value="Middlesex"/>
<xs:enumeration value="Norfolk"/>
<xs:enumeration value="North Yorkshire"/>
<xs:enumeration value="Northamptonshire"/>
<xs:enumeration value="Northumberland"/>
<xs:enumeration value="Nottinghamshire"/>
<xs:enumeration value="Oxfordshire"/>
<xs:enumeration value="Rutland"/>
<xs:enumeration value="Shropshire"/>
<xs:enumeration value="Somerset"/>
<xs:enumeration value="South Yorkshire"/>
<xs:enumeration value="Staffordshire"/>
<xs:enumeration value="Suffolk"/>
<xs:enumeration value="Surrey"/>
<xs:enumeration value="Sussex"/>
<xs:enumeration value="Tyne and Wear"/>
<xs:enumeration value="Warwickshire"/>
<xs:enumeration value="West Yorkshire"/>
<xs:enumeration value="Westmorland"/>
<xs:enumeration value="Wiltshire"/>
<xs:enumeration value="Wirral"/>
<xs:enumeration value="Worcestershire"/>
<xs:enumeration value="Yorkshire"/>
</xs:restriction>
</xs:simpleType>
<xs:element name="folktask">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="unbounded" ref="member"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="member">
<xs:complexType>
<xs:sequence>
<xs:element ref="user" minOccurs="0" maxOccurs="unbounded"/>
<xs:element ref="festival" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="user">
<xs:complexType>
<xs:sequence>
<xs:element ref="personal"/>
<xs:element ref="account"/>
</xs:sequence>
<xs:attribute name="id" type="xs:integer"/>
</xs:complexType>
</xs:element>
<xs:element name="personal">
<xs:complexType>
<xs:sequence>
<xs:element ref="name"/>
<xs:element ref="sex"/>
<xs:element ref="address1"/>
<xs:element ref="address2"/>
<xs:element ref="city"/>
<xs:element ref="county"/>
<xs:element ref="postcode"/>
<xs:element ref="telephone"/>
<xs:element ref="mobile"/>
<xs:element ref="email"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="name" type="xs:string"/>
<xs:element name="sex" type="xs:string"/>
<xs:element name="address1" type="xs:string"/>
<xs:element name="address2" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="county" type="xs:string"/>
<xs:element name="postcode" type="postcode"/>
<xs:element name="telephone" type="telephone"/>
<xs:element name="mobile" type="mobile"/>
<xs:element name="email" type="xs:string"/>
<xs:element name="account">
<xs:complexType>
<xs:sequence>
<xs:element ref="username"/>
<xs:element ref="password"/>
<xs:element ref="userlevel"/>
<xs:element ref="signupdate"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="username" type="xs:string"/>
<xs:element name="password" type="password"/>
<xs:element name="userlevel" type="userlevel"/>
<xs:element name="signupdate" type="xs:dateTime"/>
<xs:element name="festival">
<xs:complexType>
<xs:sequence>
<xs:element ref="event"/>
<xs:element ref="contact"/>
</xs:sequence>
<xs:attribute name="id" type="xs:integer" use="optional"/>
</xs:complexType>
</xs:element>
<xs:element name="event">
<xs:complexType>
<xs:sequence>
<xs:element ref="eventname"/>
<xs:element ref="url"/>
<xs:element ref="datefrom"/>
<xs:element ref="dateto"/>
<xs:element ref="location"/>
<xs:element ref="eventpostcode"/>
<xs:element ref="coords"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="eventname" type="xs:string"/>
<xs:element name="url" type="xs:string"/>
<xs:element name="datefrom" type="xs:date"/>
<xs:element name="dateto" type="xs:date"/>
<xs:element name="location" type="xs:string"/>
<xs:element name="eventpostcode" type="postcode"/>
<xs:element name="coords">
<xs:complexType>
<xs:sequence>
<xs:element ref="lat"/>
<xs:element ref="lng"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="lat" type="xs:decimal"/>
<xs:element name="lng" type="xs:decimal"/>
<xs:element name="contact">
<xs:complexType>
<xs:sequence>
<xs:element ref="conname"/>
<xs:element ref="conaddress1"/>
<xs:element ref="conaddress2"/>
<xs:element ref="concity"/>
<xs:element ref="concounty"/>
<xs:element ref="conpostcode"/>
<xs:element ref="contelephone"/>
<xs:element ref="conmobile"/>
<xs:element ref="fax"/>
<xs:element ref="conemail"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="conname" type="xs:string"/>
<xs:element name="conaddress1" type="xs:string"/>
<xs:element name="conaddress2" type="xs:string"/>
<xs:element name="concity" type="xs:string"/>
<xs:element name="concounty" type="xs:string"/>
<xs:element name="conpostcode" type="postcode"/>
<xs:element name="contelephone" type="telephone"/>
<xs:element name="conmobile" type="mobile"/>
<xs:element name="fax" type="telephone"/>
<xs:element name="conemail" type="xs:string"/>
</xs:schema>
Yaneeve was kind enough to fix my schema for me which was causing lots of problems
This isn't so much of a problem but i would be nice to have this work
Some of my elements are empty such as a mobile number or fax number.
I need to validate the length of the element to make sure they have the correct number of digits but i don't want to validate an empty element
cvc-minLength-valid: Value '' with
length = '0' is not facet-valid with
respect to minLength '11' for type
'mobile'.cvc-type.3.1.3: The value '' of
element 'conmobile' is not valid.
Is there a way i can skip an empty element and validate it only if it is not empty?
My example user in the xml
<member>
<user id="4">
<personal>
<name>Connor Lawson</name>
<sex>Male</sex>
<address1>12 Ash Way</address1>
<address2></address2>
<city>Swindon</city>
<county>Wiltshire</county>
<postcode>SN3 6GS</postcode>
<telephone>01791928119</telephone>
<mobile>07338695664</mobile>
<email>[email protected]</email>
</personal>
<account>
<username>iTuneStinker</username>
<password>3a1f5fda21a07bfff20c41272bae7192</password>
<userlevel>3</userlevel>
<signupdate>2010-03-26T09:23:50</signupdate>
</account>
</user>
<festival id="1">
<event>
<eventname>Oxford Folk Festival</eventname>
<url>http://www.oxfordfolkfestival.com/</url>
<datefrom>2010-04-07</datefrom>
<dateto>2010-04-09</dateto>
<location>Oxford</location>
<eventpostcode>OX19BE</eventpostcode>
<coords>
<lat>51.735640</lat>
<lng>-1.276136</lng>
</coords>
</event>
<contact>
<conname>Stuart Vincent</conname>
<conaddress1>P.O. Box 642</conaddress1>
<conaddress2></conaddress2>
<concity>Oxford</concity>
<concounty>Bedfordshire</concounty>
<conpostcode>OX13BY</conpostcode>
<contelephone>01865 79073</contelephone>
<conmobile></conmobile>
<fax></fax>
<conemail>[email protected]</conemail>
</contact>
</festival>
</member>
and my new schema
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:simpleType name="postcode">
<xs:restriction base="xs:string">
<xs:minLength value="5"/>
<xs:maxLength value="8"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="telephone">
<xs:restriction base="xs:string">
<xs:minLength value="10"/>
<xs:maxLength value="13"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="mobile">
<xs:restriction base="xs:string">
<xs:minLength value="11"/>
<xs:maxLength value="11"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="password">
<xs:restriction base="xs:string">
<xs:minLength value="32"/>
<xs:maxLength value="32"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="userlevel">
<xs:restriction base="xs:integer">
<xs:enumeration value="1"/>
<xs:enumeration value="2"/>
<xs:enumeration value="3"/>
<xs:enumeration value="4"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="county">
<xs:restriction base="xs:string">
<xs:enumeration value="Bedfordshire"/>
<xs:enumeration value="Berkshire"/>
<xs:enumeration value="Bristol"/>
<xs:enumeration value="Buckinghamshire"/>
<xs:enumeration value="Cambridgeshire"/>
<xs:enumeration value="Cheshire"/>
<xs:enumeration value="Cleveland"/>
<xs:enumeration value="Cornwall"/>
<xs:enumeration value="Cumberland"/>
<xs:enumeration value="Derbyshire"/>
<xs:enumeration value="Devon"/>
<xs:enumeration value="Dorset"/>
<xs:enumeration value="Durham"/>
<xs:enumeration value="East Ridings Of Yorkshire"/>
<xs:enumeration value="Essex"/>
<xs:enumeration value="Gloucestershire"/>
<xs:enumeration value="Hampshire"/>
<xs:enumeration value="Herefordshire"/>
<xs:enumeration value="Hertfordshire"/>
<xs:enumeration value="Huntingdonshire"/>
<xs:enumeration value="Isle Of Man"/>
<xs:enumeration value="Kent"/>
<xs:enumeration value="Lancashire"/>
<xs:enumeration value="Leicestershire"/>
<xs:enumeration value="Lincolnshire"/>
<xs:enumeration value="London"/>
<xs:enumeration value="Middlesex"/>
<xs:enumeration value="Norfolk"/>
<xs:enumeration value="North Yorkshire"/>
<xs:enumeration value="Northamptonshire"/>
<xs:enumeration value="Northumberland"/>
<xs:enumeration value="Nottinghamshire"/>
<xs:enumeration value="Oxfordshire"/>
<xs:enumeration value="Rutland"/>
<xs:enumeration value="Shropshire"/>
<xs:enumeration value="Somerset"/>
<xs:enumeration value="South Yorkshire"/>
<xs:enumeration value="Staffordshire"/>
<xs:enumeration value="Suffolk"/>
<xs:enumeration value="Surrey"/>
<xs:enumeration value="Sussex"/>
<xs:enumeration value="Tyne and Wear"/>
<xs:enumeration value="Warwickshire"/>
<xs:enumeration value="West Yorkshire"/>
<xs:enumeration value="Westmorland"/>
<xs:enumeration value="Wiltshire"/>
<xs:enumeration value="Wirral"/>
<xs:enumeration value="Worcestershire"/>
<xs:enumeration value="Yorkshire"/>
</xs:restriction>
</xs:simpleType>
<xs:element name="folktask">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="unbounded" ref="member"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="member">
<xs:complexType>
<xs:sequence>
<xs:element ref="user" minOccurs="0" maxOccurs="unbounded"/>
<xs:element ref="festival" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="user">
<xs:complexType>
<xs:sequence>
<xs:element ref="personal"/>
<xs:element ref="account"/>
</xs:sequence>
<xs:attribute name="id" type="xs:integer"/>
</xs:complexType>
</xs:element>
<xs:element name="personal">
<xs:complexType>
<xs:sequence>
<xs:element ref="name"/>
<xs:element ref="sex"/>
<xs:element ref="address1"/>
<xs:element ref="address2"/>
<xs:element ref="city"/>
<xs:element ref="county"/>
<xs:element ref="postcode"/>
<xs:element ref="telephone"/>
<xs:element ref="mobile"/>
<xs:element ref="email"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="name" type="xs:string"/>
<xs:element name="sex" type="xs:string"/>
<xs:element name="address1" type="xs:string"/>
<xs:element name="address2" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="county" type="xs:string"/>
<xs:element name="postcode" type="postcode"/>
<xs:element name="telephone" type="telephone"/>
<xs:element name="mobile" type="mobile"/>
<xs:element name="email" type="xs:string"/>
<xs:element name="account">
<xs:complexType>
<xs:sequence>
<xs:element ref="username"/>
<xs:element ref="password"/>
<xs:element ref="userlevel"/>
<xs:element ref="signupdate"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="username" type="xs:string"/>
<xs:element name="password" type="password"/>
<xs:element name="userlevel" type="userlevel"/>
<xs:element name="signupdate" type="xs:dateTime"/>
<xs:element name="festival">
<xs:complexType>
<xs:sequence>
<xs:element ref="event"/>
<xs:element ref="contact"/>
</xs:sequence>
<xs:attribute name="id" type="xs:integer" use="optional"/>
</xs:complexType>
</xs:element>
<xs:element name="event">
<xs:complexType>
<xs:sequence>
<xs:element ref="eventname"/>
<xs:element ref="url"/>
<xs:element ref="datefrom"/>
<xs:element ref="dateto"/>
<xs:element ref="location"/>
<xs:element ref="eventpostcode"/>
<xs:element ref="coords"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="eventname" type="xs:string"/>
<xs:element name="url" type="xs:string"/>
<xs:element name="datefrom" type="xs:date"/>
<xs:element name="dateto" type="xs:date"/>
<xs:element name="location" type="xs:string"/>
<xs:element name="eventpostcode" type="postcode"/>
<xs:element name="coords">
<xs:complexType>
<xs:sequence>
<xs:element ref="lat"/>
<xs:element ref="lng"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="lat" type="xs:decimal"/>
<xs:element name="lng" type="xs:decimal"/>
<xs:element name="contact">
<xs:complexType>
<xs:sequence>
<xs:element ref="conname"/>
<xs:element ref="conaddress1"/>
<xs:element ref="conaddress2"/>
<xs:element ref="concity"/>
<xs:element ref="concounty"/>
<xs:element ref="conpostcode"/>
<xs:element ref="contelephone"/>
<xs:element ref="conmobile"/>
<xs:element ref="fax"/>
<xs:element ref="conemail"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="conname" type="xs:string"/>
<xs:element name="conaddress1" type="xs:string"/>
<xs:element name="conaddress2" type="xs:string"/>
<xs:element name="concity" type="xs:string"/>
<xs:element name="concounty" type="xs:string"/>
<xs:element name="conpostcode" type="postcode"/>
<xs:element name="contelephone" type="telephone"/>
<xs:element name="conmobile" type="mobile"/>
<xs:element name="fax" type="telephone"/>
<xs:element name="conemail" type="xs:string"/>
</xs:schema>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我设法使用 < 来修复它xs:联合>
从此
改为
I managed to fix it by using < xs:union >
chnaged from this
to
您尝试过模式限制吗?像这样的内容:
注意:未经实际 XML 测试。
Did you try pattern restrictions? Something like this:
Caution: untested with actual XML.