Question

if I have this simple 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'd like to get all the employees whose state name begins with "I". So I've written the query:

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

but the function starts-with always returns false. I've also tried with

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

but it's the same. Where am I wrong? Thanks

Was it helpful?

Solution

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

Should do the trick, there's a lot of whitespace at the start of the state element

OTHER TIPS

OR your query will work when the xml structure is like this -

<employees>
 <employee>
   <name>Lorenzo Vinci</name>
   <address>Via Rosa Luxemburg 68 A, Imola
      <state>Italia</state>
   </address>
 </employee>
 ......
 ......
</employees>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top