【问题标题】:reference an xsd file in xml在 xml 中引用一个 xsd 文件
【发布时间】:2012-01-19 06:17:10
【问题描述】:

我是 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 文件时。 任何人都可以解决我的问题吗?任何帮助表示赞赏

提前致谢。

【问题讨论】:

  • +1:我见过的最好的第一个帖子之一!
  • 您能描述一下您是如何验证 XML 的吗?你使用一些外部程序还是使用一些库?
  • @bbaja42 我正在使用 XMLPAD 进行验证。

标签: xml xsd


【解决方案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 替换为字符串。

为了确定您遇到的失败的确切原因,我需要查看您的整个架构和实例文档。

【讨论】:

  • 感谢您的回复,我正在为不同的 xml 文件格式编写一个 xsd,SWService、HWService、ModuleSpecification 涉及不同的 xml 文件,并且 SWService xml 文件 xsd 在 SWServiceType 下被引用。同样适用于三个不同的 xml文件。我编辑了我的帖子可以看看我编辑的帖子。我需要以三种不同的 xml 文件格式引用这个 xsd。你能像那样帮助我吗?如果我在没有命名空间的情况下引用这个 xsd,它的验证,但是命名空间它给出错误。
  • 很抱歉,您必须向我提供整个 XSD 架构,以便我可以帮助您构建实例 XML 文档。
  • 你能看看我的 xsd,我提供了整个 xsd 架构。
  • 谢谢 - 你能发布你的三种不同的 xml 文件格式吗?然后,我可以帮助您了解为什么当您从架构中包含名称空间时这些都没有验证。然后,我将更新我的原始答案以包含此内容。
猜你喜欢
  • 1970-01-01
  • 2014-06-17
  • 1970-01-01
  • 1970-01-01
  • 2011-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多