欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

XML DTD XSD Study Guide

程序员文章站 2022-05-30 12:27:27
...

http://www.w3schools.com/schema/schema_intro.asp

http://www.w3schools.com/xml/default.asp

http://www.w3schools.com/dtd/default.asp

 

http://www.w3schools.com/

 

 

 

XML was designed to transport and store data.

HTML was designed to display data.

 

The declaration is not a part of the XML document itself, and it has no closing tag

XML Tags are Case Sensitive

 

Entity References

Some characters have a special meaning in XML.

If you place a character like "<" inside an XML element, it will generate an error because the parser interprets it as the start of a new element.

only < is illegal..

 

With XML, White Space is Preserved

XML Stores New Line as LF

In Unix applications, a new line is normally stored as a LF character.

Macintosh applications use only a CR character to store a new line.

Windows use both CR and LF to store a new line.

 

 

attributes cannot contain multiple values (elements can) attributes cannot contain tree structures (elements can) attributes are not easily expandable (for future changes)

 

 

XML with correct syntax is "Well Formed" XML.

XML validated against a DTD is "Valid" XML.

 

 

 

 

 

 

 

 

 

 

 

 

 

A DTD can be declared inline inside an XML document, or as an external reference.

<?xml version="1.0"?>
<!DOCTYPE note [
  <!ELEMENT note (to,from,heading,body)>
  <!ELEMENT to      (#PCDATA)>
  <!ELEMENT from    (#PCDATA)>
  <!ELEMENT heading (#PCDATA)>
  <!ELEMENT body    (#PCDATA)>
]>
<note>
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget me this weekend</body>
</note>

(there must be a space character after the element name .)

 

 

 

Seen from a DTD point of view, all XML documents (and HTML documents) are made up by the following building blocks:

  • Elements
  • Attributes
  • Entities
  • PCDATA (PCDATA means parsed character data.The text will be examined by the parser for entities and markup.)
  • CDATA (Tags inside the text will NOT be treated as markup and entities will not be expanded.)

 

<!ELEMENT element-name category>
or
<!ELEMENT element-name (element-content)>

 

Empty Elements

<!ELEMENT element-name EMPTY>
Example:
<!ELEMENT br EMPTY>

Elements with Parsed Character Data

<!ELEMENT element-name (#PCDATA)>
Example:
<!ELEMENT from (#PCDATA)>

 

Elements with Parsed Character Data

<!ELEMENT element-name ANY>
Example:
<!ELEMENT note ANY>

Elements with Children (sequences)

<!ELEMENT element-name (child1)>
or
<!ELEMENT element-name (child1,child2,...)>
Example:
<!ELEMENT note (to,from,heading,body)>

When children are declared in a sequence separated by commas, the children must appear in the same sequence in the document

 

Declaring Only One Occurrence of an Element 

<!ELEMENT element-name (child-name)>

 

Declaring Minimum One Occurrence of an Element

<!ELEMENT element-name (child-name+)>

 

Declaring Zero or More Occurrences of an Element 

<!ELEMENT element-name (child-name*)>

 

Declaring Zero or One Occurrences of an Element 

<!ELEMENT element-name (child-name?)>

 

Declaring either/or Content

<!ELEMENT note (to,from,header,(message|body))>

 

Declaring Mixed Content

<!ELEMENT note (#PCDATA|to|from|header|message)*>

 

 

 

 

 

 

 

 

 

 

 

DTD - Attributes

In a DTD, attributes are declared with an ATTLIST declaration.

Declaring Attributes

<!ATTLIST element-name attribute-name attribute-type default-value>
DTD example:
<!ATTLIST payment type CDATA "check">
XML example:
<payment type="check" />

 

The attribute-type can be one of the following:

Type Description

CDATA

The value is character data

(en1|en2|..)

The value must be one from an enumerated list

ID

The value is a unique id

IDREF

The value is the id of another element

IDREFS

The value is a list of other ids

NMTOKEN

The value is a valid XML name

NMTOKENS

The value is a list of valid XML names

ENTITY

The value is an entity

ENTITIES

The value is a list of entities

NOTATION

The value is a name of a notation

xml:

The value is a predefined xml value

 

The default-value can be one of the following:

Value Explanation

value

The default value of the attribute

#REQUIRED

The attribute is required

#IMPLIED

The attribute is not required

#FIXED value

The attribute value is fixed

 

 

 <!ATTLIST to content (tw|china|USA|English) "china">

<!ATTLIST note type CDATA #REQUIRED>

 

 

 

 

 

 

 

dispaly xml with css or xsl.

 

 

 

 

Use attribute instead of element when dealt with id .

 

 

 

 

 

 

 

 

 

 

 

DTD - Entities

Entity references are references to entities

Entities can be declared internal or external

 

<!ENTITY entity-name "entity-value">

 

 

 

 

XML Namespaces - The xmlns Attribute

Name conflicts in XML can easily be avoided using a name prefix.

 

<h:table xmlns:h="http://www.w3.org/TR/html4/">
   <h:tr>
   <h:td>Apples</h:td>
   <h:td>Bananas</h:td>
   </h:tr>
</h:table>

 Default Namespaces

<table xmlns="http://www.w3.org/TR/html4/">
   <tr>
   <td>Apples</td>
   <td>Bananas</td>
   </tr>
</table>

 

Namespaces in Real Use

XSLT is an XML language that can be used to transform XML documents into other formats, like HTML.

In the XSLT document below, you can see that most of the tags are HTML tags.

The tags that are not HTML tags have the prefix xsl, identified by the namespace xmlns:xsl="http://www.w3.org/1999/XSL/Transform":

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
  <h2>My CD Collection</h2>
  <table border="1">
    <tr>
      <th align="left">Title</th>
      <th align="left">Artist</th>
    </tr>
    <xsl:for-each select="catalog/cd">
    <tr>
      <td><xsl:value-of select="title"/></td>
      <td><xsl:value-of select="artist"/></td>
    </tr>
    </xsl:for-each>
  </table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

 

 

 

 

 

 

 

 

XSD:

With an extensible Schema definition you can:

  • Reuse your Schema in other Schemas
  • Create your own data types derived from the standard types
  • Reference multiple schemas in the same document

 

The <schema> element may contain some attributes. A schema declaration often looks something like this:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com"
xmlns="http://www.w3schools.com"
elementFormDefault="qualified">
...
...
</xs:schema>

The following fragment:

xmlns:xs="http://www.w3.org/2001/XMLSchema"

indicates that the elements and data types used in the schema come from the "http://www.w3.org/2001/XMLSchema" namespace. It also specifies that the elements and data types that come from the "http://www.w3.org/2001/XMLSchema" namespace should be prefixed with xs:

This fragment:

targetNamespace="http://www.w3schools.com"

indicates that the elements defined by this schema (note, to, from, heading, body.) come from the "http://www.w3schools.com" namespace.

This fragment:

xmlns="http://www.w3schools.com"

indicates that the default namespace is "http://www.w3schools.com".

This fragment:

elementFormDefault="qualified"

indicates that any elements used by the XML instance document which were declared in this schema must be namespace qualified.

 

 

 

 

Defining a Simple Element

The syntax for defining a simple element is:

<xs:element name="xxx" type="yyy"/>

 

Default and Fixed Values for Simple Elements

In the following example the default value is "red":

<xs:element name="color" type="xs:string" default="red"/>

 

In the following example the fixed value is "red":

<xs:element name="color" type="xs:string" fixed="red"/>