A Complete Guide to Web Design

(やまだぃちぅ) #1
XML Syntax Basics 447

XML

XML Syntax Basics

Web Design in a Nutshell, eMatter Edition

tags unique to that document and therefore can’t make assumptions about
whether or not a tag should be closed. Every XML element must be closed.


Like HTML, XML tags cannot overlap. Overlapping tags look like this:


<Element1><Element2>This is content contained</Element1> in
overlapping tags</Element2>

In the above example, it is unclear whether the text, “This is content contained” is
an Element 1 or Element 2. To avoid such confusion, an XML document cannot
contain overlapping tags. The above example should be written like this:


<Element1><Element2>This is content contained</Element2>
</Element1><Element2> in overlapping tags</Element2>

With this code, there is no question as to which tags or objects are contained
within others.


Turning Existing HTML Documents into XML


Because HTML and XML are closely related, it isn’t difficult to make an HTML
document XML-compliant. You basically have to make sure your HTML is “well-
formed.”



  • Replace theDOCTYPEdeclaration and any internal subset with the XML decla-
    ration. Replace:
    <!DOCTYPE HTML ...>
    with:
    <?xml version="1.0" standalone="yes"?>

  • Change any empty elements such as,,,,

    ,
    , or so they end with/>, for example:Photo
    These elements may require some experimentation. For instance, some
    browsers treat
    or the same as
    or
    . Others will accept
    /> if there is a space before it, but not otherwise.

  • Make sure that each nonempty element has a correctly matched end-tag;
    every

    must have a

    .

  • Escape all markup characters. (< and &should be written as < and
    &).

  • Make sure all attribute values are in quotes.

  • Ensure all element names match with respect to upper- and lowercase charac-
    ters in both start and end tags and are consistent throughout the file.

  • Ensure all attribute tags are similarly in a consistent case throughout the file.

  • Make sure there are no overlapping tags. Each tag should completely contain
    any tags within it.


HTML browsers may not accept XML style empty elements with a trailing slash (e.g.,



) and are not backward compatible. If you want your XML document read
Free download pdf