0

I'm using two different xsd schema files in one project.

First one is input.xsd. This file uses targetNamespace:

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

  <xsd:element name="Mobiles">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="Mobile" type="w3s:Mobile" maxOccurs="unbounded"/>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>

  <xsd:complexType name="Mobile">
    <xsd:sequence>
      <xsd:element name="Model" type="xsd:string"/>
      <xsd:element name="OS">
        <xsd:simpleType>
          <xsd:restriction base="xsd:string">
            <xsd:enumeration value="Android"/>
          </xsd:restriction>
        </xsd:simpleType>
      </xsd:element>
      <xsd:element name="Origin" type="xsd:string"/>
      <xsd:element name="Material" type="xsd:string"/>
      <xsd:element name="Samsung" type="w3s:Samsung"/>
    </xsd:sequence>
  </xsd:complexType>

  <xsd:complexType name="Samsung" mixed="true">
    <xsd:sequence>
      <xsd:element name="Wlan">
        <xsd:simpleType>
          <xsd:restriction base="xsd:string">
            <xsd:enumeration value="802.11"/>
          </xsd:restriction>
        </xsd:simpleType>
      </xsd:element>
      <xsd:element name="CardSlot" type="xsd:string"/>
      <xsd:element name="RadioAvailability" type="xsd:boolean" default="true"/>
      <xsd:element name="BluetoothAvailability" type="xsd:boolean" default="false"/>
    </xsd:sequence>
  </xsd:complexType>
</xsd:schema>

Another is input_noTargetNamespace.xsd. This file doesn't have targetNamespace:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

    <xsd:element name="Mobiles">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="Mobile" type="Mobile" maxOccurs="unbounded"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>

    <xsd:complexType name="Mobile">
        <xsd:sequence>
            <xsd:element name="Model" type="xsd:string"/>
            <xsd:element name="OS">
                <xsd:simpleType>
                    <xsd:restriction base="xsd:string">
                        <xsd:enumeration value="Android"/>
                    </xsd:restriction>
                </xsd:simpleType>
            </xsd:element>
            <xsd:element name="Origin" type="xsd:string"/>
            <xsd:element name="Material" type="xsd:string"/>
            <xsd:element name="Samsung" type="Samsung"/>
        </xsd:sequence>
    </xsd:complexType>

    <xsd:complexType name="Samsung" mixed="true">
        <xsd:sequence>
            <xsd:element name="Wlan">
                <xsd:simpleType>
                    <xsd:restriction base="xsd:string">
                        <xsd:enumeration value="802.11"/>
                    </xsd:restriction>
                </xsd:simpleType>
            </xsd:element>
            <xsd:element name="CardSlot" type="xsd:string"/>
            <xsd:element name="RadioAvailability" type="xsd:boolean" default="true"/>
            <xsd:element name="BluetoothAvailability" type="xsd:boolean" default="false"/>
        </xsd:sequence>
    </xsd:complexType>
</xsd:schema>

XML file looks like:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<p7:Mobiles xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns:p7="https://www.w3schools.com"
            xsi:schemaLocation="https://www.w3schools.com input.xsd" >

  <Mobile>
    <Model>G975F</Model>
    <OS>Android</OS>
    <Origin>USA</Origin>
    <Material>Plastic</Material>
    <Samsung>
      <Wlan>802.11</Wlan>
      <CardSlot>MicroSD</CardSlot>
      <RadioAvailability>true</RadioAvailability>
      <BluetoothAvailability>true</BluetoothAvailability>
    </Samsung>
  </Mobile>
  <Mobile>
    <Model>G986</Model>
    <OS>Android</OS>
    <Origin>USA-Israel</Origin>
    <Material>Silicon-Plastic</Material>
    <Samsung>
      <Wlan>802.11</Wlan>
      <CardSlot>MicroSD</CardSlot>
      <RadioAvailability>true</RadioAvailability>
      <BluetoothAvailability>false</BluetoothAvailability>
    </Samsung>
  </Mobile>
  <Mobile>
    <Model>G770F</Model>
    <OS>Android</OS>
    <Origin>Israel</Origin>
    <Material>Silicon-Plastic</Material>
    <Samsung>
      <Wlan>802.11</Wlan>
      <CardSlot>MicroSD</CardSlot>
      <RadioAvailability>true</RadioAvailability>
      <BluetoothAvailability>false</BluetoothAvailability>
    </Samsung>
  </Mobile>
</p7:Mobiles>

If I use these separate xsd schema files in one project, SonarQube shows the error:

