我不知道我的问题是否被正确询问。我有一个包含DTD的XML文档。我已经定义了ATTLIST并且我想限制或修改XML验证将导致错误或将失败的DTD,如果我多次使用相同的属性。 我的XML文档如下所示:
<?xml version="1.0" encoding="utf-8"?>
<!-- DTD START -->
<!DOCTYPE dtype
[
<!ELEMENT dtype (ea?, eb?, ec, ed?) >
<!ELEMENT ea (data+) >
<!ELEMENT eb (ee+) >
<!ELEMENT ec (data+) >
<!ELEMENT ed (data+) >
<!ELEMENT ee (data+) >
<!ELEMENT data (#PCDATA) >
<!ATTLIST ee
ef CDATA #REQUIRED
>
<!ATTLIST data
element CDATA #REQUIRED
value CDATA #IMPLIED
unit CDATA #IMPLIED
min CDATA #IMPLIED
max CDATA #IMPLIED
avg CDATA #IMPLIED
>
]>
<!-- DTD END -->
<!-- DATA START -->
<dtype>
<ec>
<data element="A" unit="counts" min="0.6621" avg="0.6819" max="0.7089" />
<data element="B" unit="counts" min="0.0463" avg="0.0473" max="0.0503" />
<data element="C" unit="counts" min="0.1238" avg="0.1264" max="0.1344" />
</ec>
</dtype>
<!-- DATA END -->
<!-- should be detected as not ok ! -->
<!--
<dtype>
<ec>
<data element="A" unit="counts" min="0.6621" avg="0.6819" max="0.7089" />
<data element="B" unit="counts" min="0.0463" avg="0.0473" max="0.0503" />
<data element="B" unit="counts" min="0.1238" avg="0.1264" max="0.1344" />
<data element="C" unit="counts" min="0.1348" avg="0.1644" max="0.1654" />
</ec>
</dtype>
-->
我想将DTD限制为允许一个属性element="B"
,例如。
这可能吗?
答案 0 :(得分:0)
我认为DTD中唯一的选择是创建属性类型ID
。 ID
必须是Name
(http://www.w3.org/TR/REC-xml/#NT-Name),因此这可能对您的数据无效。 (虽然它适用于样本数据中的element
属性。)
例如,此XML无效,因为element="A"
被使用了两次:
<!DOCTYPE dtype
[
<!ELEMENT dtype (ea?, eb?, ec, ed?) >
<!ELEMENT ea (data+) >
<!ELEMENT eb (ee+) >
<!ELEMENT ec (data+) >
<!ELEMENT ed (data+) >
<!ELEMENT ee (data+) >
<!ELEMENT data (#PCDATA) >
<!ATTLIST ee
ef CDATA #REQUIRED
>
<!ATTLIST data
element ID #REQUIRED
value CDATA #IMPLIED
unit CDATA #IMPLIED
min CDATA #IMPLIED
max CDATA #IMPLIED
avg CDATA #IMPLIED
>
]>
<!-- DTD END -->
<!-- DATA START -->
<dtype>
<ec>
<data element="A" unit="counts" min="0.6621" avg="0.6819" max="0.7089" />
<data element="B" unit="counts" min="0.0463" avg="0.0473" max="0.0503" />
<data element="A" unit="counts" min="0.1238" avg="0.1264" max="0.1344" />
</ec>
</dtype>