xml - How to fix xsd schema validation errors? -
i trying write xsd schema based on example msdn. have modified example xsd local xml file , not use namespace. however, xml fails validate.
xsd file (sys_params.xsd):
<xs:schema xmlns:xs="http://www.w3.org/2001/xmlschema" targetnamespace="http://tempuri.org/po.xsd" xmlns="http://tempuri.org/po.xsd" elementformdefault="qualified"> <xs:annotation> <xs:documentation xml:lang="en"> purchase order schema example.com. copyright 2000 example.com. rights reserved. </xs:documentation> </xs:annotation> <xs:element name="purchaseorder" type="purchaseordertype"/> <xs:element name="comment" type="xs:string"/> <xs:complextype name="purchaseordertype"> <xs:sequence> <xs:element name="shipto" type="usaddress"/> <xs:element name="billto" type="usaddress"/> <xs:element ref="comment" minoccurs="0"/> <xs:element name="items" type="items"/> </xs:sequence> <xs:attribute name="orderdate" type="xs:date"/> </xs:complextype> <xs:complextype name="usaddress"> <xs:annotation> <xs:documentation> purchase order schema example.microsoft.com. copyright 2001 example.microsoft.com. rights reserved. </xs:documentation> <xs:appinfo> application info. </xs:appinfo> </xs:annotation> <xs:sequence> <xs:element name="name" type="xs:string"/> <xs:element name="street" type="xs:string"/> <xs:element name="city" type="xs:string"/> <xs:element name="state" type="xs:string"/> <xs:element name="zip" type="xs:decimal"/> </xs:sequence> <xs:attribute name="country" type="xs:nmtoken" fixed="us"/> </xs:complextype> <xs:complextype name="items"> <xs:sequence> <xs:element name="item" minoccurs="0" maxoccurs="unbounded"> <xs:complextype> <xs:sequence> <xs:element name="productname" type="xs:string"/> <xs:element name="quantity"> <xs:simpletype> <xs:restriction base="xs:positiveinteger"> <xs:maxexclusive value="100"/> </xs:restriction> </xs:simpletype> </xs:element> <xs:element name="usprice" type="xs:decimal"/> <xs:element ref="comment" minoccurs="0"/> <xs:element name="shipdate" type="xs:date" minoccurs="0"/> </xs:sequence> <xs:attribute name="partnum" type="sku" use="required"/> </xs:complextype> </xs:element> </xs:sequence> </xs:complextype> <!-- stock keeping unit, code identifying products --> <xs:simpletype name="sku"> <xs:restriction base="xs:string"> <xs:pattern value="\d{3}-[a-z]{2}"/> </xs:restriction> </xs:simpletype> </xs:schema>
xml file (sys_params.xml):
<?xml version="1.0" encoding="utf-8"?> <purchaseorder xmlns="http://www.w3.org/2001/xmlschema-instance" xsi:nonamespaceschemalocation="sys_params.xsd" orderdate="1999-10-20"> <shipto country="us"> <name>alice smith</name> <street>123 maple street</street> <city>mill valley</city> <state>ca</state> <zip>90952</zip> </shipto> <billto country="us"> <name>robert smith</name> <street>8 oak avenue</street> <city>old town</city> <state>pa</state> <zip>95819</zip> </billto> <comment>hurry, lawn going wild!</comment> <items> <item partnum="872-aa"> <productname>lawnmower</productname> <quantity>1</quantity> <usprice>148.95</usprice> <comment>confirm electric</comment> </item> <item partnum="926-aa"> <productname>baby monitor</productname> <quantity>1</quantity> <usprice>39.98</usprice> <shipdate>1999-05-21</shipdate> </item> </items> </purchaseorder>
line 3 of xml file fails validate:
<purchaseorder xmlns="http://www.w3.org/2001/xmlschema-instance" xsi:nonamespaceschemalocation="leda_sys_params.xsd" orderdate="1999-10-20">
the error is:
error prefix 'xsi' can not resolved namespace uri
how can fix please?
you not allowed add <xsd:element>
directly under <xsd:complextype>
.
assuming want have first <shipto>
tag, <item>
tag, use <xsd:sequence>
gathering tag constitute content of order
type:
<xsd:complextype name="order"> <xsd:sequence> <xsd:element name="shiptos" type="shipaddress"/> <xsd:element name="items" type="cditems"/> </xsd:sequence> </xsd:complextype>
the fixed version of schema turn to:
<?xml version="1.0" encoding="utf-8"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/xmlschema" elementformdefault="qualified"> <xsd:element name="root"/> <xsd:element name="shiporder" type="order"/> <xsd:complextype name="order"> <xsd:sequence> <xsd:element name="shipto" type="shipaddress"/> <xsd:element name="items" type="cditems"/> </xsd:sequence> </xsd:complextype> <xsd:complextype name="shipaddress"> <xsd:sequence> <xsd:element name="name" type="xsd:string"/> <xsd:element name="street" type="xsd:string"/> <xsd:element name="address" type="xsd:string"/> <xsd:element name="country" type="xsd:string"/> </xsd:sequence> </xsd:complextype> <xsd:complextype name="cditems"> <xsd:sequence> <xsd:element name="item" minoccurs="0" maxoccurs="unbounded" type="cditem"/> </xsd:sequence> </xsd:complextype> <xsd:complextype name="cditem"> <xsd:sequence> <xsd:element name="title" type="xsd:string"/> <xsd:element name="quantity" type="xsd:positiveinteger"/> <xsd:element name="price" type="xsd:decimal"/> </xsd:sequence> </xsd:complextype> </xsd:schema>
note: have not bound <root>
element type of content...
Comments
Post a Comment