xquerybasex

Why does BaseX XQuery result format change when I wrap the results in a new element?


I'm building up an XQuery piece by piece in the BaseX editor (my first XQuery attempt). At the last step, the result format changed and I don't understand why. I am really having a hard time with XQuery syntax (book on order). I had expected a "pretty print" view as had been returned on all the steps leading up the to the end, but the view changed.

This XQuery script:

for $pc in /fhx/batch_equipment_phase_class
let $pc_name := $pc/@name
return
  for $fb in $pc/phase_class_algorithm/function_block[@name != 'FAIL_MONITOR']
  let $fb_name := $fb/@name
  return
    for $step in /fhx/function_block_definition[@name = $fb/@definition]/sfc_algorithm/step
    let $step_name := $step/@name
    return
      for $action in $step/action
      let $action_name := $action/@name
      return
        (<phase_class>{data($pc_name)}</phase_class>,
        <fb>{data($fb_name)}</fb>,
        <step>{data($step_name)}</step>,
        <action>{data($step_name)}</action>,
        $action/*)

gives an output like this:

<phase_class>4_AC25_CIPSU</phase_class>
<fb>RUN_LOGIC</fb>
<step>CLEAR_INT_BYP</step>
<action>CLEAR_INT_BYP</action>
<description>Step Message.</description>
<action_type>ASSIGN</action_type>
<qualifier>P</qualifier>
<expression>'^/STEP_MSG.CV' := "Clearing Interlock Bypasses";</expression>
<delay_time>0</delay_time>

But modifying to add the <row> element wrapper:

for $pc in /fhx/batch_equipment_phase_class
let $pc_name := $pc/@name
return
  for $fb in $pc/phase_class_algorithm/function_block[@name != 'FAIL_MONITOR']
  let $fb_name := $fb/@name
  return
    for $step in /fhx/function_block_definition[@name = $fb/@definition]/sfc_algorithm/step
    let $step_name := $step/@name
    return
      for $action in $step/action
      let $action_name := $action/@name
      return
      <row>
        <phase_class>{data($pc_name)}</phase_class>
        <fb>{data($fb_name)}</fb>
        <step>{data($step_name)}</step>
        <action>{data($step_name)}</action>
        {$action/*}
      </row>

it changes to this:

<row><phase_class>4_AC25_CIPSU</phase_class><fb>RUN_LOGIC</fb><step>CLEAR_INT_BYP</step><action>CLEAR_INT_BYP</action><description>Step Message.</description><action_type>ASSIGN</action_type><qualifier>P</qualifier><expression>'^/STEP_MSG.CV' := "Clearing Interlock Bypasses";</expression><delay_time>0</delay_time></row>

I expected this:

<row>
   <phase_class>4_AC25_CIPSU</phase_class>
   <fb>RUN_LOGIC</fb>
   <step>CLEAR_INT_BYP</step>
   <action>CLEAR_INT_BYP</action>
   <description>Step Message.</description>
   <action_type>ASSIGN</action_type>
   <qualifier>P</qualifier>
   <expression>'^/STEP_MSG.CV' := "Clearing Interlock Bypasses";</expression>
   <delay_time>0</delay_time>
</row>

Please help me understand why the format changes and what syntax I should be using to get the output format I had expected.


Solution

  • By default, descendant nodes of XML are not indented. You change change this by prepending the following output declaration to your query:

    declare option output:indent 'yes';