在XSD中创建不同的元素

时间:2013-04-06 20:35:44

标签: xml xsd xsd-validation

我修改了XSD以添加新项目而无需创建新的XSD。目前我在这种情况下只有一个元素是“轮胎”。

我创建了一个“轮胎”类型的元素。

<xs:element name="tire" type="x:tire" />

但验证时出错。的错误

Not valid.
Error - Line 4, 43: org.xml.sax.SAXParseException; lineNumber: 4; columnNumber: 43; src-resolve.4.2: Error resolving component 'x:tire'. It was detected that 'x:tire' is in namespace 'http://www.w3.org/2001/XMLSchema', but components from this namespace are not referenceable from schema document 'null'. If this is the incorrect namespace, perhaps the prefix of 'x:tire' needs to be changed. If this is the correct namespace, then an appropriate 'import' tag should be added to 'null'.
Error - Line 4, 43: org.xml.sax.SAXParseException; lineNumber: 4; columnNumber: 43; src-resolve: Cannot resolve the name 'x:tire' to a(n) 'type definition' component.

XML

<?xml version="1.0" encoding="UTF-8"?>
<tire trademark="runway" id="369954" product-type="tire">
  <url>http://www.tire.es/product/369954.html</url>
  <price>28.95</price>
  <specs>
    <spec name="b_homologation">0</spec>
    <spec name="b_xl">0</spec>
    <spec name="b_runflat">0</spec>
    <spec name="s_drawing_type">simétrico</spec>
    <spec name="s_consumption">f</spec>
    <spec name="i_noise">69</spec>
    <spec name="s_grip">c</spec>
  </specs>
</tire>

XSD

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

  <xs:element name="tire" type="x:tire" />

  <xs:complexType name="product">
    <xs:sequence>
      <xs:element type="xs:anyURI" name="url" />
      <xs:element type="xs:double" name="price" />

      <xs:element name="specs" maxOccurs="unbounded"
        minOccurs="0">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="spec" maxOccurs="unbounded"
              minOccurs="0">
              <xs:complexType>
                <xs:simpleContent>
                  <xs:extension base="xs:string">
                    <xs:attribute type="xs:string" name="name"
                      use="optional" />
                  </xs:extension>
                </xs:simpleContent>
              </xs:complexType>
            </xs:element>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
      <xs:element name="features" maxOccurs="unbounded"
        minOccurs="0">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="feature" maxOccurs="unbounded"
              minOccurs="0">
              <xs:complexType>
                <xs:simpleContent>
                  <xs:extension base="xs:string">
                    <xs:attribute type="xs:string" name="name"
                      use="optional" />
                  </xs:extension>
                </xs:simpleContent>
              </xs:complexType>
            </xs:element>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
    <xs:attribute type="xs:string" name="id" />
    <xs:attribute type="xs:string" name="product-type" />
  </xs:complexType>

  <xs:complexType name="tire">
    <xs:complexContent>
      <xs:extension base="product">
        <xs:attribute type="xs:string" name="model" />
        <xs:attribute type="xs:string" name="trademark" />
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>

<xs:element name="products">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="tire" maxOccurs="unbounded"
        minOccurs="0" type="x:tire" />
    </xs:sequence>
  </xs:complexType>
</xs:element>

</xs:schema>

新产品会将它们添加为元素。

<xs:element name="glass" maxOccurs="unbounded" minOccurs="0" type="x:glass" />

感谢。

1 个答案:

答案 0 :(得分:2)

类型“轮胎”不在XSD名称空间http://www.w3.org/2001/XMLSchema中,它位于架构文档的目标名称空间中,在本例中为“无名称空间”。所以它应该是type =“tire”而不是type =“x:tire”。