Error resolving component 'w3s:Mobile'. It was detected that 'w3s:Mobile' is in namespace 'https://www.w3schools.com', but components from this namespace are not referenceable from schema document. If this is the incorrect namespace, perhaps the prefix of 'w3s:Mobile' needs to be changed. If this is the correct namespace, then an appropriate 'import' tag should be added to 'input_noTargetNamespace.xsd'.

Does anyone know why it happens? I can provide any other information if needed. Thanks in advance.

invzbl3
  • 5,872
  • 9
  • 36
  • 76
  • Why do you say in your title that "SonarQube requires separate xsd schema files"? Separate in what way and for what purpose? – kjhughes Jan 10 '21 at 04:46
  • I noted that by validating a project, SonarQube requires a separate file from me. "Separate" means that I've already created one file with targetNamespace, but a validator which is based on SonarQube wants without targetNamespace. – invzbl3 Jan 10 '21 at 04:50
  • 1
    You posted one XML document whose root element was in a namespace. It can be successfully validated with the XSD whose target namespace matches its root element. Is there another XML document whose root element is in no namespace? If not, what is the purpose of the second XSD? To say its purpose is to satisfy SonarQube is to place the unexplained demands of a code quality checker above those well-defined and standardized semantics of XSD-based validation. – kjhughes Jan 10 '21 at 04:57
  • After some observation I suppose validator takes my `input.xsd` schema file removes on his own attribute `targetNamespace` then validates output files using `input_noTargetNamespace.xsd`. As I understand, `input_noTargetNamespace.xsd` is creating by validator, so I don't need it in project as additional file. – invzbl3 Jan 11 '21 at 01:54

1 Answers1

0

The issue was solved with the following files and content:

input.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Mobiles
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="input.xsd">
    <Mobile>
        <Model>G975F</Model>
        <OS>Android</OS>
        <Origin>USA</Origin>
        <Material>Plastic</Material>
        <Samsung>
            <Wlan>802.11</Wlan>
            <CardSlot>MicroSD</CardSlot>
            <RadioAvailability>true</RadioAvailability>
            <BluetoothAvailability>true</BluetoothAvailability>
        </Samsung>
    </Mobile>
    <Mobile>
        <Model>G986</Model>
        <OS>Android</OS>
        <Origin>USA-Israel</Origin>
        <Material>Silicon-Plastic</Material>
        <Samsung>
            <Wlan>802.11</Wlan>
            <CardSlot>MicroSD</CardSlot>
            <RadioAvailability>true</RadioAvailability>
            <BluetoothAvailability>false</BluetoothAvailability>
        </Samsung>
    </Mobile>
    <Mobile>
        <Model>G770F</Model>
        <OS>Android</OS>
        <Origin>Israel</Origin>
        <Material>Silicon-Plastic</Material>
        <Samsung>
            <Wlan>802.11</Wlan>
            <CardSlot>MicroSD</CardSlot>
            <RadioAvailability>true</RadioAvailability>
            <BluetoothAvailability>false</BluetoothAvailability>
        </Samsung>
    </Mobile>
</Mobiles>

To be more specific: I've modified xml to realize correct linking an XSD to an XML document without namespaces:

<Mobiles
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="input.xsd">

Hence input.xsd looks like:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

    <xsd:element name="Mobiles">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="Mobile" type="Mobile" maxOccurs="unbounded"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>

    <xsd:complexType name="Mobile">
        <xsd:sequence>
            <xsd:element name="Model" type="xsd:string"/>
            <xsd:element name="OS">
                <xsd:simpleType>
                    <xsd:restriction base="xsd:string">
                        <xsd:enumeration value="Android"/>
                    </xsd:restriction>
                </xsd:simpleType>
            </xsd:element>
            <xsd:element name="Origin" type="xsd:string"/>
            <xsd:element name="Material" type="xsd:string"/>
            <xsd:element name="Samsung" type="Samsung"/>
        </xsd:sequence>
    </xsd:complexType>

    <xsd:complexType name="Samsung" mixed="true">
        <xsd:sequence>
            <xsd:element name="Wlan">
                <xsd:simpleType>
                    <xsd:restriction base="xsd:string">
                        <xsd:enumeration value="802.11"/>
                    </xsd:restriction>
                </xsd:simpleType>
            </xsd:element>
            <xsd:element name="CardSlot" type="xsd:string"/>
            <xsd:element name="RadioAvailability" type="xsd:boolean" default="true"/>
            <xsd:element name="BluetoothAvailability" type="xsd:boolean" default="false"/>
        </xsd:sequence>
    </xsd:complexType>
</xsd:schema>

without using input_noTargetNamespace.xsd because of redundancy.

And for xsd file I've changed:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
            xmlns:w3s="https://www.w3schools.com"
            targetNamespace="https://www.w3schools.com">

simply to

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
invzbl3
  • 5,872
  • 9
  • 36
  • 76