开始我的XML之旅

 这几天开始了XML之旅,一些体会和大家说说……

XML涉及的方面太多,学起来比较费劲。不过学XML首先得学好XPATH。

1、先要了解XML结构.

1)序言

<?xml version="1.0"?>

<!DOCTYPE 文档元素 SYSTEM  "url">

2)主体

<books>

<book>

<书名>XML</书名>

<价格>100</价格>

</book>

</books>

3)尾声

尾声不是必须的

2、明确“根元素”不是“文档元素”

3、一个简单练习

1)定义DTD

 

<? xml version="1.0" encoding="UTF-8" ?>
<!-- 元素类型声明 -->
<! ELEMENT players (player*) >
<! ELEMENT player (name,score_1,score_2,touch_times) >
<! ELEMENT name (#PCDATA) >
<! ELEMENT score_1 (#PCDATA) >
<! ELEMENT score_2 (#PCDATA) >
<! ELEMENT touch_times (#PCDATA) >

也可以用schema来验证有效性,以后再和大家讨论下schema

2)XML文档

 

<? xml version="1.0" encoding="UTF-8" ?>
<? xml-stylesheet type="text/xsl" href="x_players.xsl" ?>
<! DOCTYPE players SYSTEM "x_players.dtd" >
< players >
    
< player >
        
< name > Vivian Richards </ name >
        
< score_1 > 76 </ score_1 >
        
< score_2 > 26 </ score_2 >
        
< touch_times > 276 </ touch_times >
    
</ player >
    
< player >
        
< name > Sachin Tendulkar </ name >
        
< score_1 > 45 </ score_1 >
        
< score_2 > 30 </ score_2 >
        
< touch_times > 250 </ touch_times >
    
</ player >
    
< player >
        
< name > Steve Waugh </ name >
        
< score_1 > 49 </ score_1 >
        
< score_2 > 22 </ score_2 >
        
< touch_times > 208 </ touch_times >
    
</ player >
    
< player >
        
< name > Sunil Gavaskar </ name >
        
< score_1 > 86 </ score_1 >
        
< score_2 > 32 </ score_2 >
        
< touch_times > 200 </ touch_times >
    
</ player >
</ players >

3)用XSL转换显示内容

 

<? xml version="1.0" encoding="UTF-8" ?>
< xsl:stylesheet  version ="1.0"  xmlns:xsl ="http://www.w3.org/1999/XSL/Transform"  xmlns:fo ="http://www.w3.org/1999/XSL/Format" >
< xsl:output  method ="html"  media-type ="all" />
< xsl:template  match ="/" >
    
< html >
        
< head >
            
< title > 选手得分情况表 </ title >
            
< style  type ="text/css" >
                table{font-size:12px;background-color:black;}
                th{text-align:center;background-color:#ccc;}
                td{background-color:white;width:150px;}
            
</ style >
        
</ head >
        
< body >
            
< div  align ="center" >
                
< h1 > 选手得分情况表 </ h1 >
            
</ div >
            
< div  align ="center" >
                
< table  cellpadding ="5px"  cellspacing ="1px" >
                    
< tbody >
                        
< tr >
                            
< th > 选手 </ th >
                            
< th > No.得分超过50的次数 </ th >
                            
< th > 得分超过100的次数 </ th >
                            
< th > No.有效接球次数 </ th >
                        
</ tr >
                        
< xsl:for-each  select ="players/player" >
                        
< tr >
                            
< td >< xsl:value-of  select ="name" /></ td >
                            
< td >< xsl:value-of  select ="score_1" /></ td >
                            
< td >< xsl:value-of  select ="score_2" /></ td >
                            
< td >< xsl:value-of  select ="touch_times" /></ td >
                        
</ tr >
                        
</ xsl:for-each >
                    
</ tbody >
                
</ table >
            
</ div >
        
</ body >
    
</ html >
</ xsl:template >
</ xsl:stylesheet >

这样,就用到了DTD和XSL,要明白XSL转换,首先要了解XPATH语言.

XPATH实现了对XML文档的定位.上面的例子中match="/"匹配了文档的根节点.定义了文档的上下文.

接着用<xsl:for-each>来循环并取出相应节点的文本内容.其中select属性包含了XPATH表达式和定位路径.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值