如何修复 "Error resolving component in XSD file"?

How to fix "Error resolving component in XSD file"?

我使用的 XSD 看起来像:

<?xml version="1.0" encoding="UTF-8"?>
<!--  XML schema -->

<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 我遇到了问题:

Error resolving component 'tns:Mobile'. It was detected that 'tns: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 'tns:Mobile' needs to be changed. If this is the correct namespace, then an appropriate 'import' tag should be added to 'input_noTargetNamespace.xsd'.

为了解决这个问题,我检查了这个 有类似的问题,但是我在 Samsung 的声明中的 type="Samsung" 中没有任何命名空间前缀,所以它看起来就像我错过了别的东西。

谁能解释一下这种情况下的问题原因?我需要为 xsd:schema 添加内容吗?如果需要,我可以提供任何其他信息。

XSD 的 xsd:schema/@targetNamespace 应该与 XML 文档中根元素的名称空间匹配。

然后,为目标命名空间定义一个命名空间前缀,并用它来引用 XSD 中的类型。

以下是您的 XSD 和 XML 文件已更正,以便您的 XSD 将成功验证您的 XML:

XSD

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

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>