My xml is:
<record>
<field name="f1"/>
<id name="f2"/>
<id name="f3"/>
<field name="f4"/>
<info/>
</record>
I want to loop through it in xquery like this:
for $i in $records/record/field | $records/record/id
return
if ( .... $i is id .... ) then .... do something .... else ... do something else ...
Is this possible? How can distinguish when $i
is id and when it is field?
Another neat XQuery solution is to use typeswitch:
for $i in $records/record/field | $records/record/id
return
typeswitch($i)
case element(id) return ...
case element(field) return ...
default return ...
Instead of those attempts with name() or local-name() I would use self::id e.g.
if ($i[self::id]) then ... else ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With