来源:
中国XML论坛
看到有人问如何使用xquery,其实我也是个菜鸟。
刚刚运行了两个例子,给不会的指个路。
首先下载saxon8.1.1
http://saxon.sourceforge.net/
Saxon-B 8.1.1是免费的,Saxon-SA 8.1.1是有试用期的,前者就够了。
系统要安装jdk1.4.1以上版本,因为saxon是用这个版本写的,但不知道1.3.1行不。
记得设置JAVA_HOME环境变量指向jdk。
然后,试试吧!
例子:文件characters.xml
<characters>
<character>
<name>Aleria</name>
<gender>Female</gender>
<species>Heroleim</species>
<vocation>Bard</vocation>
<level>5</level>
<health>25</health>
</character>
<character>
<name>Shar</name>
<gender>Male</gender>
<species>Human</species>
<vocation>Merchant</vocation>
<level>6</level>
<health>28</health>
</character>
<character>
<name>Gite</name>
<gender>Female</gender>
<species>Aelvar</species>
<vocation>Mage</vocation>
<level>7</level>
<health>18</health>
</character>
<character>
<name>Horukkan</name>
<gender>Male</gender>
<species>Udrecht</species>
<vocation>Warrior</vocation>
<level>5</level>
<health>32</health>
</character>
<character>
<name>Gounna</name>
<gender>Female</gender>
<species>Noleim</species>
<vocation>Mage</vocation>
<level>8</level>
<health>31</health>
</character>
<character>
<name>Sheira</name>
<gender>Female</gender>
<species>Human</species>
<vocation>Cleric</vocation>
<level>4</level>
<health>17</health>
</character>
<character>
<name>Drue</name>
<gender>Female</gender>
<species>Voleim</species>
<vocation>Warrior</vocation>
<level>6</level>
<health>32</health>
</character>
<character>
<name>Paccu</name>
<gender>Male</gender>
<species>Human</species>
<vocation>Merchant</vocation>
<level>5</level>
<health>24</health>
</character>
</characters>
查询文件:Date.xquery
for $character in doc('characters.xml')//character
order by $character/name descending
return
<dateReport>
{$character/name}
</dateReport>
(: Stylus Studio meta-information - (c)1998-2003. Sonic Software Corporation. All rights reserved.
<metaInformation>
<scenarios/><MapperInfo><TargetSchema SchemaFilePath="" SchemaRoot="" AssociatedInstance=""/></MapperInfo>
</metaInformation>
:)
在命令提示下,输入:
set classpath=d:\saxon\saxon8.jar (saxon8.jar所在文件夹)
java net.sf.saxon.Query -s characters.xml Date.xquery
ok!
看结果吧!
D:\xml>java net.sf.saxon.Query -s characters.xml Date.xquery
<?xml version="1.0" encoding="UTF-8"?>
<dateReport>
<name>Sheira</name>
</dateReport>
<dateReport>
<name>Shar</name>
</dateReport>
<dateReport>
<name>Paccu</name>
</dateReport>
<dateReport>
<name>Horukkan</name>
</dateReport>
<dateReport>
<name>Gounna</name>
</dateReport>
<dateReport>
<name>Gite</name>
</dateReport>
<dateReport>
<name>Drue</name>
</dateReport>
<dateReport>
<name>Aleria</name>
</dateReport>