Re: [Videlibri-xidel] How to merge preceding empty nodes?
Client for public libraries
Brought to you by:
benibela
|
From: Benito v. d. Z. <be...@be...> - 2023-04-22 10:40:23
|
Hi Peng, the standard has a few examples: https://www.w3.org/TR/xquery-31/#id-tumbling-windows You can also ask ChatGPT about these things: Explain the tumbling window syntax of XQuery. In XQuery, a tumbling window is a construct used to perform calculations on a set of consecutive items within a sequence. The syntax for defining a tumbling window in XQuery involves the use of the "window" clause, which allows you to specify the size and sliding behavior of the window. Here is the syntax for the tumbling window clause in XQuery: sqlCopy code |for tumbling window $window in $sequence start $start when $condition end $end when $condition return $result | Let's break down each part of the syntax: * "for tumbling window": This specifies that you are creating a tumbling window. * "$window": This is the name of the window that you are defining. * "in $sequence": This specifies the sequence of items that you want to operate on. * "start $start when $condition": This specifies when the window should start. "$start" is the name of the variable that holds the starting item, and "$condition" is a boolean expression that evaluates to true when the starting item is found. * "end $end when $condition": This specifies when the window should end. "$end" is the name of the variable that holds the ending item, and "$condition" is a boolean expression that evaluates to true when the ending item is found. * "return $result": This specifies the result of the window operation. "$result" is an expression that calculates the result based on the items in the window. In essence, the tumbling window syntax allows you to define a sliding window that "tumbles" over the sequence of items, performing some operation on each set of consecutive items within the window. Given an example that shows how to use this syntax with nodes from an XML document. Sure! Let's say we have an XML document containing information about books, like this: xmlCopy code |<books> <book> <title>Harry Potter and the Philosopher's Stone</title> <author>J.K. Rowling</author> <year>1997</year> </book> <book> <title>The Hobbit</title> <author>J.R.R. Tolkien</author> <year>1937</year> </book> <book> <title>To Kill a Mockingbird</title> <author>Harper Lee</author> <year>1960</year> </book> <book> <title>The Catcher in the Rye</title> <author>J.D. Salinger</author> <year>1951</year> </book> </books> | And we want to calculate the average publication year of books in sets of three, starting from the first book. We can use the tumbling window syntax to accomplish this: xqueryCopy code |let $books := doc("books.xml")/books/book for tumbling window $window in $books start at $start when position() = 1 end at $end when position() mod 3 = 0 return avg($window/year) | In this example, we first assign the sequence of "book" nodes to the variable "$books". Then, we create a tumbling window with the "for tumbling window" clause, using the variable "$books" as the sequence. The "start at" clause specifies that the window should start at the first item in the sequence, using the "position()" function to test the position of the current item. The "end at" clause specifies that the window should end at every third item in the sequence, again using the "position()" function and the modulo operator to check if the current position is a multiple of three. Finally, we use the "return" clause to calculate the average publication year of the items in each window, using the "avg()" function to average the "year" elements of the window. When we execute this query, we get the following result: xmlCopy code |19.57 1951 | The first line of the result is the average publication year of the first three books, and the second line is the average publication year of the last book. Bye, Benito On 22.04.23 12:09, Peng Yu wrote: > I am not familiar with tumbling window. It is not clear to me where is > a good resource for learning it quickly. Do you have a good reference? > > On 4/21/23, Benito van der Zander<be...@be...> wrote: >> Hi Peng, >> >> XQuery got an amazingly complicated syntax for that: >> >> xidel /tmp/x.xml --xquery '<a>{for tumbling window $w in /a/x >> start when true() >> end $x when $x != "" >> return <x >> label="{string-join($w/@label)}">{$x/node()}</x> >> }</a> ' --xml >> >> >> >> Cheers, >> Benito >> On 19.04.23 06:08, Peng Yu wrote: >>> <a> >>> <x label="1"></x> >>> <x label="2"></x> >>> <x label="3">y</x> >>> <x label="4"></x> >>> <x label="5"></x> >>> <x label="6">z</x> >>> </a> >>> >>> For the above input, I want to merge the preceding empty nodes with >>> nonempty ones. >>> >>> The output should look like this. >>> >>> <a> >>> <x label="123">y</x> >>> <x label="456">z</x> >>> </a> >>> >>> How can I make this transform with xidel? >>> > |