简体   繁体   中英

XSD the element cannot contain text. content model is empty after adding xs:unique

Below is my XML and the validating XSD. looks like there is something wrong with my xs:unique constraint for the id field.

<?xml version='1.0' encoding='utf-8'?>
<records>
  <record>
    <date>2016-02-01</date>
    <id>3</id>       
  </record>
  <record>
    <date>2016-02-01</date>
    <id>4</id>
  </record>
  <record>
    <date>2016-02-01</date>
    <id>7</id>
  </record>
</records>

XSD:

 <xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
      <xs:element name="records">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="record" maxOccurs="unbounded" minOccurs="0">
              <xs:complexType>
                <xs:sequence>
                  <xs:element type="xs:date" name="date"/>
                  <xs:element name="id" maxOccurs="unbounded">
                    <xs:complexType>
                      <xs:attribute name="recordid" type="xs:integer"/>
                    </xs:complexType>
                  </xs:element>
                </xs:sequence>
              </xs:complexType>
              <xs:unique name="Unique-id">
                <xs:selector xpath="id" />
                <xs:field xpath="@recordid" />
              </xs:unique>
            </xs:element>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:schema>

Your XSD is not exactly related to your XML : in your XSD you mention an attribute recordid that doesn't exist in your code

Supposing your XML is correct, here is the corresponding XSD that should give you the correct result

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="unqualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="records">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="record" maxOccurs="unbounded" minOccurs="0">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element type="xs:date" name="date"/>
                        <xs:element name="id" type="xs:integer"/>                            
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:sequence>            
    </xs:complexType>
    <xs:unique name="Unique-id">
        <xs:selector xpath="record" />
        <xs:field xpath="id" />
    </xs:unique>                                
</xs:element>

I got same problem with XML like:

<SomeRoot>
  <SomeElement SomeAttribute="1"></SomeElement>
</SomeRoot>

The problem was in tag SomeElement, it's contain empty string which count as text. So solution for me was - use self close tag:

<SomeRoot>
  <SomeElement SomeAttribute="1" />
</SomeRoot>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM