简体   繁体   English

XML和DTD中的ID和IDREF配置

[英]ID and IDREF configuration in XML and DTD

This is my first attempt at XML and I'm not able to validate the xml at all. 这是我对XML的第一次尝试,我根本无法验证xml。 The DTD gets validated, but not the XML. DTD得到验证,但不是XML。 I'm not able to figure out the ID and IDREF part. 我无法找出ID和IDREF部分。 Can someone tell me how I can fix this as I keep getting the error "Element type must be declared" particularly for medID and docID when I try to validate. 有人可以告诉我如何解决这个问题,因为当我尝试验证时,我不断收到错误“必须声明元素类型”,特别是对于medID和docID。

Here's the XML 这是XML

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE patient_list SYSTEM "patients.dtd">
<patient_list>
<patient serialNo="a1b2c3">
<patientFName>Jon</patientFName>
<patientLName>Won</patientLName>
<gender>male</gender>
<medID IDREF="M1"/>
<docID IDREF="D37"/>
</patient>

<patient serialNo="k4t5g2">
<patientFName>Min</patientFName>
<patientLName>Hin</patientLName>
<gender>female</gender>
<medID IDREF="M2"/>
<docID IDREF="D21"/>
</patient>

<medicine medID="M1">
<name>Panadol</name>
<manufacture>GSK</manufacture>
<regNo>oo9807</regNo>
</medicine>

<medicine medID="M2">
<name>Alprazolam</name>
<manufacture>Novartis</manufacture>
<regNo>hu5432</regNo>
</medicine>

<doctor docID="D21">
<registration ID="8472392"/>
<fName>Alfred</fName>
<lName>Campbell</lName>
<specialisation>Paediatrics</specialisation>
</doctor>

<doctor docID="D37">
<registration ID="4364786"/>
<fName>Mick</fName>
<lName>Foley</lName>
<specialisation>Psychiatry</specialisation>
</doctor>
</patient_list>

and here's the DTD: 这是DTD:

<!ELEMENT patient_list (patient+, medicine+, doctor+)>
<!ELEMENT patient (patientFName, patientLName, gender)>
<!ATTLIST patient serialNo CDATA #REQUIRED>
<!ELEMENT patientFName (#PCDATA)>
<!ELEMENT patientLName (#PCDATA)>
<!ELEMENT gender (#PCDATA)>

<!ATTLIST medicine medID ID #REQUIRED>
<!ATTLIST doctor docID ID #REQUIRED>

<!ELEMENT medicine (name, manufacture, regNo)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT manufacture (#PCDATA)>
<!ELEMENT regNo (#PCDATA)>

<!ELEMENT doctor (registration, fName, lName, specialisation)>
<!ELEMENT registration (#PCDATA)>
<!ATTLIST registration ID CDATA #REQUIRED>
<!ELEMENT fName (#PCDATA)>
<!ELEMENT lName (#PCDATA)>
<!ELEMENT specialisation (#PCDATA)>

I think the attributes medID on medicine and docID on doctor are fine. 我认为属性medIDmedicinedocIDdoctor的罚款。 They're both declared as the attribute type ID like they should be. 它们都被声明为属性类型ID就像它们应该的那样。

I think where your problem is happening is when you try to use medID and docID as elements without declaring them. 我认为你的问题出现在你尝试使用medIDdocID作为元素时,而不是声明它们。 Instead of using the same name as the medID and docID attributes, name them something different like medref and docref . 而不是使用作为同名medIDdocID属性,他们的名字像不同medrefdocref

The attribute IDREF would need to be declared too, however IDREF is another attribute type so you should name the attribute something else like refid . 还需要声明属性IDREF ,但IDREF是另一种属性类型,因此您应该将属性命名为refid

Full example... 完整的例子......

XML XML

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE patient_list SYSTEM "patients.dtd">
<patient_list>
    <patient serialNo="a1b2c3">
        <patientFName>Jon</patientFName>
        <patientLName>Won</patientLName>
        <gender>male</gender>
        <medref refid="M1"/>
        <docref refid="D37"/>
    </patient>

    <patient serialNo="k4t5g2">
        <patientFName>Min</patientFName>
        <patientLName>Hin</patientLName>
        <gender>female</gender>
        <medref refid="M2"/>
        <docref refid="D21"/>
    </patient>

    <medicine medID="M1">
        <name>Panadol</name>
        <manufacture>GSK</manufacture>
        <regNo>oo9807</regNo>
    </medicine>

    <medicine medID="M2">
        <name>Alprazolam</name>
        <manufacture>Novartis</manufacture>
        <regNo>hu5432</regNo>
    </medicine>

    <doctor docID="D21">
        <registration ID="8472392"/>
        <fName>Alfred</fName>
        <lName>Campbell</lName>
        <specialisation>Paediatrics</specialisation>
    </doctor>

    <doctor docID="D37">
        <registration ID="4364786"/>
        <fName>Mick</fName>
        <lName>Foley</lName>
        <specialisation>Psychiatry</specialisation>
    </doctor>
</patient_list>

DTD DTD

<!ELEMENT patient_list (patient+, medicine+, doctor+)>
<!ELEMENT patient (patientFName, patientLName, gender, medref*, docref*)>
<!ATTLIST patient serialNo CDATA #REQUIRED>
<!ELEMENT patientFName (#PCDATA)>
<!ELEMENT patientLName (#PCDATA)>
<!ELEMENT gender (#PCDATA)>

<!ATTLIST medicine medID ID #REQUIRED>
<!ATTLIST doctor docID ID #REQUIRED>

<!ELEMENT medref EMPTY>
<!ATTLIST medref refid IDREF #REQUIRED>

<!ELEMENT docref EMPTY>
<!ATTLIST docref refid IDREF #REQUIRED>

<!ELEMENT medicine (name, manufacture, regNo)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT manufacture (#PCDATA)>
<!ELEMENT regNo (#PCDATA)>

<!ELEMENT doctor (registration, fName, lName, specialisation)>
<!ELEMENT registration (#PCDATA)>
<!ATTLIST registration ID CDATA #REQUIRED>
<!ELEMENT fName (#PCDATA)>
<!ELEMENT lName (#PCDATA)>
<!ELEMENT specialisation (#PCDATA)>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM