SonarQube requires separate xsd schema files and displays the error: "Error resolving component in XSD file"

SonarQube requires separate xsd schema files and displays the error: "Error resolving component in XSD file"

我在一个项目中使用两个不同的 xsd 架构文件。

第一个是input.xsd。此文件使用 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>

另一个是input_noTargetNamespace.xsd。此文件没有 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 文件看起来像:

<?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>

如果我在一个项目中使用这些单独的 xsd 模式文件,SonarQube 会显示错误:

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'.

有谁知道为什么会这样?如果需要,我可以提供任何其他信息。提前致谢。

问题已通过以下文件和内容解决:

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>

更具体地说: 我修改了 xml 以实现将 XSD 正确链接到 XML 文档 without namespaces:

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

因此 input.xsd 看起来像:

<?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>

由于冗余而未使用 input_noTargetNamespace.xsd

对于 xsd 文件,我已更改:

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

只需

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">