Docsity

XML and HTML: A Comparison of Markup Languages, Slides of Computer Science

Partial preview of the text

Download XML and HTML: A Comparison of Markup Languages and more Slides Computer Science in PDF only on Docsity!

XML

HTML and XML, I

XML stands for eXtensible Markup Language

HTML is used to mark up text so it can be displayed to users

XML is used to mark up data so it can be processed by computers

HTML describes both structure (e.g.

,

, ) and appearance (e.g.
, , )

XML describes only content, or “meaning”

HTML uses a fixed, unchangeable set of tags

In XML, you make up your own tags

HTML and XML, III

• HTML is for humans

  • HTML describes web pages
  • You don’t want to see error messages about the

web pages you visit

  • Browsers ignore and/or correct as many HTML

errors as they can, so HTML is often sloppy

• XML is for computers

  • XML describes data
  • The rules are strict and errors are not allowed
    • In this way, XML is like a programming language
  • Current versions of most browsers can display XML
    • However, browser support of XML is spotty at best

XML-related technologies

  • DTD (Document Type Definition) and XML Schemas are used to define legal XML tags and their attributes for particular purposes
  • CSS (Cascading Style Sheets) describe how to display HTML or XML in a browser
  • XSLT (eXtensible Stylesheet Language Transformations) and XPath are used to translate from one form of XML to another
  • DOM (Document Object Model), SAX (Simple API for XML, and JAXP (Java API for XML Processing) are all APIs for XML parsing

Overall structure

• An XML document may start with one or more

processing instructions (PIs) or directives:

• Following the directives, there must be exactly

one root element containing all the rest of the

XML:

...

XML building blocks

• Aside from the directives, an XML document

is built from:

  • elements: high in 103
  • tags, in pairs: 103
  • attributes: 103
  • entities: Sunny & hot
  • character data, which may be:
    • parsed (processed as XML)--this is the default
    • unparsed (all characters stand for themselves)

Well-formed XML

  • Every element must have both a start tag and an end tag, e.g. ... - But empty elements can be abbreviated: . - XML tags are case sensitive - XML tags may not begin with the letters xml, in any combination of cases
  • Elements must be properly nested, e.g. not bold and italic
  • Every XML document must have one and only one root element
  • The values of attributes must be enclosed in single or doubleDocsity.com

Entities

• Five special characters must be written as

entities:

& for & (almost always necessary) < for < (almost always necessary) > for > (not usually necessary) " for " (necessary inside double quotes) ' for ' (necessary inside single quotes)

• These entities can be used even in places

where they are not absolutely required

• These are the only predefined entities in XMLDocsity.com

Processing instructions

  • PIs (Processing Instructions) may occur anywhere in the XML document (but usually first)
  • A PI is a command to the program processing the XML document to handle it in a certain way
  • XML documents are typically processed by more than one program
  • Programs that do not recognize a given PI should just ignore it
  • General format of a PI:
  • Example:

Comments

  • Comments can be put anywhere in an XML document
  • Comments are useful for:
    • Explaining the structure of an XML document
    • Commenting out parts of the XML during development and testing
  • Comments are not elements and do not have an end tag
  • The blanks after are optional
  • The character sequence -- cannot occur in the comment
  • The closing bracket must be -->
  • Comments are not displayed by browsers, but can be seen by anyone who looks at the source code

Names in XML

• Names (as used for tags and attributes) must

begin with a letter or underscore, and can

consist of:

  • Letters, both Roman (English) and foreign
  • Digits, both Roman and foreign . (dot)
    • (hyphen) _ (underscore) : (colon) should be used only for namespaces
  • Combining characters and extenders (not used inDocsity.com

Namespaces

• Recall that DTDs are used to define the tags

that can be used in an XML document

• An XML document may reference more than

one DTD

• Namespaces are a way to specify which DTD

defines a given tag

• XML, like Java, uses qualified names

  • This helps to avoid collisions between names
  • Java: myObject.myVariable
  • XML: myDTD:myTag Docsity.com

Namespace syntax

  • In any start tag you can use the reserved attribute name xmlns:

  • This namespace will be used as the default for all elements up to the corresponding end tag

  • You can override it with a specific prefix

  • You can use almost this same form to declare a prefix:

  • Use this prefix on every tag and attribute you want to use from this namespace, including end tags--it is not a default prefix To Begin

  • You can use the prefix in the start tag in which it is defined:

Review of XML rules

• Start with

• XML is case sensitive

• You must have exactly one root element that

encloses all the rest of the XML

• Every element must have a closing tag

• Elements must be properly nested

• Attribute values must be enclosed in double

or single quotation marks

• There are only five predeclared entities