5

I have an xml fragment for which I need to write 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>

The following XSD gives error while JAXB class generation.

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

Error is

C:\Program Files\Java\jdk1.7.0_06\bin>xjc -p test C:\book.xsd parsing a schema... [ERROR] src-resolve.4.2: Error resolving component 'ns:number'. It was detected that 'ns:number' is in namespace 'http://xmlns.oracle.com/id/1.0', but component s from this namespace are not referenceable from schema document 'file:/C:/book. xsd'. If this is the incorrect namespace, perhaps the prefix of 'ns:number' need s to be changed. If this is the correct namespace, then an appropriate 'import' tag should be added to 'file:/C:/book.xsd'. line 10 of file:/C:/book.xsd

2 Answers 2

8

You actually need at least as many XSD files as namespaces since one XSD file can target only one namespace, or none.

Since your root element is in one namespace, and the attribute in another, you need then two files at least. You "link" them through an xsd:import.

Top 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>
Sign up to request clarification or add additional context in comments.

Comments

0

Use the below two schemas

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.