我正在尝试获取p标签,但尚未成功。以下是我想要获得的数据。
for $i in $data
let $ptag := $i//*[p]/text()
(: $data example :)
<div xmlns="http://www.w3.org/1999/xhtml">
<div class="title">my title</div>
<div class="course">
<div class="room">112</div>
<div class="teacher">Mr. Wilson</div>
<div class="student">123456</div>
<p>approved</p>
</div>
</div>
答案 0 :(得分:6)
两个问题:
p
元素绑定到XHTML名称空间。您正在“无名称空间”中引用名为“p”的元素。声明XHTML命名空间并在XPath中使用namespace-prefix。text()
中具有$data
子元素的任何元素处理所有p
个节点,而不是从中选择所有text()
个节点所有的XHTML p元素。(: declare a namespace prefix for the XHTML namespace,
in order to use in the XPath below :)
declare namespace x="http://www.w3.org/1999/xhtml";
(: $data example :)
let $data :=
<div xmlns="http://www.w3.org/1999/xhtml">
<div class="title">my title</div>
<div class="course">
<div class="room">112</div>
<div class="teacher">Mr. Wilson</div>
<div class="student">123456</div>
<p>approved</p>
</div>
</div>
return
for $i in $data
(: use the namespace prefix "x" when addressing the HTML p element
and pull it out of the predicate
:)
let $ptag := $i//x:p/text()
return $ptag