tags:

views:

64

answers:

3

hi, can you please tell how i can extend the following code so that five professions can be added in the xml document?

<?xml version=”1.0” encoding=”ISO-8859”?>
<!DOCTYPE person [
<!ELEMENT first_name (#PCDATA)>
<!ELEMENT last_name (#PCDATA)>
<!ELEMENT profession (#PCDATA)>
<!ELEMENT name (first_name, last_name)>
<!ELEMENT person (name, profession)>]>
<person>
<name>
<first_name>Jack</first_name>
<last_name>Jill</last_name>
</name>
<profession>website</profession>
</person>

thanks for answering

+3  A: 

Something like this should do

<?xml version=”1.0” encoding=”ISO-8859”?>
<!DOCTYPE person [
    <!ELEMENT first_name ( #PCDATA ) >
    <!ELEMENT last_name ( #PCDATA ) >
    <!ELEMENT name ( first_name, last_name ) >
    <!ELEMENT person ( name, professions ) >
    <!ELEMENT profession ( #PCDATA ) >
    <!ELEMENT professions ( profession+ ) >
]>
<person>
    <name>
        <first_name>Jack</first_name>
        <last_name>Jill</last_name>
    </name>
    <professions>
        <profession>website</profession>
        <profession>some other profession</profession>
    </professions>
</person>
Marek Karbarz
Are you still grabbing the 'name' and 'person' elements that he is asking about?
Chris
I think the problem is with the embedded DTD-type spec at the top of the document, which allows only one profession element per person element.
Larry Lustig
I failed to update the DTD - but this update one should be fine
Marek Karbarz
thanks, it works. how can i modify the code so that one or zero address can be added in the xml code? the one showed me was about one or many. thanks for replying
Selom
+1  A: 

The DTD at the top of the document specifies one name and one profession per person element.

Change it to use + (if you want one or more professions) or * (if you want 0 or more professions):

<!ELEMENT person (name, profession+)

or

<!ELEMENT person (name, profession*)

Note that DTDs are rather out-of-fashion, generally replaced by XSD specifications, which are more flexible and written in XML themselves.

Larry Lustig
thanks, it works. how can i modify the code so that one or zero address can be added in the xml code? the one showed me was about one or many. thanks for replying
Selom
+1  A: 

Change the DTD to

<?xml version=”1.0” encoding=”ISO-8859”?>
<!DOCTYPE person [
<!ELEMENT first_name (#PCDATA)>
<!ELEMENT last_name (#PCDATA)>
<!ELEMENT profession (#PCDATA)>
<!ELEMENT professions (profession*)>
<!ELEMENT name (first_name, last_name)>
<!ELEMENT person (name, professions)>]>
<person>
<name>
<first_name>Jack</first_name>
<last_name>Jill</last_name>
</name>
<professions>
 <profession>prof 1</profession>
 <profession>prof 2</profession>
</professions>
</person>
mqbt
thanks, it works. how can i modify the code so that one or zero address can be added in the xml code? the one showed me was about one or many. thanks for replying
Selom
You mean profession right? Simply change the `*` to `?`.
mqbt
thanks a lot for the answer.
Selom