XML DTD Specify a cdata/text pattern for attribute

2019-08-01 11:05发布

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

标签: xml dtd
1条回答
【Aperson】
2楼-- · 2019-08-01 11:24

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.

查看更多
登录 后发表回答