xquery- how to select value from a specific element even when that element has null values/multiple return-separated values

StackOverflow https://stackoverflow.com/questions/12897876

  •  07-07-2021
  •  | 
  •  

Domanda

Please consider the following XML--

<table class="rel_patent"><tbody>
<tr><td>Name</td><td>Description</td></tr>
<tr><td>A</td><td>Type-A</td></tr>
<tr><td>B</td><td>Type-B</td></tr>
<tr><td>C</td><td>Type-C</td></tr>
<tr><td>AC</td><td>Type-C
Type-A</td></tr>
<tr><td>D</td><td></td></tr>
</tbody></table>

Now I want to select and display all values of "Name" with corresp. values of "Description" element...even when Description element has null values viz element with name=D, and also, when description element has values separated by enter then I want those values (of Description) in separate rows- viz Type-C and Type-A for element with name=AC

This is the type of query I have written--

let $rows_data:= $doc//table[@class="rel_patent"]/tbody/tr[1]/following-sibling::tr
for $data_single_row in $rows_data
return 
    let $cited_name:= $data_single_row/td[1]
    let $original_types_w_return:= $data_single_row/td[4]
    let $original_types_list:=    tokenize($original_types_w_return, '(\r?\n|\r)$')
    for $cited_type_each at $pos2 in $original_types_list
    return concat( $cited_name, '^', $original_type_each, '^', $pos2)

However, I am getting the following type of response--

A^Type-A^1
B^Type-B^1
C^Type-C^1
AC^Type-C
Type-A^1

Now, I need to get the following correct in the above code+response---

(1) The data for "AC" should be 2 separate rows with "Type-C" and "Type-A" being in each of the 2 rows along with corresp. value for last field in each row as 1 and 2 (because these are 2 values)

(2) The data for "D" is not being shown at all.

How do I correct the above code to conform with these 2 requirements?

È stato utile?

Soluzione

This works:

for $data_single_row in $rows_data
return 
    let $cited_name:= $data_single_row/td[1]
    let $original_types_w_return:= $data_single_row/td[2]
    let $original_types_list:=    tokenize(concat($original_types_w_return, " "), '(\r?\n|\r)')
    for $cited_type_each at $pos2 in $original_types_list
    return concat( $cited_name, '^', normalize-space($cited_type_each), '^', $pos2)

(The first change was to replace $original_type_each with $cited_type_each and [4] with [2] which may ).

The first problem can be solved by removing the $ at the end of the tokenize parameter, since in the default mode $ only match the end of the string.

The second one is solved by adding an space $original_types_w_return, so it is not empty and tokenize returns something, and then removing it again with normalize-space (in XQuery 3.0 it could probably be solved by using 'allowing empty' in the for expression)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top