PDA

View Full Version : XML DTD Help


TheJoey
03-30-2010, 08:56 AM
Hello, i really need help on trying to get my dtd to work.

What im trying to do is create a dtd to give the users only a few selections avaible that would be valid.

this is what i have been trying
<!ELEMENT root (option) >
<!ELEMENT option (option1|option2|option3)

Where only if option 1 -3 is put in the xml field it will only work.

Do i need to use attlist?

Dormilich
03-30-2010, 09:07 AM
What im trying to do is create a dtd to give the users only a few selections avaible that would be valid.

please define "valid" (example) clearly, since upon that depends the DTD structure.

an ATTLIST defines an element's attributes.

TheJoey
03-30-2010, 01:47 PM
umm well what i mean is that

i have a xml structure that is

<root>
<option>
</option>
</root>

the only values i want that the xml will accept is option1 or option2 or option3 other wise it when i test it in a parser it will say invalid.

i guess by valid i mean accepted.

Dormilich
03-30-2010, 02:16 PM
you mean
<root>
<option>option1</option>
</root>
?

afaik, a DTD cannot limit the content of a text node. although it would be possible for attributes.
<!ATTLIST option
value (option1|option2|option3) "option1"
>

TheJoey
03-30-2010, 03:04 PM
Thank you for the help, may i ask why
<!ATTLIST option
value (option1|option2|option3) "option1"
>
at the end of the brackets "option1" is there?

Dormilich
03-30-2010, 03:05 PM
Thank you for the help, may i ask why at the end of the brackets "option1" is there?

that’s the default value (it’s required to provide one).

TheJoey
03-30-2010, 03:11 PM
Would i need to declare a element before i make the attribute?

such as <!ELEMENT option (#PCDATA)*>

thank you so much

Dormilich
03-30-2010, 03:11 PM
of course.

TheJoey
03-30-2010, 03:17 PM
Well it seems to parse fine, but i think the fact that i have PCDATA in the element means it accepts anything :(

Dormilich
03-30-2010, 03:20 PM
right, you wouldn’t be able to write HTML pages, if a DTD were that restrictive. it may be possible with XML Schema, but not sure about that.

TheJoey
03-30-2010, 03:26 PM
Well its just for my playing around really, but ive been determined to learn how!

its driving me nuts.

Is there another way to avoid using #PCDATA?

Dormilich
03-30-2010, 03:26 PM
no .

TheJoey
03-30-2010, 03:35 PM
So what i am doing wrong if it seems to accept everything i put inside the xml even after declaring that attribute


<root>
<option>ghghghg</option>
</root>


<!ELEMENT root (option) >
<!ELEMENT option (#PCDATA) >
<!ATTLIST option value (option1|option2|option3) "option1" > should i try a #required?

Dormilich
03-30-2010, 03:36 PM
you can only try.

TheJoey
03-30-2010, 03:44 PM
It will not parse gives me message
Expected Attribute name

Dormilich
03-30-2010, 03:47 PM
well, then obviously #REQUIRED doesn’t work for this case.

TheJoey
03-30-2010, 04:06 PM
Tried many different ways all seem to ignore the rules im creating in the DTD.

<!ATTLIST option value(could this be the problem) (option1|option2) "option1">

it has to be something im doing wrong
<!DOCTYPE root SYSTEM "root.dtd">
<root>
<option>
dsfsdf
</option>
</root>
[CODE]<!ELEMENT root (option)>
<!ELEMENT option (#PCDATA)>
<!ATTLIST option
option (option1|option2) "option1" >[/CODE

when i parse it i get no errors, when i should be

Arbitrator
03-31-2010, 07:05 AM
Tried many different ways all seem to ignore the rules im creating in the DTD.An attribute declaration only allows you to force a structure on attributes; your structure contains no attributes, so I'm not sure why you're expecting an attribute declaration to have any effect.

You ultimately appear to be trying to limit the text data of an element to one of three strings. XML DTDs are not powerful enough to do that; you can control whether parsed character data and/or elements are allowed, whether child elements are required, and in what order elements should appear, and that's it.

Presumably, you need XML Schema which is a much more powerful language intended to replace DTDs for the purpose of document structure validation. Unfortunately, I don't know that language so I can't help you further.

Dormilich
03-31-2010, 07:12 AM
when i parse it i get no errors, when i should be

why should there be errors? you have a default value.

TheJoey
03-31-2010, 11:41 AM
but there is "dfgdfdsfsDF" inside <option></option> doesnt that make it invalid.

So if there isnt one of the choices it automaticaly makes it option1?

Dormilich
03-31-2010, 11:49 AM
the coice is about the value attribute, the dfgdfdsfsDF counts as #PCDATA.

TheJoey
03-31-2010, 11:59 AM
Well thats what i want to be restricting the information inside the <option> in this case, dfgdfdsfsDF, if that is doable..

Dormilich
03-31-2010, 12:04 PM
as already mentioned, you can’t do that with a DTD.

TheJoey
03-31-2010, 12:07 PM
Thought it would be possible considering, u can with xsd (schema)