我是xml语言的新手,我有一个xml文件,我为该文件创建了xsd模式,但我的问题是如何在xml文件中引用这个模式。我的xml架构看起来像这样
<?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>
我将此文件保存为service.xsd。 我需要在我的xml文件中引用这个模式,我试过这样但是没有验证。
<?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>
我无法解决问题所在。它给出了这样的错误
No DTD of the document found
我试过这个
<?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>
但仍然是同样的问题。 当我使用xmlpad验证xml文件时。 任何人都可以解决我的问题。非常感谢
提前致谢。
答案 0 :(得分:1)
schemaLocation的使用完全是可选的,并且您的实例中的Version属性不正确(除非您在模式中定义了名为Version的属性)
以下实例
<Service xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://axis.com/service">
any string
</Service>
根据架构验证罚款:
<?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>
我所做的就是用字符串替换你的AxisServiceType类型。
为了确定您遇到故障的确切原因,我需要查看整个架构和实例文档。