2013-04-28 65 views
0

之间有什么区别(上前缀^ h焦点:, C:一:和默认):进口模式和命名空间

<?xml version="1.0" encoding="UTF-8"?> 
<xmlBoo xmlns="http://www.example.org/boo" 
     xmlns:c="http://www.example.org/customer" 
     xmlns:a="http://www.example.org/address" 
     xmlns:h="http://www.example.org/header"> 
    <h:header> 
     <h:id>101</h:id> 
    </h:header> 
    <c:customer> 
     <c:id>1</c:id> 
     <c:name>John</c:name> 
     <a:address> 
     <a:street>Long street</a:street> 
     </a:address> 
    </c:customer> 
    <someBooSpecificField>Specific data in Boo</someBooSpecificField> 
</xmlBoo> 

<?xml version="1.0" encoding="UTF-8"?> 
<xmlBoo xmlns="http://www.example.org/boo" 
     xmlns:c="http://www.example.org/customer" 
     xmlns:a="http://www.example.org/address" 
     xmlns:h="http://www.example.org/header"> 
    <header> 
     <h:id>101</h:id> 
    </header> 
    <customer> 
     <c:id>1</c:id> 
     <c:name>John</c:name> 
     <address> 
     <a:street>Long street</a:street> 
     </address> 
    </customer> 
    <someBooSpecificField>Specific data in Boo</someBooSpecificField> 
</xmlBoo> 

我问我原因我已经通过JAXB和marshaller实现了映射,创建了第一个xml。但是当我添加模式验证时,我得到了异常:

cvc-complex-type.2.4.a: Invalid content was found starting with element 'ns3:header'. One of '{"http://www.example.org/boo":header}' is expected. 

看起来,验证器期望(基于模式)第二个XML。

哪个前缀应该是元素头之前? 默认(与命名空间连通[http://www.example.org/boo])或H:(与命名空间连通[http://www.example.org/header])。

我认为整个元素标题与h完全连接:不仅仅是像第二个例子中的子元素。最佳实践是什么,对此的解释是什么。

我的XML模式:

<?xml version="1.0" encoding="UTF-8"?> 
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
      xmlns:h="http://www.example.org/header" 
      xmlns:c="http://www.example.org/customer" 
      targetNamespace="http://www.example.org/boo" 
      elementFormDefault="qualified"> 

    <xsd:import namespace="http://www.example.org/header" schemaLocation="header.xsd"/> 
    <xsd:import namespace="http://www.example.org/customer" schemaLocation="customer.xsd"/> 

    <xsd:element name="xmlBoo"> 
     <xsd:complexType> 
      <xsd:sequence> 
       <xsd:element name="header" type="h:header"/> 
       <xsd:element name="customer" type="c:customer"/> 
       <xsd:element name="someBooSpecificField" type="xsd:string"/> 
      </xsd:sequence> 
     </xsd:complexType> 
    </xsd:element> 

</xsd:schema> 

和EG。 header.xsd

<?xml version="1.0" encoding="UTF-8"?> 
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
      targetNamespace="http://www.example.org/header" 
      elementFormDefault="qualified"> 

    <xsd:complexType name="header"> 
     <xsd:sequence> 
      <xsd:element name="id" type="xsd:long"/> 
     </xsd:sequence> 
    </xsd:complexType> 

</xsd:schema> 

回答

2

第二个XML示例是正确的,如您的模式验证所示。

我想你混淆了元素的命名空间的类型的命名空间。行 < xsd:element name =“header”type =“h:header”> 在模式中定义名称空间中名为“header”的元素,该元素在“http://www.example.org/header”命名空间中为“header”类型。

考虑XML是什么样子,如果行是不是 < XSD:元素名称= “myHeader” TYPE = “H:头” >

+0

你是完全正确的,谢谢你。 – Ziletka 2013-04-30 16:16:25

+0

不错的提示,thx! +1 – JBA 2013-09-24 12:27:29