xml.css的使用
在xml文件中使用
<?xml-stylesheet type="text/css" href="test.css"?>
在test.css文件中对元素做说明,比如YEAR{font-weight:bold;font-size:24pt;color:
red;}.其中YEAR是xml文件中的节点
在css中,内联和块级元素(display: block)如何设置?什么区别?
1.xml DTD与Schema的区别:
1.1)XML Schema与XML
DTD相比的一个最显著的特征,就是其对数据类型的支持了。这完全是因为XML DTD提供的数据类型只有CDATA 、Enumerated、NMTOKEN
、NMTOKENS等十种内置(built-in)数据类型。这样少的数据类型通常无法满足文档的可理解性和数据交换的需要。XML
Schema则不同,它内置了三十七种数据类型,如long,int,short,double等常用的数据类型,并通过将数据类型表示为由value
space、lexical space和facet三部分组成的三元组而获得更大的灵活性。但是,
XML Schema数据类型的真正灵活性来自于其对用户自定义类型的支持。XML
Schema提供两种方式来实现数据类型的定义。
1)简单类型定义(simpleType),即在XML
Schema内置的数据类型基础上或其它由XML
Schema内置的数据类型继承或定义所得到的简单的数据类型(simpleType)基础上,通过restriction,list 或者 union方式定义新的数据类型
2)复合类型定义(complexType),该方法提供了一种功能强大的复杂数据类型定义机制,可以实现包括结构描述在内的复杂的数据类型。下面是一个以complexType定义实现关系模式中表结构的例子,设有表T_C_Type(Psign,Count),其中Psign为CHAR数据类型,Count为NUMBER数据类型。
1.2)元素顺序的支持
1.3)命名空间
1.4)对于API的支持
1.5)注释
1.6)对数据库的支持
2.命名空间
在XML中引入命名空间的目的是为了能够在一个XML文档中使用其它XML文档中的一些具有通用性的定义(通常是一些元素或数据类型等的定义),并保证不产生语义上的冲突。XML
DTD并不能支持这一特性,这进一步限制了XML DTD的适用范围。而XML
Schema则很好的满足了这一点。
3.xml相关技术包括HTML、CSS(Cascading Style
Sheet,级联样式单)和XSL(eXtensible Style
Languages,可扩展的样式语言)、URL和URI、XLL(eXtensible Linking Language,可扩展的链接语言)和Unicode字符集。
CSS和XSL是两种最常用的样式语言
3.何时使用子元素或属性
没有严格的规则可循,通常要看哪一种更适合自己应用的需要。随着经验的增长就会有一种感觉,知道在何时使用属性比子元素更简单,反之亦然。一个好的经验规则是数据本身应当存储在元素中,而有关数据的信息(元数据)应当存储在属性中。不知道怎么做时,就把信息放在元素中。
CSS样式单仅适用于元素内容,而不适用于属性。在使用CSS时,希望显示给读者的任何数据应当是元素内容的一部分,而不是它的属性。
4.XSL:可选择的样式单语言能够访问并显示属性数据。这就是Extensible Style
Language (XSL).XSL分为两部分:变换部分和格式化部分。
XSL替换部分能够将一个标记替换为另一个标记。通过定义替换规则,使用标准的HTML标记代替XML标记或者使用HTML标记与CSS属性来替换XML标记。同时还可以在文档中重新安排元素和在XML文档中添加没有出现过的附加内容。
XSL格式化部分把功能强大的文档视图定义为页面。XSL格式化功能能够指定页面的外观和编排,包括多个专栏、围绕主题的字数、行间距、相配的字体属性等等。它的功能非常强大,足可以为网络和打印自动处理来自于相同源文档的编排任务。
下面的新XSL指令能够从SEASON元素中抽取YEAR属性。
<xsl:for-each select="SEASON">
<xsl:value-of select="@YEAR"/>
</xsl:for-each>
第8章 文档类型定义(DTD)
DTD以元素、实体、属性和记号的标记声明来做到这一点。
元素声明指明了元素名称和元素可能的内容
关键词ANY(也要区分大小写)表明所有可能的元素以及可析的字符数据都可以是SEASON元素的子元素。
元素:
+代表了1个或者多个元素;*代表了0个或者多个元素;?代表了0个或者1个元素;|代表必须包括选项中的某一个;
()代表匹配对
eg:如<!ELEMENT PAYMENT (CASH | CREDIT_CARD)>
表示PAYMENT元素必须有CASH或CREDIT_CARD中的一个子元素。
假设要说明一个DOCUMENT元素,它没有很多子元素,但必须有一个TITLE后接任意数量的混合文本段落和图像,以及一个任选的SIGNATURE块。该元素声明书写如下:
<!ELEMENT DOCUMENT (TITLE, (PARAGRAPH | IMAGE)*, SIGNATURE?)>
属性:
<!ATTLIST Element_name Attribute_name Type Default_value>
例如:
<GREETING LANGUAGE= "Spanish">
Hola!
</GREETING>
在DTD中,可按如下格式声明该元素:
<!ELEMENT GREETING (#PCDATA)>
<!ATTLIST GREETING LANGUAGE CDATA "English">
属性声明可以要求作者提供属性值,或者完全忽略该属性值,甚至总是使用缺省值。这三种类型分别由三个关键词#REQUIRED、#IMPLIED、#FIXED加以指定。
属性类型:CDATA Enumerated(枚举) NMTOKEN MTOKENS ID IDREF IDREFS ENTITY
ENTITIES NOTATION
实体:
内部实体:
<?xml version="1.0" standalone="Yes">
<!DOCTYPE DOCUME [
<!ETITY ERH "Elliotte Rusty Harold">
]>
<DOCUMENT>
<TITLE>&ERH;</TITLE>
</DOCUMENT>
就会在<TITLE>之间换成Elliotte Rusty Harold
外部实体:
假设文件为sinagure.xml
<?xml version="1.0">
<SIGNATURE>
<COPYRIGHT>1999 Elliotte Rusty Harold</COPYRIGHT>
<EMAIL>elharo@metalab.unc.edu</EMAIL>
</SIGNATURE>
<?xml version="1.0" standalone="no"?>
<!DOCTYPE DOCUMENT [
<!ENTITY SIG SYSTEM "signature.xml">
]>
</DOCUMENT>
<TITLE>Entity references</TITLE>
&SIG;
</DOCUMENT>
&SIG;部分就被xml文件内容替换
第12章 级联样式单级别 CSS1和CSS2
第13章 XSL
XSL是两种语言,而不是一种。第一种语言是变换语言,第二种是格式化语言
xml部分
An XML Schema:
defines elements that can appear in a document
defines attributes that can appear in a document
defines which elements are child elements
defines the order of child elements
defines the number of child elements
defines whether an element is empty or can include text
defines data types for elements and attributes
defines default and fixed values for elements and attributes
XML Schemas are the Successors of DTDs
We think that very soon XML Schemas will be used in most Web
applications as a replacement for DTDs. Here are some reasons:
XML Schemas are extensible to future additions
XML Schemas are richer and more powerful than DTDs
XML Schemas are written in XML
XML Schemas support data types
XML Schemas support namespaces
1.简单举例:
DTD和schema
A Simple XML Document
Look at this simple XML document called "note.xml":
<?xml version="1.0"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
--------------------------------------------------------------------------------
A DTD File
The following example is a DTD file called "note.dtd" that defines the
elements of the XML document above ("note.xml"):
<!ELEMENT note (to, from, heading, body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
The first line defines the note element to have four child elements:
"to, from, heading, body".
Line 2-5 defines the to, from, heading, body elements to be of type
"#PCDATA".
--------------------------------------------------------------------------------
An XML Schema
The following example is an XML Schema file called "note.xsd" that
defines the elements of the XML document above ("note.xml"):
<?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:element name="note">
<xs:complexType>
<xs:sequence>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="heading" type="xs:string"/>
<xs:element name="body" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element></xs:schema>
The note element is a complex type because it contains other elements.
The other elements (to, from, heading, body) are simple types because
they do not contain other elements. You will learn more about simple and
complex types in the following chapters.
--------------------------------------------------------------------------------
A Reference to a DTD
This XML document has a reference to a DTD:
<?xml version="1.0"?><!DOCTYPE note SYSTEM
"http://www.w3schools.com/dtd/note.dtd"><note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
--------------------------------------------------------------------------------
A Reference to an XML Schema
This XML document has a reference to an XML Schema:
<?xml version="1.0"?><note
xmlns="http://www.w3schools.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3schools.com note.xsd">
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
2.简单类型:
2.1)xsd Elements:
XML Schema has a lot of built-in data types. The most common types are:
xs:string
xs:decimal
xs:integer
xs:boolean
xs:date
xs:time
Example
Here are some XML elements:
<lastname>Refsnes</lastname>
<age>36</age>
<dateborn>1970-03-27</dateborn>
And here are the corresponding simple element definitions:
<xs:element name="lastname" type="xs:string"/>
<xs:element name="age" type="xs:integer"/>
<xs:element name="dateborn" type="xs:date"/>
2.2)xsd Attributes:
XML Schema has a lot of built-in data types. The most common types are:
xs:string
xs:decimal
xs:integer
xs:boolean
xs:date
xs:time
Example
Here is an XML element with an attribute:
<lastname lang="EN">Smith</lastname>
And here is the corresponding attribute definition:
<xs:attribute name="lang" type="xs:string"/>
2.3)xsd Restrictions:
Restrictions on Values
The following example defines an element called "age" with a
restriction. The value of age cannot be lower than 0 or greater than 120:
<xs:element name="age"><xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="0"/>
<xs:maxInclusive value="120"/>
</xs:restriction>
</xs:simpleType></xs:element>
支持的限制类型:
Restrictions for Datatypes
Constraint Description
enumeration Defines a list of acceptable values
fractionDigits Specifies the maximum number of decimal places allowed.
Must be equal to or greater than zero
length Specifies the exact number of characters or list items allowed.
Must be equal to or greater than zero
maxExclusive Specifies the upper bounds for numeric values (the value
must be less than this value)
maxInclusive Specifies the upper bounds for numeric values (the value
must be less than or equal to this value)
maxLength Specifies the maximum number of characters or list items
allowed. Must be equal to or greater than zero
minExclusive Specifies the lower bounds for numeric values (the value
must be greater than this value)
minInclusive Specifies the lower bounds for numeric values (the value
must be greater than or equal to this value)
minLength Specifies the minimum number of characters or list items
allowed. Must be equal to or greater than zero
pattern Defines the exact sequence of characters that are acceptable
totalDigits Specifies the exact number of digits allowed. Must be
greater than zero
whiteSpace Specifies how white space (line feeds, tabs, spaces, and
carriage returns) is handled
3.复杂类型:XSD:Complex Elements
A complex element is an XML element that contains other elements and/or
attributes.
There are four kinds of complex elements:
empty elements
elements that contain only other elements
elements that contain only text
elements that contain both other elements and text
举例:
3.1)<employee>
<firstname>John</firstname>
<lastname>Smith</lastname>
</employee>
We can define a complex element in an XML Schema two different ways:
1. The "employee" element can be declared directly by naming the
element, like this:
<xs:element name="employee">
<xs:complexType>
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
2. The "employee" element can have a type attribute that refers to the
name of the complex type to use:
<xs:element name="employee" type="personinfo"/><xs:complexType
name="personinfo">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
If you use the method described above, several elements can refer to
the same complex type, like this:
<xs:element name="employee" type="personinfo"/>
<xs:element name="student" type="personinfo"/>
<xs:element name="member" type="personinfo"/><xs:complexType
name="personinfo">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
You can also base a complex element on an existing complex element and
add some elements, like this:
<xs:element name="employee" type="fullpersoninfo"/><xs:complexType
name="personinfo">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType><xs:complexType name="fullpersoninfo">
<xs:complexContent>
<xs:extension base="personinfo">
<xs:sequence>
<xs:element name="address" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
4.一个完整的xsd的例子:
This chapter will demonstrate how to write an XML Schema. You will also
learn that a schema can be written in different ways.
--------------------------------------------------------------------------------
An XML Document
Let's have a look at this XML document called "shiporder.xml":
<?xml version="1.0" encoding="ISO-8859-1"?><shiporder orderid="889923"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="shiporder.xsd">
<orderperson>John Smith</orderperson>
<shipto>
<name>Ola Nordmann</name>
<address>Langgt 23</address>
<city>4000 Stavanger</city>
<country>Norway</country>
</shipto>
<item>
<title>Empire Burlesque</title>
<note>Special Edition</note>
<quantity>1</quantity>
<price>10.90</price>
</item>
<item>
<title>Hide your heart</title>
<quantity>1</quantity>
<price>9.90</price>
</item>
</shiporder>
The XML document above consists of a root element, "shiporder", that
contains a required attribute called "orderid". The "shiporder" element
contains three different child elements: "orderperson", "shipto" and
"item". The "item" element appears twice, and it contains a "title", an
optional "note" element, a "quantity", and a "price" element.
The line above: xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
tells the XML parser that this document should be validated against a
schema. The line: xsi:noNamespaceSchemaLocation="shiporder.xsd" specifies
WHERE the schema resides (here it is in the same folder as
"shiporder.xml").
--------------------------------------------------------------------------------
Create an XML Schema
Now we want to create a schema for the XML document above.
We start by opening a new file that we will call "shiporder.xsd". To
create the schema we could simply follow the structure in the XML
document and define each element as we find it. We will start with the
standard XML declaration followed by the xs:schema element that defines a
schema:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">......
</xs:schema>
In the schema above we use the standard namespace (xs), and the URI
associated with this namespace is the Schema language definition, which
has the standard value of http://www.w3.org/2001/XMLSchema.
Next, we have to define the "shiporder" element. This element has an
attribute and it contains other elements, therefore we consider it as a
complex type. The child elements of the "shiporder" element is
surrounded by a xs:sequence element that defines an ordered sequence of sub
elements:
<xs:element name="shiporder">
<xs:complexType>
<xs:sequence>
...
...
</xs:sequence>
...
</xs:complexType>
</xs:element>
Then we have to define the "orderperson" element as a simple type
(because it does not contain any attributes or other elements). The type
(xs:string) is prefixed with the namespace prefix associated with XML
Schema that indicates a predefined schema data type:
<xs:element name="orderperson" type="xs:string"/>
Next, we have to define two elements that are of the complex type:
"shipto" and "item". We start by defining the "shipto" element:
<xs:element name="shipto">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="address" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
With schemas we can define the number of possible occurrences for an
element with the maxOccurs and minOccurs attributes. maxOccurs specifies
the maximum number of occurrences for an element and minOccurs
specifies the minimum number of occurrences for an element. The default value
for both maxOccurs and minOccurs is 1!
Now we can define the "item" element. This element can appear multiple
times inside a "shiporder" element. This is specified by setting the
maxOccurs attribute of the "item" element to "unbounded" which means that
there can be as many occurrences of the "item" element as the author
wishes. Notice that the "note" element is optional. We have specified
this by setting the minOccurs attribute to zero:
<xs:element name="item" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="note" type="xs:string" minOccurs="0"/>
<xs:element name="quantity" type="xs:positiveInteger"/>
<xs:element name="price" type="xs:decimal"/>
</xs:sequence>
</xs:complexType>
</xs:element>
We can now declare the attribute of the "shiporder" element. Since this
is a required attribute we specify use="required".
Note: The attribute declarations must always come last:
<xs:attribute name="orderid" type="xs:string" use="required"/>
Here is the complete listing of the schema file called "shiporder.xsd":
<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"><xs:element
name="shiporder">
<xs:complexType>
<xs:sequence>
<xs:element name="orderperson" type="xs:string"/>
<xs:element name="shipto">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="address" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="item" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="note" type="xs:string" minOccurs="0"/>
<xs:element name="quantity" type="xs:positiveInteger"/>
<xs:element name="price" type="xs:decimal"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="orderid" type="xs:string" use="required"/>
</xs:complexType>
</xs:element></xs:schema>
--------------------------------------------------------------------------------
Divide the Schema
The previous design method is very simple, but can be difficult to read
and maintain when documents are complex.
The next design method is based on defining all elements and attributes
first, and then referring to them using the ref attribute.
Here is the new design of the schema file ("shiporder.xsd"):
<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"><!-- definition
of simple elements -->
<xs:element name="orderperson" type="xs:string"/>
<xs:element name="name" type="xs:string"/>
<xs:element name="address" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
<xs:element name="title" type="xs:string"/>
<xs:element name="note" type="xs:string"/>
<xs:element name="quantity" type="xs:positiveInteger"/>
<xs:element name="price" type="xs:decimal"/><!-- definition of
attributes -->
<xs:attribute name="orderid" type="xs:string"/><!-- definition of
complex elements -->
<xs:element name="shipto">
<xs:complexType>
<xs:sequence>
<xs:element ref="name"/>
<xs:element ref="address"/>
<xs:element ref="city"/>
<xs:element ref="country"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="item">
<xs:complexType>
<xs:sequence>
<xs:element ref="title"/>
<xs:element ref="note" minOccurs="0"/>
<xs:element ref="quantity"/>
<xs:element ref="price"/>
</xs:sequence>
</xs:complexType>
</xs:element><xs:element name="shiporder">
<xs:complexType>
<xs:sequence>
<xs:element ref="orderperson"/>
<xs:element ref="shipto"/>
<xs:element ref="item" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute ref="orderid" use="required"/>
</xs:complexType>
</xs:element></xs:schema>
--------------------------------------------------------------------------------
Using Named Types
The third design method defines classes or types, that enables us to
reuse element definitions. This is done by naming the simpleTypes and
complexTypes elements, and then point to them through the type attribute
of the element.
Here is the third design of the schema file ("shiporder.xsd"):
<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"><xs:simpleType
name="stringtype">
<xs:restriction base="xs:string"/>
</xs:simpleType><xs:simpleType name="inttype">
<xs:restriction base="xs:positiveInteger"/>
</xs:simpleType><xs:simpleType name="dectype">
<xs:restriction base="xs:decimal"/>
</xs:simpleType><xs:simpleType name="orderidtype">
<xs:restriction base="xs:string">
<xs:pattern value="[0-9]{6}"/>
</xs:restriction>
</xs:simpleType><xs:complexType name="shiptotype">
<xs:sequence>
<xs:element name="name" type="stringtype"/>
<xs:element name="address" type="stringtype"/>
<xs:element name="city" type="stringtype"/>
<xs:element name="country" type="stringtype"/>
</xs:sequence>
</xs:complexType><xs:complexType name="itemtype">
<xs:sequence>
<xs:element name="title" type="stringtype"/>
<xs:element name="note" type="stringtype" minOccurs="0"/>
<xs:element name="quantity" type="inttype"/>
<xs:element name="price" type="dectype"/>
</xs:sequence>
</xs:complexType><xs:complexType name="shipordertype">
<xs:sequence>
<xs:element name="orderperson" type="stringtype"/>
<xs:element name="shipto" type="shiptotype"/>
<xs:element name="item" maxOccurs="unbounded" type="itemtype"/>
</xs:sequence>
<xs:attribute name="orderid" type="orderidtype" use="required"/>
</xs:complexType><xs:element name="shiporder"
type="shipordertype"/></xs:schema>
The restriction element indicates that the datatype is derived from a
W3C XML Schema namespace datatype. So, the following fragment means that
the value of the element or attribute must be a string value:
<xs:restriction base="xs:string">
The restriction element is more often used to apply restrictions to
elements. Look at the following lines from the schema above:
<xs:simpleType name="orderidtype">
<xs:restriction base="xs:string">
<xs:pattern value="[0-9]{6}"/>
</xs:restriction>
</xs:simpleType>
This indicates that the value of the element or attribute must be a
string, it must be exactly six characters in a row, and those characters
must be a number from 0 to 9.