Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Target namespaces #26

Closed
umarti opened this issue Aug 5, 2020 · 6 comments
Closed

Target namespaces #26

umarti opened this issue Aug 5, 2020 · 6 comments

Comments

@umarti
Copy link

umarti commented Aug 5, 2020

I get an exception "XsdSchema refers a namespace which was not imported." while parsing the following valid XSD file:

<?xml version="1.0" ?>
<xsd:schema elementFormDefault="qualified" targetNamespace="urn:com/example/mynamespace" xmlns:mynamespace="urn:com/example/mynamespace" version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
	<xsd:simpleType name="type">
		<xsd:restriction base="xsd:string" />
	</xsd:simpleType>
	<xsd:simpleType name="anothertype">
		<xsd:list itemType="mynamespace:type" />
	</xsd:simpleType>
</xsd:schema>

It seams, that XsdParser doesn't like targetNamesparce and its namespace declaration.

@lcduarte
Copy link
Member

lcduarte commented Aug 5, 2020

Hello,

Thanks for using the library. The library only works with local files, which means that if the value used in the target namespace isn't a path to a local file it won't be able to validate the elements using that namespace.

I'll try to look into adding support to external namespace declarations, maybe this weekend.

Edit: If you could provide me with the real example that you are using it would help my development.

@umarti
Copy link
Author

umarti commented Aug 5, 2020

Dear lcduarte

Thank you for your answer. The given XSD triggers the bug. It's a minimal example for you.

We have a whole chain of XSDs, therefore each XSD has a different targetNamespace, which is never empty. Example:

One XSD with base types, let's call it "first.xsd", imports all elements into targetNamespace="urn:com/example/mynamespace".
A second XSD imports it with <xsd:import namespace="urn:com/example/mynamespace" schemaLocation="first.xsd" />

This is a not-so-exotic ability of XSDs I guess and I would love to have it supported in your library. We use it for extended validation, sorting of elements and for code emitter templates.

Sincerely,
umarti

@lcduarte
Copy link
Member

Hello umarti,

It took me a while but I finally had time to properly investigate the issue. I've made some changes and I'll use the following xsd file to explain them: Customer Types XSD

<xs:schema xmlns:cmn="http://NamespaceTest.com/CommonTypes"
           targetNamespace="http://NamespaceTest.com/CustomerTypes"
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           elementFormDefault="qualified">
    <xs:import schemaLocation="issue_26_CommonTypes.xsd"
               namespace="http://NamespaceTest.com/CommonTypes" />
    <xs:complexType name="CustomerType">
        <xs:sequence>
            <xs:element name="Name" type="xs:string" />
            <xs:element name="DeliveryAddress" type="cmn:AddressType" />
            <xs:element name="BillingAddress" type="cmn:AddressType" />
        </xs:sequence>
    </xs:complexType>
</xs:schema>
  • That file has multiple XMLNS tags, one of which is cmn which defines some CommonTypes;
  • For XsdParser to be able to parse the CustomerTypes.xsd it needs the location of the CommonTypes.xsd, so we need to add a xsd:import element with the CommonTypes namespace and the respective XSD file location;
  • The XsdParser then acknoledges that needs to parse the CommonTypes XSD in order to successfully parse the CustomerTypes;
  • After parsing both files the references, such as DeliveryAddress and BillingAddress from the CustomerTypes.xsd, are able to be successfully resolved;

I've made a new release with these corrections, (1.0.31).

Thank you for your input, it was something that was really missing from the library since I didn't fully understood it and hadn't time to investigate the problem.

@umarti
Copy link
Author

umarti commented Aug 17, 2020

Hi Luís

Thank you for your efforts. I gave 1.0.33-SNAPSHOT a try and got a NullPointerException.

The following extension to issue_26_CommonTypes.xsd triggers it:

<xs:schema targetNamespace="http://NamespaceTest.com/CommonTypes"
xmlns:cmn="http://NamespaceTest.com/CommonTypes"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:complexType name="ReferencesCommonType">
xs:sequence
<xs:element name="Type1" type="cmn:AddressType" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="AddressType">
xs:sequence
<xs:element name="Line1" type="xs:string" />
<xs:element name="Line2" type="xs:string" />
</xs:sequence>
</xs:complexType>
<xs:simpleType name="PriceType">
<xs:restriction base="xs:decimal">
<xs:fractionDigits value="2" />
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="PaymentMethodType">
<xs:restriction base="xs:string">
<xs:enumeration value="VISA" />
<xs:enumeration value="MasterCard" />
<xs:enumeration value="Cash" />
<xs:enumeration value="AMEX" />
</xs:restriction>
</xs:simpleType>
</xs:schema>

Sincerely,
-Urs

@lcduarte
Copy link
Member

Hi Urs,

Sorry for the delay. Your error seems to be triggered by:

<xs:element name="Type1" type="cmn:AddressType" />

Since you are already in the namespace where AddressType is declared you shouldn't need to use the namespace prefix in the type attribute. By changing it to type="AddressType" it works.

Either way I'll try to add a safeguard for this case.

@lcduarte
Copy link
Member

Safeguard added in 1.0.34.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants