Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts

    can a DTD specify that an element must contain data, or not exist at all

    here's my DTD

    Code:
    <!DOCTYPE pubinfo [
    
    <!ELEMENT pubinfo (publication)*>
    <!ELEMENT publication (title,url?,date?,size?,source?,author?)>
    <!ELEMENT title (#PCDATA)>
    <!ELEMENT url (#PCDATA)>
    <!ELEMENT date (#PCDATA)>
    <!ELEMENT size (#PCDATA)>
    <!ELEMENT source (#PCDATA)>
    <!ELEMENT author (#PCDATA)>
    
    <!ATTLIST publication cat CDATA #REQUIRED>
    <!ATTLIST publication sub CDATA #REQUIRED>
    <!ATTLIST publication sba CDATA #REQUIRED>
    
    ]>

    Most of the children of <publication> are optional, but I want to specify that, for example

    <url />

    is not allowed - each optional tag-pair must either not be there, or contain data.

    The reason I want to do this is to suit the parsing script. Is it bad practise to validate an XML so specifically, for the benefit of the parsing algorithm, or is it okay to do that? - and if so, how do I specify that in the DTD?

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    Newcastle, England
    Posts
    178
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I ws under the impression that if you specify an element contains PCDATA then it cannot be empty and still be valid.

    patrick

    No - I'm wrong!
    Last edited by mpjbrennan; 10-17-2002 at 06:32 PM.

  • #3
    Moderator
    Join Date
    May 2002
    Location
    Hayward, CA
    Posts
    1,461
    Thanks
    1
    Thanked 23 Times in 21 Posts
    You can require the element have a child element. But that's about the best you'll be able to do in the DTD.

    XML Schemas are much more defining and more strict, but they are incredibly complex.

    If the XML document is in a Gecko-based browser like Netscape 6+ or Mozilla 1.0+, you can use a JavaScript with it:

    <script xmlns="http://www.w3.org/1999/xhtml" src="myscript.js" />

    -- and do your validation from there.
    "The first step to confirming there is a bug in someone else's work is confirming there are no bugs in your own."
    June 30, 2001
    author, Verbosio prototype XML Editor
    author, JavaScript Developer's Dictionary
    https://alexvincent.us/blog

  • #4
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by Alex Vincent
    you can use a JavaScript with it -- and do your validation from there.
    You mean use the script to walk through the DOM and validate it like that? I hadn't thought of that ...

    ... but, i'm not DOM parsing it anyway. I'm doing it on the server using PHP extensions. It doesn't really validate; it just throws errors for malformed documents.

    I remember reading a mention of Schema at w3schools; I think I'll check that out.

    Thanks


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •