我有一个收集条目和压缩它们的功能,然后以epub的形式提供给用户。问题是我收集每一章作为<entry/>
元素,这意味着没有根元素。我只有一个带有章节数组的变量。有什么方法可以解决这个问题吗?
例如:
let $chaps := (
<entry>
<html>
<h1>abc</h1>
<h2>xxx</h2>
<h2>yyy</h2>
</html>
</entry>,
<entry>
<html>
<h1>def</h1>
<h2>xxx</h2>
<h2>yyy</h2>
</html>
</entry>,
<entry>
<html>
<h1>ghi</h1>
<h2>xxx</h2>
<h2>yyy</h2>
</html>
</entry>,
<entry>
<html>
<h1>jkl</h1>
<h2>xxx</h2>
<h2>yyy</h2>
</html>
</entry>
)
我在尝试:
for $entry in $chaps
return
<headings>
<a>{'Count h1 in preceding entries: ' || count($entry/preceding-sibling::entry//h1)}</a>
<b>{'Count h2 in preceding entries: ' || count($entry/preceding-sibling::entry//h2)}</b>
</headings>
结果:
<headings>
<a>Count h1 in preceding entries: 0</a>
<b>Count h2 in preceding entries: 0</b>
</headings>
<headings>
<a>Count h1 in preceding entries: 0</a>
<b>Count h2 in preceding entries: 0</b>
</headings>
<headings>
<a>Count h1 in preceding entries: 0</a>
<b>Count h2 in preceding entries: 0</b>
</headings>
<headings>
<a>Count h1 in preceding entries: 0</a>
<b>Count h2 in preceding entries: 0</b>
</headings>
预期:
<headings>
<a>Count h1 in preceding entries: 0</a>
<b>Count h2 in preceding entries: 0</b>
</headings>
<headings>
<a>Count h1 in preceding entries: 1</a>
<b>Count h2 in preceding entries: 2</b>
</headings>
<headings>
<a>Count h1 in preceding entries: 2</a>
<b>Count h2 in preceding entries: 4</b>
</headings>
<headings>
<a>Count h1 in preceding entries: 3</a>
<b>Count h2 in preceding entries: 6</b>
</headings>
答案 0 :(得分:1)
如果我理解你的话:
count($entry//preceding::xhtml:h1)
没有产生您的预期结果,因为您想要计算h1
之前显示的entry
?
如果是这样,请不要使用//
,因为这意味着自我或后代,相反,我认为您需要类似的内容:
$entry/preceding-sibling::entry//xhtml:h1
答案 1 :(得分:0)
因为在变量内部确实存在一个数组,所以需要迭代数组,而不是单独使用变量。最后,这对我有用:
for $entry at $index in $chaps
return
<headings>
<a>{'Count h1 in preceding entries: ' || count(
for $i in 1 to $index
return $entry//xhtml:h1
)}</a>
<b>{'Count h1 in preceding entries: ' || count(
for $i in 1 to $index
return $entry//xhtml:h2
)}</b>
</headings>
结果:
<headings>
<a>Count h1 in preceding entries: 1</a>
<b>Count h1 in preceding entries: 2</b>
</headings>
<headings>
<a>Count h1 in preceding entries: 2</a>
<b>Count h1 in preceding entries: 4</b>
</headings>
<headings>
<a>Count h1 in preceding entries: 3</a>
<b>Count h1 in preceding entries: 6</b>
</headings>
<headings>
<a>Count h1 in preceding entries: 4</a>
<b>Count h1 in preceding entries: 8</b>
</headings>
答案 2 :(得分:0)
Simpler implementation possible using the position() function. I think this is what you were aiming at originally:
for $a at $pos in $chaps
return
<headings>
<a>{'Count h1 in preceding entries: ' || count($chaps[position() < $pos]//h1)}</a>
<b>{'Count h2 in preceding entries: ' || count($chaps[position() < $pos]//h2)}</b>
</headings>