How do I declare attributes common to multiple elements?

元气小坏坏 提交于 2019-12-08 19:11:46

问题


I have multiple elements I want to give these attributes:

<!ATTLIST [all these elements]
    width   CDATA   "0"
    height  CDATA   "0"
    margin  CDATA   "0 0 0 0"
    padding CDATA   "0 0 0 0"
    rotation CDATA  "0"
    halign  (left|center|right|full)    "center"
    valign  (top|middle|bottom|full)    "middle"
    >

Is this possible somehow in DTD, or will I have to do it manually?

(Also, while I'm here, I don't think it was such a good idea to declare the margin and padding attributes that way. Does anyone know a better way?)


回答1:


Each element needs to have its own attribute declaration (ATTLIST). However, you can use a parameter entity to reuse the bulk of it.

Example...

<!ENTITY % attrs 
    'width   CDATA   "0"
     height  CDATA   "0"
     margin  CDATA   "0 0 0 0"
     padding CDATA   "0 0 0 0"
     rotation CDATA  "0"
     halign  (left|center|right|full)    "center"
     valign  (top|middle|bottom|full)    "middle"'>

<!ELEMENT elem1 (#PCDATA)>
<!ATTLIST elem1 %attrs;>

<!ELEMENT elem2 (#PCDATA)>
<!ATTLIST elem2 %attrs;>

Here's another example that has a mix of the parameter entity references along with attributes that only appear on the individual elements.

<!ELEMENT elem1 (#PCDATA)>
<!ATTLIST elem1 
    attr1 CDATA #IMPLIED
    %attrs;              >

<!ELEMENT elem2 (#PCDATA)>
<!ATTLIST elem2 
    attr2 CDATA #IMPLIED
    %attrs;              >


来源:https://stackoverflow.com/questions/20308365/how-do-i-declare-attributes-common-to-multiple-elements

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!