XML DTD XSD Study Guide

本文深入探讨了XML(可扩展标记语言)的基本概念及其与数据存储和传输的关系,并详细介绍了DTD(文档类型定义)的使用方法,包括元素声明、属性声明、实体引用等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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:

TypeDescription

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:

ValueExplanation

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"/>

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值