Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reference an xsd file in xml

Tags:

xml

xsd

I am new to xml language, I have one xml file and I created xsd schema for that file, but my problem is how to reference this schema in xml file. My xml schema look like this

<?xml version="1.0" encoding="utf-8"?>
 <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:wmh="http://www.wmhelp.com/2003/eGenerator"
    elementFormDefault="qualified"
    targetNamespace="http://axis.com/service"
    xmlns="http://axis.com/service"
    version="1.0">

  <xs:element name="SWService" type="SWServiceType"/>
  <xs:element name="HWService" type="HWServiceType"/>


 <xs:complexType name="SWServiceType">
<xs:sequence>
  <xs:element name="Service" type="ServiceType" minOccurs="0" maxOccurs="unbounded"/>
  </xs:sequence>
  </xs:complexType>

 <xs:complexType name="ServiceType">
 <xs:complexContent>
 <xs:extension base="IdType">
  <xs:sequence>
    <xs:element name="Description" type="xs:string" maxOccurs="1" minOccurs="0"/>
    <xs:element name="ServiceCustomers" type="ServiceCustomersType" maxOccurs="1" 
 minOccurs="0"/>
    <xs:element name="ServiceSuppliers" type="ServiceSuppliersType" maxOccurs="1" 
 minOccurs="0"/>
  </xs:sequence>
  <xs:attribute name="Name" type="xs:string" use="required"/>
  </xs:extension>
  </xs:complexContent>
   </xs:complexType>

    <xs:complexType name="HWServiceType">
  <xs:sequence>
    <xs:element name="element" type="elementGroupType" maxOccurs="1" minOccurs="0"/>
  </xs:sequence>
  </xs:complexType>

  <xs:complexType name="ServiceCustomersType">
  <xs:sequence>
    <xs:element name="SoftWare" type="SoftWareType" maxOccurs="unbounded" 
  minOccurs="0"/>
  </xs:sequence>
  </xs:complexType>

  <xs:complexType name="ServiceSuppliersType">
  <xs:sequence>
    <xs:element name="SoftWare" type="SoftWareType" maxOccurs="unbounded" 
 minOccurs="0"/>
    <xs:element name="HardWare" type="HardWareType" maxOccurs="unbounded" 
  minOccurs="0"/>
  </xs:sequence>
 </xs:complexType>

 <xs:complexType name="SoftWareType">
 <xs:complexContent>
 <xs:extension base="PathType">
  <xs:attribute name="Service" type="xs:string" use="required"/>
    </xs:extension>
     </xs:complexContent>
 </xs:complexType>

 <xs:complexType name="HardWareType">
 <xs:complexContent>
 <xs:extension base="PathType">
  <xs:attribute name="Type" type="xs:string" use="required"/>
  <xs:attribute name="Nr" type="xs:string" use="required"/>
  <xs:attribute name="Service" type="xs:string" use="required"/>
    </xs:extension>
     </xs:complexContent>
 </xs:complexType>

<xs:complexType name="PathType">
  <xs:attribute name="Path" type="xs:string" use="required"/>
</xs:complexType>


   <xs:complexType name="elementGroupType">
 <xs:sequence>
       <xs:element name="element" type="elementType" maxOccurs="unbounded" 
 minOccurs="1"/>
  </xs:sequence>
 </xs:complexType>


 <xs:complexType name="elementType">
  <xs:sequence>
    <xs:element name="LM" type="LMType2" maxOccurs="1" minOccurs="1"/>
    <xs:element name="Service" type="ServiceType" maxOccurs="unbounded" minOccurs="0"/>
  </xs:sequence>
  <xs:attribute name="Type" type="xs:string" use="required"/>
  <xs:attribute name="Nr" type="xs:string" use="required"/>
  <xs:attribute name="Name" type="xs:string" use="required"/>
 </xs:complexType>


  <xs:complexType name="LMType2">
  <xs:sequence>
    <xs:element name="LowerMode" type="LowerModeType2" maxOccurs="unbounded" 
 minOccurs="0"/>
  </xs:sequence>
 </xs:complexType>

 <xs:complexType name="LowerModeType2">
 <xs:complexContent>
  <xs:extension base="IdType">
    <xs:attribute name="Probability" type="xs:double" use="required"/>
  </xs:extension>
  </xs:complexContent>
  </xs:complexType>


 <xs:complexType name="IdType">
  <xs:attribute name="Id" type="xs:string" use="required"/>
 </xs:complexType>

  </xs:schema>

I saved this file as as service.xsd. I need to reference this schema in my xml file, i tried like this but it not validating.

<?xml version="1.0" encoding="UTF-8"?>
<Service xsi:schemaLocation="file:///C:/main/newfolder/service.xsd"       
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://axis.com/service"        
 Version="1.0">
--------Xml data-------
 </Service>

I cant under what is the problem. It gives error like this

  No DTD of the document found 

I tried like this

<?xml version="1.0" encoding="UTF-8"?>
<Service xsi:schemaLocation=""http://axis.com/service"       
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://axis.com/service"        
 Version="1.0">
--------Xml data-------
 </Service>

but still same problem. when I validate xml file using xmlpad. Can any one fix my problem.any help appreciated

Thanks in advance.

like image 571
user234 Avatar asked Dec 13 '11 11:12

user234


People also ask

Is an XSD an XML file?

XML Schema Definition or XSD is a recommendation by the World Wide Web Consortium (W3C) to describe and validate the structure and content of an XML document. It is primarily used to define the elements, attributes and data types the document can contain.

How does XSD work with XML?

XSD is based and written on XML. XSD defines elements and structures that can appear in the document, while XML does not. XSD ensures that the data is properly interpreted, while XML does not. An XSD document is validated as XML, but the opposite may not always be true.

Can we generate XML from XSD?

To create an XML file from an XSD file: Right-click the XML Documents folder in your data development project, and select New > XML. The New XML File wizard opens. Select the XMLSchema project in the parent folder field, type customer.

How do you specify the schema of XML file?

Create an XML Schema To create the schema we could simply follow the structure in the XML document and define each element as we find it. We will start with the standard XML declaration followed by the xs:schema element that defines a schema: <? xml version="1.0" encoding="UTF-8" ?>


1 Answers

The use of schemaLocation is entirely optional and the Version attribute is incorrect in your instance (unless you have defined an attribute called Version in your schema)

The following instance

<Service xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://axis.com/service">
any string
</Service>

Validates fine against the schema:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:wmh="http://www.wmhelp.com/2003/eGenerator"
    elementFormDefault="qualified"
    targetNamespace="http://axis.com/service"
    xmlns="http://axis.com/service"
    version="1.0">
  <xs:element name="Service" type="xs:string"/>
</xs:schema>

All I did was replace your type AxisServiceType with a string.

In order to determine the exact cause of the failure you are having I would need to see your entire schema and instance document.

like image 128
tom redfern Avatar answered Oct 26 '22 22:10

tom redfern