XML的基础

1 xml基本定义
xml产生于SGML和HTML之后,结合了这两门语言的优点。
xml彻底的将文档的结构和数据与显示样式分离开来。
看一下xml的简单定义,以供后面例子使用。
Xml代码
<?xml version="1.0" encoding="UTF-8"?>
<books>
<book>
<title>xml</title>
<author>Bob</author>
<price unit="RMB">20.0</price>
</book>
</books>


<?xml version="1.0" encoding="UTF-8"?>
<books>
<book>
<title>xml</title>
<author>Bob</author>
<price unit="RMB">20.0</price>
</book>
</books>

在这里不仅定义了节点,还定义了一个节点属性
2 dtd
任何一个xml文档,都可以包含一个它的约束dtd。dtd分为外部dtd和内部dtd。

外部dtd的定义方式 Xml代码
<?xml version="1.0" encoding="GB2312"?>
<!ELEMENT books (book*)>
<!ELEMENT book ( title,author,price)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT price (#PCDATA)>
<!ATTLIST price unit CDATA "RMB">

<?xml version="1.0" encoding="GB2312"?>
<!ELEMENT books (book*)>
<!ELEMENT book ( title,author,price)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT price (#PCDATA)>
<!ATTLIST price unit CDATA "RMB">

在xml中引用dtd的方法
Xml代码
<!DOCTYPE books SYSTEM “DTD-URL”>或者
<!DOCTYPE books PUBLIC "DTD名称" "DTD-URL">

<!DOCTYPE books SYSTEM “DTD-URL”>或者
<!DOCTYPE books PUBLIC "DTD名称" "DTD-URL">
内部dtd的定义方式 Xml代码
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE books [
<!ELEMENT books (book*)>
<!ELEMENT book ( title,author,price)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT price (#PCDATA)>
<!ATTLIST price unit CDATA "RMB">
]
<books>
<book>
<title>xml</title>
<author>Bob</author>
<price unit="RMB">20.0</price>
</book>
</books>

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE books [
<!ELEMENT books (book*)>
<!ELEMENT book ( title,author,price)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT price (#PCDATA)>
<!ATTLIST price unit CDATA "RMB">
]
<books>
<book>
<title>xml</title>
<author>Bob</author>
<price unit="RMB">20.0</price>
</book>
</books>


关于dtd中定义的说明
a 复合元素定义
Xml代码
<!ELEMENT books(book*)>
MENT book (title,author,price)>

<!ELEMENT books(book*)>
<!ELEMENT book (title,author,price)>
在这里面我们要注意的是元素出现的次数

引用

? 不出现或者出现一次
* 不出现或者出现多次(任意次,包括0)
+ 必须出现一次以上
无符号 只能出现一次


b 基本元素定义
Xml代码
<!ELEMENT title (#PCDATA)>

<!ELEMENT title (#PCDATA)>

c 空元素定义
Xml代码
<!ELEMENT hr EMPTY>

<!ELEMENT hr EMPTY>

d 定义元素属性
Xml代码
<!ATTLIST price unit CDATA "RMB">

<!ATTLIST price unit CDATA "RMB">

e 属性的赋值特征

引用
#REQUIRED 元素的属性是必须的
#IMPLIED 元素的属性是可有可无的
#FIX 元素的属性的值是固定不变的
默认值 字符数据


f 属性值的类型

引用
CDATA ENUMERATED ID IDREF ENTITY ENTITIES NOTATION等等。


3 实体
内部实体
Xml代码
<?xml version="1.0" encoding="GB2312"?>
<!DOCTYPE books [
...
<!ENTITY author "Bob">
...
]>
<books>
<book>
...
<author>&author; </author>
...
</book>
</books>

<?xml version="1.0" encoding="GB2312"?>
<!DOCTYPE books [
...
<!ENTITY author "Bob">
...
]>
<books>
<book>
...
<author>&author; </author>
...
</book>
</books>



外部实体 Xml代码
<?xml version="1.0" encoding="GB2312"?>
<!DOCTYPE books [
...
<! ENTITY author SYSTEM “entities1.dtd” >
...
]>
<books>
<book>
...
<author>&author; </author>
...
</book>
</books>

<?xml version="1.0" encoding="GB2312"?>
<!DOCTYPE books [
...
<! ENTITY author SYSTEM “entities1.dtd” >
...
]>
<books>
<book>
...
<author>&author; </author>
...
</book>
</books>



预定义实体 Xml代码
<title>我说:" 再见!" </ title>

<title>我说:" 再见!" </ title>


参数实体 Xml代码
<?xml version="1.0" encoding="GB2312"?>
<!ENTITY % para “bp”>
<!ELEMENT reference ((%para;)+)>
<!ELEMENT %para; (title,author)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (#PCDATA)>

<?xml version="1.0" encoding="GB2312"?>
<!ENTITY % para “bp”>
<!ELEMENT reference ((%para;)+)>
<!ELEMENT %para; (title,author)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (#PCDATA)>

引用外部DTD中定义的参数实体的XML文档paper.xml代码如下:
Xml代码

<?xml version="1.0" encoding="GB2312" ?>
<!DOCTYPE reference SYSTEM "referrence.dtd" [
<!ENTITY % para "paper">
]>
<reference>
<paper>
<title>xml</title>
<author>Bob</author>
</paper>
</reference>


<?xml version="1.0" encoding="GB2312" ?>
<!DOCTYPE reference SYSTEM "referrence.dtd" [
<!ENTITY % para "paper">
]>
<reference>
<paper>
<title>xml</title>
<author>Bob</author>
</paper>
</reference>

引用外部DTD中定义的参数实体的XML文档book.xml代码如下:
Xml代码

<?xml version="1.0" encoding="GB2312" ?>
<!DOCTYPE reference SYSTEM "referrence.dtd" [
<!ENTITY % para "book">
]>
<reference>
<book>
<title>JS</title>
<author>JNotnull</author>
</book>
</reference>


<?xml version="1.0" encoding="GB2312" ?>
<!DOCTYPE reference SYSTEM "referrence.dtd" [
<!ENTITY % para "book">
]>
<reference>
<book>
<title>JS</title>
<author>JNotnull</author>
</book>
</reference>


4 xml中的样式表


css Xml代码
<?xml version ="1.0" encoding ="GB2312"?>
<?xml-stylesheet type="text/css" href ="test.css"?>
<book>
<name>JS</name>
</book>

<?xml version ="1.0" encoding ="GB2312"?>
<?xml-stylesheet type="text/css" href ="test.css"?>
<book>
<name>JS</name>
</book>

test.css
Css代码
book{display:block; position:absolute; top:20px; width:400 px; border:2px solid black; padding:5px; background-color;yellow; color:green; font-family:times, serif; font-style:italic;text-align:center; }
name{display:block; position:relative; top:10 px; width:300 px; height:60 px; font-weight:bold; font-size:50px; font-family:Arial,Helvetica,sans-serif; font-style:italic; color:red; }

book{display:block; position:absolute; top:20px; width:400 px; border:2px solid black; padding:5px; background-color;yellow; color:green; font-family:times, serif; font-style:italic;text-align:center; }
name{display:block; position:relative; top:10 px; width:300 px; height:60 px; font-weight:bold; font-size:50px; font-family:Arial,Helvetica,sans-serif; font-style:italic; color:red; }


xsl Xml代码
<?xml version ="1.0" encoding ="GB2312"?>
<?xml-stylesheet type="text/xsl" href ="b.xsl"?>
<book>
<name>JS</name>
</book>

<?xml version ="1.0" encoding ="GB2312"?>
<?xml-stylesheet type="text/xsl" href ="b.xsl"?>
<book>
<name>JS</name>
</book>

Xml代码
<?xml version="1.0" encoding="GB2312"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="/">
<html>
<head><title>XSL</title></head>
<body>
<xsl:apply-templates select="book" mode="1">
</body>
</html>
</xsl:template>

<xsl:template match="book" mode="1">
<h2>
<xsl:value-of select="name"/>
</h2>
</xsl:template>

<xsl:template match="book" mode="2">
<h3>
<xsl:value-of select="name"/>
</h3>
</xsl:template>

</xsl:stylesheet>

<?xml version="1.0" encoding="GB2312"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="/">
<html>
<head><title>XSL</title></head>
<body>
<xsl:apply-templates select="book" mode="1">
</body>
</html>
</xsl:template>

<xsl:template match="book" mode="1">
<h2>
<xsl:value-of select="name"/>
</h2>
</xsl:template>

<xsl:template match="book" mode="2">
<h3>
<xsl:value-of select="name"/>
</h3>
</xsl:template>

</xsl:stylesheet>


在上面的例子中我们大概的可以看到一个xsl文件的框架,它有xsl:stylesheet包围,里面包括了三个匹配模式(其中一个主匹配模式,两个从匹配模式),还有一个执行模式xsl:apply-templates


xsl中的标签
xsl:text
Xml代码
<xsl:text>xx</xsl:text>

<xsl:text>xx</xsl:text>

xsl:for-each
Xml代码
<xsl:for-each select = "book">
...
</xsl:for-each>

<xsl:for-each select = "book">
...
</xsl:for-each>


xsl:value-of
Xml代码
<xsl:value-of select="name"/>

<xsl:value-of select="name"/>

xsl:for-each
Xml代码
<xsl:for-each select = "book">
...
</xsl:for-each>

<xsl:for-each select = "book">
...
</xsl:for-each>

xsl:if
Xml代码
<xsl:if test=“表达式” >
...
</xsl:if>

<xsl:if test=“表达式” >
...
</xsl:if>

疑问:xsl:if能否实现正值表达式。
xsl:choose, xsl:when和xsl:otherwise
Xml代码
<xsl:choose>
<xsl:when test="表达式''

</xsl:when>
<xsl:otherwise>

</xsl:otherwise>
</xsl:choose>

<xsl:choose>
<xsl:when test="表达式''

</xsl:when>
<xsl:otherwise>

</xsl:otherwise>
</xsl:choose>


xsl:sort
Xml代码
<xsl:for-each select = "book">
<xsl:sort select = "@title"/>
<b><xsl:value-of select = "title"/></b>
<br/>
<b><xsl:value-of select = "price"/></b>
<br/>
</xsl:for-each>

<xsl:for-each select = "book">
<xsl:sort select = "@title"/>
<b><xsl:value-of select = "title"/></b>
<br/>
<b><xsl:value-of select = "price"/></b>
<br/>
</xsl:for-each>


引:xsl:sort能否实现自定义排序。大案是是可以得
Xml代码
<xsl:sort select=string-expression
data-type={"text"|"number"}
order={"ascending"|"descending"}
case-order={"upper-first"|"lower-first"}/>

<xsl:sort select=string-expression
data-type={"text"|"number"}
order={"ascending"|"descending"}
case-order={"upper-first"|"lower-first"}/>
参数:data-type它的值常用的是text,number。text是指数据被当作字符串进行排序,number则是把数据转换为数字,按照数值进行排序。缺省的值是text
Order属性指定排序时是按照升序(ascending)还是降序(descending),缺省的情况是升序。
case-order属性是指按字母表顺序排时,大写在前还是小写在前

xsl读取某个节点的属性
Xml代码
<xsl:value-of select="nodeName/@attributeName"/>或者
<xsl:for-each select="price">
<h1>:<xsl:value-of select="@unit"/></h1>
</xsl:for-each>

<xsl:value-of select="nodeName/@attributeName"/>或者
<xsl:for-each select="price">
<h1>:<xsl:value-of select="@unit"/></h1>
</xsl:for-each>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值