简体   繁体   中英

Import namespaces in XML schema

I have two different .xsd ("item" and "user") and I want to merge them in a single .xsd using import: this new schema (items.xsd) will have one sequence of items and one of users.

Problem is I get this error at the lines where I call a type that I've previously defined in the namespace:

src-resolve: Cannot resolve the name 'i:item' to a(n) 'type definition' component.

Plus, in the xml I get this error on the root element:

cvc-elt.1.a: Cannot find the declaration of element 'items'.

item.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="item"
     xmlns="item" elementFormDefault="qualified">
    <xsd:simpleType name="itemID">
        <xsd:restriction base="xsd:ID">
            <xsd:pattern value="AR[0-9][0-9][0-9][0-9]"/>
        </xsd:restriction>
    </xsd:simpleType>
    <xsd:element name="item" xmlns:i="item">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="title" type="xsd:string"/>
                <xsd:element name="description">
                    <xsd:simpleType>
                        <xsd:restriction base="xsd:string">
                            <xsd:maxLength value="40"/>
                        </xsd:restriction>
                    </xsd:simpleType>
                </xsd:element>
                <xsd:element name="comments">
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:element name="comment" minOccurs="0" maxOccurs="100">
                            <xsd:complexType>
                                <xsd:sequence>
                                    <xsd:element name="comment_text">
                                        <xsd:simpleType>
                                            <xsd:restriction base="xsd:string">
                                                <xsd:maxLength value="75"/>
                                            </xsd:restriction>
                                        </xsd:simpleType>
                                    </xsd:element>
                                </xsd:sequence>
                                <xsd:attribute name="id_user" type="xsd:IDREF" use="required"/>
                                <xsd:attribute name="id_item" type="xsd:IDREF" use="required"/>
                            </xsd:complexType>
                        </xsd:element>
                    </xsd:sequence>
                </xsd:complexType>
            </xsd:element>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

user.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="user"  elementFormDefault="qualified" xmlns="user">
    <xsd:simpleType name="usrID">
        <xsd:restriction base="xsd:ID">
            <xsd:pattern value="[A-Z][0-9][0-9][0-9][0-9][0-9]"/>
        </xsd:restriction>
    </xsd:simpleType>
    <xsd:element name="user" xmlns:u="user">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="alias" type="xsd:string"/>
                <xsd:element name="email" type="xsd:string"/>
            </xsd:sequence>
            <xsd:attribute name="id_user" type="userID" use="required"/>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

items.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:i="item"
   xmlns:u="user" targetNamespace="items" elementFormDefault="qualified">
    <xsd:import namespace="item" schemaLocation="item.xsd"  />
    <xsd:import namespace="user" schemaLocation="user.xsd" />
    <xsd:element name="items">
        <xsd:complexType>
            <xsd:sequence>
                <!-- ERROR HERE --> <xsd:element name="item" type="i:item" minOccurs="1" maxOccurs="unbounded"/>
                <xsd:element name="users">
                    <xsd:complexType>
                        <xsd:sequence>
                <!-- ERROR HERE --> <xsd:element name="user" type="u:user" minOccurs="1" maxOccurs="unbounded"/>
                        </xsd:sequence>
                    </xsd:complexType>
                </xsd:element>
            </xsd:sequence>
            <xsd:attribute name="url" type="xsd:string" use="required"/>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

items.xml

<!-- ERROR HERE --><items url="http://example.com/items" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="items.xsd">
    <item id_item="AR0000">
        <title>
            Item title
        </title>
        <description>
            Item description
            </description>
            <comments>
                <comment id_user="A00000" id_item="AR0000">
                    <comment_text>
                        Text
                    </comment_text>
                </comment>
            </comments>
    </item>
    <users>
        <user id_user="A00000">
            <alias>
                User1 alias
            </alias>
            <email>
                user1@gmail.com
            </email>
        </user>
        <user id_user="A00001">
            <alias>
                User2 alias
            </alias>
            <email>
                user2@hotmail.com
            </email>
        </user>
    </users>
</items>

First fix

src-resolve: Cannot resolve the name 'i:item' to a(n) 'type definition' component.

You simply want to use the element that have already been defined in the other schemas. Thus you should use an <xsd:element ref="..."> .

You can change your declarations near the <!-- ERROR HERE --> (in the items.xsd schema) like so:

 <xsd:element ref="i:item" minOccurs="1" maxOccurs="unbounded"/>

and

<xsd:element ref="u:user" minOccurs="1" maxOccurs="unbounded"/>

because using a type on an <xsd:element> would assume you have defined either a xsd:simpleType or a xsd:complexType elsewhere.

Please note there's a typo in user.xsd : you are referring to a type named userID , but I think it the correct spelling is usrID .

Second fix

Round 1

cvc-elt.1.a: Cannot find the declaration of element 'items'.

You need to properly attach the schema to your document like follows:

<items xmlns="items" url="http://example.com/items" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="items items.xsd">

Since the target namespace of your schema is "items", it need to be declared to add in the xsi:schemaLocation value, which is expected to be a namespace uri, followed by a space, followed by the schema file url.

Moreover, you <items> element is bound to the items namespace thus i needed to add the xmlns="items" declaration.

Round 2

Once you've done that a bunch of namespaces errors will be risen because you haven't defined all the appropriate namespaces on the item and user -related elements in the input XML.

One way to that can be by fixing your XML document like follows (notice the XML namespace declaration on <item> and <user> :

<?xml version="1.0" encoding="UTF-8"?>
<items xmlns="items" url="http://example.com/items" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="items items.xsd">
    <item id_item="AR0000" xmlns="item">
        <title>
            Item title
        </title>
        <description>
            Item description
        </description>
        <comments>
            <comment id_user="A00000" id_item="AR0000">
                <comment_text>
                    Text
                </comment_text>
            </comment>
        </comments>
    </item>
    <users>
        <user id_user="A00000" xmlns="user">
            <alias>
                User1 alias
            </alias>
            <email>
                user1@gmail.com
            </email>
        </user>
        <user id_user="A00001" xmlns="user">
            <alias>
                User2 alias
            </alias>
            <email>
                user2@hotmail.com
            </email>
        </user>
    </users>
</items>

There will remain some issues regarding attribute declarations and ID/IDREFs that I let you fix. .

You can't combine the two schema documents into one schema document because they have different target namespaces. But perhaps that's not what you were trying to do: @potame seems to have read the question quite differently.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM