xquery-如何计算另一个字符串+第一个和最后一个字符串中字符串出现次数。这个字符串

时间:2012-09-04 15:12:27

标签: xquery

我正在尝试从XHTML文档中提取一些名称 -

现在我已经想出如何在一个XQuery代码中提取所有这些名称 -

例如 -

<div class="names">
    <b>Names</b>
    <a href="http://name1.com">A B.C D</a>
    <a href="http://name1.com">E F G</a>
    <a href="http://name1.com">H I</a>
</div>

我想提取第一个/中间/姓氏,我理解如何使用一些字符串函数 - 我首先想知道的是,在上面的一个名称中出现空格的次数,以及也是第一次和最后一次出现的空间位置。我该怎么做?

1 个答案:

答案 0 :(得分:0)

众多解决方案之一:

let $doc :=
  <div class="names">
    <b>Names</b>
    <a href="http://name1.com">A B.C D</a>
    <a href="http://name1.com">E F G</a>
    <a href="http://name1.com">H I</a>
  </div>
for $a in $doc//a
return
  let $text := $a/text()
  let $spaces := index-of(string-to-codepoints($a), 32)
  return <result text="{ $text }" spaces="{ count($spaces) }"
    first="{ $spaces[1] }" last="{ $spaces[last()] }"/>

但是,使用regular expressions代替使用字符位置可能更容易。

相关问题