Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get element type in xpath?

Tags:

xpath

xquery

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?

like image 425
danatel Avatar asked Mar 05 '10 06:03

danatel


2 Answers

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 ...
like image 54
Oliver Hallam Avatar answered Sep 19 '22 17:09

Oliver Hallam


Instead of those attempts with name() or local-name() I would use self::id e.g.

if ($i[self::id]) then ... else ...
like image 27
Martin Honnen Avatar answered Sep 17 '22 17:09

Martin Honnen