【问题标题】:Imported schema & namespaces导入的架构和命名空间
【发布时间】:2013-04-28 20:58:06
【问题描述】:

有什么区别(关注前缀h:、c:、a:和default):

<?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 实现了映射,并且编组器创建了第一个 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] 连接/标题])。

我认为整个元素标题与 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>

例如。 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>

【问题讨论】:

    标签: xml jaxb xsd xml-namespaces xml-validation


    【解决方案1】:

    您的架构验证表明,第二个 XML 示例是正确的。

    我认为您将类型的名称空间与元素的名称空间混淆了。线 在您的架构中,在默认命名空间中定义了一个名为“header”的元素,该元素属于“http://www.example.org/header”命名空间中的“header”类型。

    考虑一下如果该行改为 xml 的外观

    【讨论】:

      猜你喜欢
      • 2023-04-09
      • 1970-01-01
      • 2013-07-14
      • 2010-10-19
      • 1970-01-01
      • 1970-01-01
      • 2012-11-03
      • 2016-08-29
      • 2010-10-29
      相关资源
      最近更新 更多