التحقق من صحة العناصر المملوءة ولكن تجاهل العناصر الفارغة

StackOverflow https://stackoverflow.com/questions/2629480

  •  26-09-2019
  •  | 
  •  

سؤال

كان Yaneeve لطيفًا بما يكفي لإصلاح مخططي بالنسبة لي الذي كان يسبب الكثير من المشاكل

هذه ليست مشكلة كبيرة ولكن سيكون من الجيد الحصول على هذا العمل

بعض عناصري فارغة مثل رقم الهاتف المحمول أو رقم الفاكس.

أحتاج إلى التحقق من صحة طول العنصر للتأكد من أن لديهم العدد الصحيح من الأرقام ولكني لا أريد التحقق من صحة عنصر فارغ

CVC-Minlength-Valid: القيمة "" مع الطول = "0" ليست ذات جوانب من الوجه فيما يتعلق بطول الطول "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>itunestinker@fsmail.co.uk</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>stewards@oxfordfolkfestival.com</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>
هل كانت مفيدة؟

المحلول

تمكنت من إصلاحه باستخدام <XS: Union>

Chnaged من هذا

<xs:simpleType name="telephone">
    <xs:restriction base="xs:string">
        <xs:minLength value="10"/>
        <xs:maxLength value="13"/>
    </xs:restriction>
</xs:simpleType>

ل

<xs:simpleType name="telephone">
  <xs:union>
    <xs:simpleType>
      <xs:restriction base="xs:string">
        <xs:minLength value="10"/>
        <xs:maxLength value="13"/>
      </xs:restriction>
    </xs:simpleType>
    <xs:simpleType>
      <xs:restriction base="xs:string">
        <xs:enumeration value=""/>
      </xs:restriction>
    </xs:simpleType>
  </xs:union>
</xs:simpleType>

نصائح أخرى

هل جربت قيود الأنماط؟ شيء من هذا القبيل:

<xs:simpleType name="telephone">
  <xs:restriction base="xs:string">
    <xs:pattern value="[0-9]{10,13}|.{0}/>
  </xs:restriction>
</xs:simpleType>

تحذير: لم يتم اختباره مع XML الفعلي.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top