How to use the same element name for different purposes ( in XML and DTD )?
Asked Answered
T

1

10

I Want to create a DTD schema for this xml document:

<root>

    <student>
        <name>
            <firstname>S1</firstname>
            <lastname>S2</lastname>
        </name>
    </student>

    <course>
        <name>CS101</name>
    </course>

</root>

as you can see , the element name in the course contains plain text ,but the element name in the student is complex type ( first-name, last-name ). The following is the DTD:

<!ELEMENT root (course|student)*>

<!ELEMENT student (name)>
<!ELEMENT name (lastname|firstname)>
<!ELEMENT firstname (#PCDATA)>
<!ELEMENT lastname (#PCDATA)>

<!ELEMENT course (name)>

When I want to validate it , I get an error because the course's name has different structure then the student's name .

My Question:

  • how can I make a work-around solution for this situation without changing the name of element name using DTD not xml schema .

Thanks.

Traveler answered 30/5, 2010 at 2:19 Comment(0)
C
12

Sorry! That's one of the major limitations of DTD: a given element name always has the same content model.

About all you can do is give up on full validation for that particular element, and allow all possible contents:

<!ELEMENT name (#PCDATA|lastname|firstname)*>
Crazy answered 30/5, 2010 at 2:38 Comment(2)
Thanks @Crazy , I'll use what you wrote as a work-around ,it's easier then XML-schema .Traveler
When using PCDATA this is the best you can do, but if all three possibilities are elements you can get slightly better results. Consider element <p>. Element <p> may contain either element <a>, or elements <b> and <c>. We can specify this, it just won't be contextual (ie, dependant of <p>'s parent). <!ELEMENT p (a | (b, c))>. PCDATA has to be used as mixed content or all by itself; it can't be an either/or situation, unfortunately.Grist

© 2022 - 2024 — McMap. All rights reserved.