Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

xml

xsd

dtd

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.

like image 250
Abdullah Avatar asked May 30 '10 02:05

Abdullah


People also ask

What is DTD Why do we use it explain the different components of DTD?

A Document Type Definition (DTD) describes the tree structure of a document and something about its data. It is a set of markup affirmations that actually define a type of document for the SGML family, like GML, SGML, HTML, XML. A DTD can be declared inside an XML document as inline or as an external recommendation.

How XML is different from XML DTD?

The critical difference between DTDs and XML Schema is that XML Schema utilize an XML-based syntax, whereas DTDs have a unique syntax held over from SGML DTDs. Although DTDs are often criticized because of this need to learn a new syntax, the syntax itself is quite terse.

What is the purpose of DTD relating to an XML document?

The purpose of a DTD is to define the legal building blocks of an XML document. It defines the document structure with a list of legal elements. A DTD can be declared inline in your XML document, or as an external reference.


1 Answers

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)*>
like image 86
bobince Avatar answered Oct 19 '22 01:10

bobince