5

我有一个需要编写 XSD 的 xml 片段

<root xmlns="http://xmlns.oracle.com/sca/1.0" xmlns:id="http://xmlns.oracle.com/id/1.0">
  <service name="Book" id:number="465"/>
</root>

以下 XSD 在生成 JAXB 类时会出错。

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://xmlns.oracle.com/sca/1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="root">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="service">
          <xs:complexType>
            <xs:simpleContent>
              <xs:extension base="xs:string">
                <xs:attribute type="xs:string" name="name"/>
                <xs:attribute ref="ns:number" xmlns:ns="http://xmlns.oracle.com/id/1.0"/>
              </xs:extension>
            </xs:simpleContent>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  </xs:schema>

错误是

C:\Program Files\Java\jdk1.7.0_06\bin>xjc -p test C:\book.xsd 解析模式... [错误] src-resolve.4.2:解析组件“ns:number”时出错。检测到“ns:number”在命名空间“http://xmlns.oracle.com/id/1.0”中,但该命名空间中的组件无法从模式文档“file:/C:/book”中引用。xsd'。如果这是不正确的命名空间,则可能需要更改 'ns:number' 的前缀。如果这是正确的命名空间,则应将适当的“import”标签添加到“file:/C:/book.xsd”。file:/C:/book.xsd 的第 10 行

4

2 回答 2

8

实际上,您至少需要与命名空间一样多的 XSD 文件,因为一个 XSD 文件只能针对一个命名空间,或者没有。

由于您的根元素位于一个名称空间中,而属性位于另一个名称空间中,因此您至少需要两个文件。您通过xsd:import “链接”它们。

顶级 XSD:

<?xml version="1.0" encoding="utf-8"?>
<!--XML Schema generated by QTAssistant/XML Schema Refactoring (XSR) Module (http://www.paschidev.com)-->
<xsd:schema xmlns="http://xmlns.oracle.com/sca/1.0" xmlns:id="http://xmlns.oracle.com/id/1.0" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://xmlns.oracle.com/sca/1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:import schemaLocation="xsd-syntax-for-xml-attributes-with-namespace1.xsd" namespace="http://xmlns.oracle.com/id/1.0" />
  <xsd:element name="root">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="service">
          <xsd:complexType>
            <xsd:attribute name="name" type="xsd:string" use="required" />
            <xsd:attribute ref="id:number" use="required" />
          </xsd:complexType>
        </xsd:element>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

xsd-syntax-for-xml-attributes-with-namespace1.xsd

<?xml version="1.0" encoding="utf-8"?>
<!--XML Schema generated by QTAssistant/XML Schema Refactoring (XSR) Module (http://www.paschidev.com)-->
<xsd:schema xmlns="http://xmlns.oracle.com/id/1.0" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://xmlns.oracle.com/id/1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:attribute name="number" type="xsd:unsignedShort" />
</xsd:schema>
于 2012-08-24T14:12:43.163 回答
0

使用以下两种模式

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="http://xmlns.oracle.com/sca/1.0" xmlns:id="http://xmlns.oracle.com/id/1.0" xmlns:sca="http://xmlns.oracle.com/sca/1.0">
  <xs:import namespace="http://xmlns.oracle.com/id/1.0" schemaLocation="id.xsd"/>
  <xs:element name="root">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="sca:service"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="service">
    <xs:complexType>
      <xs:attribute name="name" use="required" type="xs:NCName"/>
      <xs:attribute ref="id:number" use="required"/>
    </xs:complexType>
  </xs:element>
</xs:schema>

身份证

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="http://xmlns.oracle.com/id/1.0" xmlns:id="http://xmlns.oracle.com/id/1.0" xmlns:sca="http://xmlns.oracle.com/sca/1.0">
  <xs:import namespace="http://xmlns.oracle.com/sca/1.0" schemaLocation="Untitled2.xsd"/>
  <xs:attribute name="number" type="xs:integer"/>
</xs:schema>
于 2012-08-24T14:13:05.007 回答