0

如果我有这个简单的 file.xml:

<?xml version="1.0" encoding="UTF-8"?>

<employees>
<employee>
    <name>
        Lorenzo Vinci
    </name>
    <address>
        Via Rosa Luxemburg 68 A, Imola
        <state>
        Italia
        </state>
    </address>
</employee>
<employee>
    <name>
        Marco Di Nicola
    </name>
    <address>
        Via Venezia, Pescara
        <state>
            Italia
        </state>
    </address>
</employee>
<employee>
    <name>
        Luca Pompei
    </name>
    <address>
        Via qualcosa, Londra
        <state>
            England
        </state>
    </address>
</employee>
</employees>

我想获取州名以“I”开头的所有员工。所以我写了查询:

let $emp := doc("employees.xml")
for $e in $emp//employee
  let $state := $e/address/state
  return starts-with($state, "I")

但函数开头总是返回false。我也试过

return starts-with(string($state), "I")

但它是一样的。我哪里错了?谢谢

4

2 回答 2

3
return starts-with(normalize-space(string($state)), "I")

应该做的伎俩,状态元素的开头有很多空格

于 2014-05-13T12:47:38.430 回答
1

或者当 xml 结构如下时,您的查询将起作用 -

<employees>
 <employee>
   <name>Lorenzo Vinci</name>
   <address>Via Rosa Luxemburg 68 A, Imola
      <state>Italia</state>
   </address>
 </employee>
 ......
 ......
</employees>
于 2014-05-15T04:27:11.597 回答