DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Validate XML Request Against XML Schema in Mule 4
  • Freshness as a First-Class Schema Concern: Modeling Data Staleness Across 83 Data Sources
  • Replacing JSON With Protobuf in Your Microservice Mesh: A Zero-Downtime Migration Blueprint
  • Dynamic Tool Selection: A Portable Pattern for Agents Drowning in Tool Schemas

Trending

  • SSRF Prevention in C#: Securing File Imports From User-Supplied URLs
  • Searching Kafka Without an Index: Lessons From Building Message Search for Kafdrop
  • Giving AI Agents Safe Access to Legacy Systems: Introducing legacy2mcp
  • Beyond the Endpoint: How Agent Cards Enable Discovery, Interoperability, and Trust

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Languages
  4. JAXB (XJC) Imported Schemas and XML Catalogs

JAXB (XJC) Imported Schemas and XML Catalogs

Import allows one XML schema to reference elements and types from another XML schema.

By 
Blaise Doughan
·
Oct. 22, 11 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
39.0K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

XML schema has a power mechanism called "import".  Import allows one XML schema to reference elements and types from another XML schema.  This means you could define types to represent commonly used information once and import these types into other XML schemas. Like any powerful tool, the import mechanism also has the ability to inflict pain.  In this post I'll demonstrate how to leverage an XML catalog to eliminate the pain when using JAXB's XJC tool to generate classes from an XML schema with imports.

XML Schemas

The following XML schemas will be used for this post.  The imported schemas (address.xsd and phone-number.xsd) are in a subdirectory called "imports" relative to customer.xsd.

customer.xsd (Root XML Schema)

This is the root XML schema that we will be generating our Java classes from.  This XML schema contains two import statement:

  1. The first import includes a system ID specifying the hosted location of the imported XML schema.  JAXB's XJC tool can generate classes from a hosted XML schema (see Processing Atom Feeds with JAXB), but in this example the XML schemas are still under development and have not been hosted yet.
  2. The second import does not include a system ID at all.  The XJC tool will not know where to find the imported XML schema.


<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
    targetNamespace="http://www.example.com/customer"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns="http://www.example.com/customer"
    xmlns:add="http://www.example.com/address"
    xmlns:phn="http://www.example.com/phone-number"
    elementFormDefault="qualified">

    <xs:import
        schemaLocation="http://www.example.com/address/address.xsd"
        namespace="http://www.example.com/address"/>

    <xs:import
        namespace="http://www.example.com/phone-number"/>

    <xs:element name="customer">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="name" type="xs:string"/>
                <xs:element ref="add:address"/>
                <xs:element ref="phn:phone-number"
                    minOccurs="0" maxOccurs="unbounded"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

</xs:schema>
imports/address.xsd (Imported Schema)


<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
    targetNamespace="http://www.example.com/customer"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns="http://www.example.com/customer"
    xmlns:add="http://www.example.com/address"
    xmlns:phn="http://www.example.com/phone-number"
    elementFormDefault="qualified">

    <xs:import
        schemaLocation="http://www.example.com/address/address.xsd"
        namespace="http://www.example.com/address"/>

    <xs:import
        namespace="http://www.example.com/phone-number"/>

    <xs:element name="customer">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="name" type="xs:string"/>
                <xs:element ref="add:address"/>
                <xs:element ref="phn:phone-number"
                    minOccurs="0" maxOccurs="unbounded"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

</xs:schema>


imports/phone-number.xsd (Imported Schema)
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://www.example.com/phone-number"
    xmlns="http://www.example.com/phone-number"
    elementFormDefault="qualified">

    <xs:element name="phone-number">
        <xs:complexType>
            <xs:simpleContent>
                <xs:extension base="xs:string">
                    <xs:attribute name="type" type="xs:string"/>
                </xs:extension>
            </xs:simpleContent>
        </xs:complexType>
    </xs:element>

</xs:schema>


XJC - Generating Java Classes from an XML Schema

The XJC tool can be used to generate Java classes from an XML schema. In this example I have customer.xsd on my local file system

xjc -d out customer.xsd

Below are the error messages that I receive from making the above XJC call.  One error we see is that it could not find the schema at "http://www.example.com/address/address.xsd" which is expected as I stated earlier that I haven't hosted that XML schema yet.  Another error we see is that "phn:phone-number" can not be resolved to an element definition, this is expected too since we did not specify a schema location.

parsing a schema...
[WARNING] schema_reference.4: Failed to read schema document 'http://www.example.com/address/address.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>.
  line 12 of file:/C:/Program%20Files/Java/jdk1.6.0_20/bin/customer.xsd

[ERROR] src-resolve: Cannot resolve the name 'add:address' to a(n) 'element declaration' component.
  line 21 of file:/C:/Program%20Files/Java/jdk1.6.0_20/bin/customer.xsd

[ERROR] src-resolve: Cannot resolve the name 'phn:phone-number' to a(n) 'element declaration' component.
  line 23 of file:/C:/Program%20Files/Java/jdk1.6.0_20/bin/customer.xsd

Failed to parse a schema.


XML Catalog (catalog.cat)

We can solve the problems we are encountering through the use of an XML catalog.  An XML catalog will allow us to specify real locations for our imported XML schemas.  The XJC tool supports several different XML Catalog formats:  TR9401, XCatalog, OASIS XML Catalog.  

The XML catalog formats have system and public mappings.  The system mappings are useful for when the import contains a system ID, and the public mappings must be used when the import does not contain a system ID.

The following import can be used with a system mapping

<xs:import
    schemaLocation="http://www.example.com/address/address.xsd"
    namespace="http://www.example.com/address"/>

 

This import requires a public mapping based on the namespace URI:

<xs:import
    namespace="http://www.example.com/phone-number"/>

 

Below are a couple of examples based on the XML schemas from this post:

TR9401 Format

-- Match address.xsd by URL --
SYSTEM "http://www.example.com/address/address.xsd" "imports/address.xsd"

-- Match phone-number.xsd by namespace URI --
PUBLIC "http://www.example.com/phone-number" "imports/phone-number.xsd"


OASIS XML Catalog Format

<!DOCTYPE catalog
    PUBLIC "-//OASIS//DTD Entity Resolution XML Catalog V1.0//EN"
           "http://www.oasis-open.org/committees/entity/release/1.0/catalog.dtd">
<catalog xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog">

    <system
        systemId="http://www.example.com/address/address.xsd"
        uri="imports/address.xsd"/>
    <public
        publicId="http://www.example.com/phone-number"
        uri="imports/phone-number.xsd"/>

</catalog>


XJC - Specifying the Catalog File

The"-catalog" option is used to specify the catalog file when running the XJC command to generate Java classed from XML schemas.

xjc -d out -catalog catalog.cat customer.xsd


 

From http://blog.bdoughan.com/2011/10/jaxb-xjc-imported-schemas-and-xml.html

XML Schema

Opinions expressed by DZone contributors are their own.

Related

  • Validate XML Request Against XML Schema in Mule 4
  • Freshness as a First-Class Schema Concern: Modeling Data Staleness Across 83 Data Sources
  • Replacing JSON With Protobuf in Your Microservice Mesh: A Zero-Downtime Migration Blueprint
  • Dynamic Tool Selection: A Portable Pattern for Agents Drowning in Tool Schemas

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook