Short circuited or

Viewed 41

Is there a way to do a short circuited or operator in XQuery like you can do in C#, Java, JavaScript, etc?

I have some XML that can be in two formats, either:

<sometag>
  <bodytext>
    <section>
      <bodytext>
        <h1>Some html here</h1>
        <h2>Some more html</h2>
      </bodytext>
    </section>
  </bodytext>
</sometag>

or

<sometag>
  <bodytext>
        <h1>Some html here</h1>
        <h2>Some more html</h2>
  </bodytext>
</sometag>

I'm trying to return the html that's inside the inner body text tag. If i do

/sometag/bodytext/section/bodytext/* 

will return the first scenario

/sometag/bodytext/*

it will return the second scenario.

2 Answers

Well, you can always use /sometag/innermost(.//bodytext)/* if the function is supported by your XQuery processor or version. Or /sometag//bodytext[not(.//bodytext)]/*.

You may want to use a simple if-then-else condition:

let $v1 := /sometag/bodytext/section/bodytext/*
let $v2 := /sometag/bodytext/*
return 
  if (fn:exists($v1)) 
  then $v1 
  else (
    $v2
  )
Related