使用 xjc 离线包含 XMLSchema

Offline inclusion of XMLSchema with xjc

我有一个 XSD 文件引用和元素 xs:schema。当我 运行 xjc 连接到互联网时,它会创建适当的 JAXB 对象。当我尝试将 XMLSchema.xsd 作为本地文件引用时,它失败了。

如何引用 XMLSchema.xsd 的本地 xsd 文件副本,以便我可以 运行 xjc 离线创建 JAXB 类?

目前我打电话给xjc -d out schema0.xsd schema1.xsd

Schema0.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="http://www.w3schools.com" 
    xmlns="http://www.w3schools.com" xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    elementFormDefault="qualified">

    <xs:import namespace="http://www.w3.org/2001/XMLSchema" schemaLocation="http://www.w3.org/2001/XMLSchema.xsd"/>
    <xs:element name="children">
        <xs:complexType>
            <xs:sequence>
                <xs:element minOccurs="0" maxOccurs="1" name="childname" />
                <xs:element ref="xs:schema" />
                <xs:any />
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

Schema1.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema 
targetNamespace="http://www.w3schools.com" 
xmlns="http://www.w3schools.com" 
xmlns:xs="http://www.w3.org/2001/XMLSchema" 
elementFormDefault="qualified">
    <xs:import namespace="http://www.w3.org/2001/XMLSchema"  schemaLocation="http://www.w3.org/2001/XMLSchema.xsd" />
    <xs:element name="person">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="firstname" type="xs:string"/>
                <xs:element name="lastname" type="xs:string"/>
                <xs:any minOccurs="0"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

下载 XMLSchema.xsd...

curl -H "Accept: application/xml" -H "Content-Type: application/xml" -X GET http://www.w3.org/2001/XMLSchema.xsd > XMLSchema-new.xsd

从...中删除文件的第一部分

<!DOCTYPE xs:schema PUBLIC "-//W3C//DTD XMLSCHEMA.....
.... To ....
<!ATTLIST xs:union id ID #IMPLIED>
]>

下载 XMLSchema.xsd 引用的 xml.xsd 文件....

curl -H "Accept: application/xml" -H "Content-Type: application/xml" -X GET http://www.w3.org/2001/xml.xsd > xml-new.xsd

创建目录文件....

catalog.cat

SYSTEM "http://www.w3.org/2001/XMLSchema" "XMLSchema-new.xsd"
PUBLIC "http://www.w3.org/2001/XMLSchema" "XMLSchema-new.xsd"
PUBLIC "http://www.w3.org/XML/1998/namespace" "xml.xsd"

断开互联网连接 运行 xjc -catalog catalog.cat schema0.xsd schema1.xsd

感谢 Blaise 提供的详细信息 article。我未能删除 XMLSchema.xsd 的 <!DOCTYPE 部分,这导致 xjc 失败。