前段时间刚学了XML和XSLT,作为练习,我用XML和XSLT结合JavaScript写了一个实现页面内容筛选显示,这样在做一些基本的数据处理时就不需要重新载入页面了,提高了效率。
代码如下:
@H_502_103@1<?xmlversion="1.0"encoding="GB2312"?>
@H_502_103@2<xs:schemaxmlns:xs="http://www.w3.org/2001/XMLSchema"elementFormDefault="qualified"attributeFormDefault="unqualified">
@H_502_103@3<xs:elementname="PoemList">
@H_502_103@4<xs:complexType>
@H_502_103@5<xs:sequence>
@H_502_103@6<xs:elementname="Poem"minOccurs="1"maxOccurs="unbounded">
@H_502_103@7<xs:complexType>
@H_502_103@8<xs:sequence>
@H_502_103@9<xs:elementname="Title"/>
@H_502_103@10<xs:elementname="Write"/>
@H_502_103@11<xs:elementname="Text">
@H_502_103@12<xs:complexType>
@H_502_103@13<xs:sequence>
@H_502_103@14<!--必须定义Line元素,否则诗句无法分行显示-->
@H_502_103@15<xs:elementname="Line"minOccurs="1"maxOccurs="unbounded"/>
@H_502_103@16</xs:sequence>
@H_502_103@17</xs:complexType>
@H_502_103@18</xs:element>
@H_502_103@19<xs:elementname="Mean"/>
@H_502_103@20</xs:sequence>
@H_502_103@21<xs:attributename="Type"type="xs:string"use="required"/>
@H_502_103@22</xs:complexType>
@H_502_103@23</xs:element>
@H_502_103@24</xs:sequence>
@H_502_103@25</xs:complexType>
@H_502_103@26</xs:element>
@H_502_103@27</xs:schema>
@H_502_103@28
@H_
502_103@
1
<?
xmlversion="1.0"encoding="UTF-8"
?>
@H_502_103@2
<
xsl:stylesheet
version
="2.0"
xmlns:xsl
="http://www.w3.org/1999/XSL/Transform"
xmlns:xs
="http://www.w3.org/2001/XMLSchema"
xmlns:fn
="http://www.w3.org/2005/xpath-functions"
xmlns:xdt
="http://www.w3.org/2005/xpath-datatypes"
>
@H_502_103@3
<
xsl:output
version
="1.0"
encoding
="GB2312"
indent
="no"
omit-xml-declaration
="no"
media-type
="text/html"
/>
@H_502_103@4
<
xsl:template
match
="/"
>
@H_502_103@5
<
html
>
@H_502_103@6
<
head
>
@H_502_103@7
<
title
>
SelectPoem
</
title
>
@H_502_103@8
<
script
language
="javascript"
>
@H_502_103@9
functionselectPoem(write,selectedIndex){
@H_502_103@10
stylesheet=document.XSLDocument;
@H_502_103@11
xmldoc =document.XMLDocument;
@H_502_103@12
selectField=stylesheet.selectSingleNode("//xsl:if/@test");
@H_502_103@13
@H_502_103@14
if(write=="全部")
@H_502_103@15
selectField.value="Write=*";
@H_502_103@16
else
@H_502_103@17
selectField.value="Write='"+write+"'";
@H_502_103@18
@H_502_103@19
PoemList.innerHTML=xmldoc.documentElement.transformNode(stylesheet);
@H_502_103@20
document.SelectPoemForm.SelectPoemList.selectedIndex=selectedIndex;
@H_502_103@21
}
@H_502_103@22
</
script
>
@H_502_103@23
</
head
>
@H_502_103@24
<
body
>
@H_502_103@25
<
br
/>
@H_502_103@26
<
div
id
="PoemList"
>
@H_502_103@27
<
xsl:apply-templates
select
="PoemList"
/>
@H_502_103@28
</
div
>
@H_502_103@29
</
body
>
@H_502_103@30
</
html
>
@H_502_103@31
</
xsl:template
>
@H_502_103@32
<
xsl:template
match
="PoemList"
>
@H_502_103@33
<
table
align
="center"
border
="0"
>
@H_502_103@34
<
thead
>
@H_502_103@35
<
tr
align
="center"
>
@H_502_103@36
<
td
align
="center"
height
="39"
>
@H_502_103@37
<
h1
>
唐  诗
</
h1
>
@H_502_103@38
</
td
>
@H_502_103@39
</
tr
>
@H_502_103@40
</
thead
>
@H_502_103@41
<
tbody
>
@H_502_103@42
<
tr
>
@H_502_103@43
<
td
align
="center"
>
@H_502_103@44
<
form
name
="SelectPoemForm"
>
@H_502_103@45
作者:
@H_502_103@46
<
select
id
="SelectPoemList"
name
="SelectPoem"
class
="droplist"
onChange
="selectPoem(this.options[selectedIndex].text,selectedIndex);"
>
@H_502_103@47
<
option
>
全部
</
option
>
@H_502_103@48
<
xsl:for-each
select
="Poem"
>
@H_502_103@49
<
option
>
@H_502_103@50
<
xsl:value-of
select
="Write"
/>
@H_502_103@51
</
option
>
@H_502_103@52
</
xsl:for-each
>
@H_502_103@53
</
select
>
@H_502_103@54
</
form
>
@H_502_103@55
<
hr
color
="black"
size
="2"
/>
@H_502_103@56
<
br
/>
@H_502_103@57
<
xsl:for-each
select
="Poem"
>
@H_502_103@58
<
xsl:if
test
="Write=*"
>
@H_502_103@59
<
table
align
="center"
border
="0"
>
@H_502_103@60
<
tbody
>
@H_502_103@61
<
tr
align
="center"
valign
="middle"
>
@H_502_103@62
<
td
align
="center"
height
="2"
valign
="center"
width
="350"
>