简体   繁体   中英

From DTD to XSD (Document Type Definition to XML Schema)

I have this DTD:

<!DOCTYPE a [
  <!ELEMENT a (b?, c*, (d|e)+)>
  <!ELEMENT b (#PCDATA)>
  <!ELEMENT c (#PCDATA)>
  <!ELEMENT d (#PCDATA)>
  <!ELEMENT e EMPTY>
  <!ATTLIST c attr CDATA #IMPLIED>
]>

I want to convert (manually not by program) it to XML Schema, but I don't understand how do I create attribute for the c element.

Here's how in XSD to declare that c has string content and an attribute named attr :

        <xs:element name="c" minOccurs="0" maxOccurs="unbounded">
          <xs:complexType>
            <xs:simpleContent>
              <xs:extension base="xs:string">
                <xs:attribute name="attr" type="xs:string"/>
              </xs:extension>
            </xs:simpleContent>
          </xs:complexType>

XSD

Here's the entire DTD written as an XSD:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="a">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="b" minOccurs="0" type="xs:string"/>
        <xs:element name="c" minOccurs="0" maxOccurs="unbounded">
          <xs:complexType>
            <xs:simpleContent>
              <xs:extension base="xs:string">
                <xs:attribute name="attr" type="xs:string"/>
              </xs:extension>
            </xs:simpleContent>
          </xs:complexType>
        </xs:element>
        <xs:choice maxOccurs="unbounded">
          <xs:element name="d" type="xs:string"/>
          <xs:element name="e">
            <xs:complexType/>
          </xs:element>
        </xs:choice>        
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

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