XML DTD Specify a cdata/text pattern for attribute

2019-08-01 11:33发布

问题:

Is there a way, in a DTD, to specify a "pattern" for a given attribute.

Example: I want to have an attribute called "position" which is a string of the form "X,Y".

I would like to have something in my DTD similar to:

<!ATTLIST MyElement 
    myattribute "*,*"
>

(I know, for this example two attributes X and Y would certainly be better, but that's just to highlight what I want to do)

Thanks

回答1:

You can't specify a pattern using DTD. You could do it using a schema though:

  <xs:element name="MyElement">
    <xs:complexType>
      <xs:attribute name="myattribute" use="required">
        <xs:simpleType>
            <xs:restriction base="xs:string">
                <xs:pattern value="[^,]+,[^,]+"/>
            </xs:restriction>
        </xs:simpleType>
      </xs:attribute>
    </xs:complexType>
  </xs:element>

The value in xs:pattern is a regular expression.



标签: xml dtd