命名空间为的XML属性的XSD语法



我有一个xml片段,需要为它编写XSD

<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测试C:\book.xsd正在分析架构。。。[错误]src resolve.4.2:解析组件'ns:number'时出错。已检测到"ns:number"在命名空间中http://xmlns.oracle.com/id/1.0',但是组件来自此命名空间的无法从架构文档"file:/C:/book"中引用。xsd’。如果这是不正确的命名空间,则可能需要前缀"ns:number"s要更改。如果这是正确的命名空间,则使用适当的"import"标记应添加到"file:/C:/book.xsd"中。文件的第10行:/C:/book.xsd

实际上,您至少需要与命名空间一样多的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>

使用以下两个模式

<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>

对于ID

<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>

最新更新