Monday, 15 September 2014

xml - Xquery: nested ifs -


Basically I have several views which can be empty, if they are empty then I do not want to output it to parent node. If all the sequences are empty then I should have a node. Basically I have a code, which does not work:

  $ a: = // Let's run $ b: = //b Return & lt; Root & gt; {If (exists ($ a) or exists ($ b)) then & lt; Div id = "container" & gt; {If (exists ($ a)) then & lt; H2 & gt; Like & lt; / H2 & gt; & Lt; Div & gt; {$ A for loop for sequence ...} & lt; / Div & gt; Else ()} {If (present ($ B)) then & lt; H2 & gt; BS & lt; / H2 & gt; & Lt; Div & gt; {$ B for loop sequence ...} & lt; / Div & gt; Else ()} & lt; / Div & gt; Else ()} & lt; / Root & gt;  

You have a simple syntax error in your query:

  if (exists ($ a)) then & lt; H2 & gt; Like & lt; / H2 & gt; & Lt; Div & gt; {$ A for loop for sequence ...} & lt; / Div & gt; Other ()  

Then the section should be an exquire expression, but here you have two exquisite expressions, and lt; H2 & gt; node and & lt;

  If (exists ($ a)) then (& lt; h2 & gt; as  

is a clean move that you can use to organize To some extent the query expresion if (exists (a)) then b and () is equal to if (a) then b and () , since calling the boolean After it is presented around the predicate. In return, this is just equivalent to B [A] , as a method can be hoisted.

Using this trick, the query can be written more concise:

  let $ a: = // one let $ b: = // b Return & lt; Root & gt; {& Lt; Div id = "container" & gt; {(& Lt; h2 & gt; AS & lt; / h2 & gt;, & lt; div & gt; {...} & lt; / div & gt;) [$ a], (& lt; h2 & Gt; BS , & lt; div & gt; {.. ..}} ) [$ b])} & lt; / Div & gt; [$ A or $ b]} & lt; / Root & gt;  

No comments:

Post a Comment