Flows
Flows

Reputation: 3863

XQuery - Select and display child nodes using a pattern

I launched test from SoapUI and I want to figure out an XQuery assert. I am not able to write the XQuery request.

My xml file (answer request SoapUI)

<parent>
  <total1>10.000</total1>
  <total2>15</total2>
  <value1>1</value1>
  <value2>2</value2>
</parent>

Expected answer

I want only nodes starting with total, and the number value of node value

<parent>
  <total1>10.0</total1>
  <total2>15.0</total2>
</parent>

Current not working XQuery request

<parent>
{ for $n in //parent/*[starts-with(name(),'total')]
 return ($n/name() ,$n/number(text()) )
}
</parent>

This is not right because it display only node name. All nodes are on the same line : <parent> total1 10.0 total2 15.0 </parent> I am OK to do it using XQuery or XPath

Upvotes: 0

Views: 538

Answers (1)

Christian Gr&#252;n
Christian Gr&#252;n

Reputation: 6229

New element nodes can be created with element constructors:

<parent>{
  for $n in //parent/*[starts-with(name(), 'total')]
  return element { name($n) } { number($n) }
}</parent>

The function call number(<total1>10.000</total1>) yields 10 instead of 10.0. If you need a customized representation of your numeric values, the format-number function can be used:

<parent>{
  for $n in //parent/*[starts-with(name(), 'total')]
  return element { name($n) } { format-number(number($n), '#.0') }
}</parent>

Upvotes: 3

Related Questions