可以利用其自带的例子里面已经写好了的那个函数,在Functions.xml文件里面,只要
在配置文件的开头把include进来就行了;
Functions.xml文件如下:
<function name="download-multipage-list">
<return>
<while condition="${pageUrl.toString().length() != 0}" maxloops="${maxloops}" index="i">
<empty>
<var-def name="content">
<html-to-xml>
<http url="${pageUrl}"/>
</html-to-xml>
</var-def>
<var-def name="nextLinkUrl">
<xpath expression="${nextXPath}">
<var name="content"/>
</xpath>
</var-def>
<var-def name="pageUrl">
<template>${sys.fullUrl(pageUrl.toString(), nextLinkUrl.toString())}</template>
</var-def>
</empty>
<xpath expression="${itemXPath}">
<var name="content"/>
</xpath>
</while>
</return>
</function>
其里面的各个参数的意义:
<!--
Download multi-page list of items.
@param pageUrl - URL of starting page
@param itemXPath - XPath expression to obtain single item in the list
@param nextXPath - XPath expression to URL for the next page
@param maxloops - maximum number of pages downloaded
@return list of all downloaded items
-->
第一个是指开始的那个页面的URL;
第二个是指你要在页面里面取得的一个list,比如说是href的List或者说是一个tr的
List,我下面的例子就是取得一个tr的List;
第三个是指下一个页面的URL;
第四个是指循环查找的页数;
这就是我所写的配置文件:
<include path="functions.xml"/>
<var-def name="hotels">
<call name="download-multipage-list">
<call-param name="pageUrl">http://www.51zyr.com/tpl/index/hotel_list_web.do?page=1</call-param>
<call-param name="nextXPath">//a[contains(text(),'下页')]/@href</call-param>
<call-param name="itemXPath">//table[@background='../images/dotline.gif']/tbody/tr</call-param>
<call-param name="maxloops">10</call-param>
</call>
</var-def>
<file action="write" path="51zyr/hotels.xml">
<![CDATA[ <hotels> ]]>
<loop item="item" index="i">
<list><var name="hotels"/></list>
<body>
<xquery>
<xq-param name="item" type="node()"><var name="item"/></xq-param>
<xq-expression><![CDATA[
declare variable $item as node() external;
let $num := data($item//td[1])
let $name := data($item//td[2])
let $star := data($item//td[3])
let $address := data($item//td[4])
let $telephone := data($item//td[5])
return
<hotel>
<num>{data($num)}</num>
<name>{data($name)}</name>
<star>{data($star)}</star>
<address>{data($address)}</address>
<telephone>{data($telephone)}</telephone>
</hotel>
]]></xq-expression>
</xquery>
</body>
</loop>
<![CDATA[ </hotels> ]]>
</file>