relpipe-data/examples-xquery-atom.xml
author František Kučera <franta-hg@frantovo.cz>
Mon, 08 Apr 2019 11:56:24 +0200
branchv_0
changeset 254 23247d93a012
parent 244 d4f401b5f90c
child 262 846510a73535
permissions -rw-r--r--
examples: atom → recfile

<stránka
	xmlns="https://trac.frantovo.cz/xml-web-generator/wiki/xmlns/strana"
	xmlns:m="https://trac.frantovo.cz/xml-web-generator/wiki/xmlns/makro">
	
	<nadpis>Reading an Atom feed using XQuery</nadpis>
	<perex>converting arbitrary XML into relational data using XQuery</perex>
	<m:pořadí-příkladu>01100</m:pořadí-příkladu>

	<text xmlns="http://www.w3.org/1999/xhtml">
		
		<p>
			Atom Syndication Format is a standard for publishing web feeds a.k.a web syndication. 
			These feeds are usually consumed by a <em>feed reeder</em> that aggregates news from many websites and displays them in a uniform format.
			The Atom feed is an XML with a list of recent news containing their titles, URLs and short annotations.
			It also contains some metadata (website author, title etc.).
		</p>
		<p>
			Using this simple XQuery<m:podČarou>see <a href="https://en.wikibooks.org/wiki/XQuery">XQuery</a> at Wikibooks</m:podČarou>
			<em>FLWOR Expression</em>
			we convert the Atom feed into the XML serialization of relational data:
		</p>
		
		<m:pre jazyk="xq" src="examples/atom.xq" odkaz="ano"/>
		
		<p>
			This is similar operation to <a href="https://www.postgresql.org/docs/current/functions-xml.html">xmltable</a> used in SQL databases.
			It converts an XML tree structure to the relational form.
			In our case, the output is still XML, but in a format that can be read by <code>relpipe-in-xml</code>.
			All put together in a single shell script:
		</p>
		
		<m:pre jazyk="bash" src="examples/atom.sh"/>
		
		<p>Will generate a table with web news:</p>
		
		<m:pre jazyk="text" src="examples/atom.txt"/>
		
		<p>Or with <code>relpipe-out-recfile</code>, we will get output in the recfile format (<a href="https://www.gnu.org/software/recutils/">GNU Recutils</a>), like this:</p>
		
		<m:pre jazyk="text" src="examples/atom.rec"/>
		
		<p>
			For frequent usage we can create a script or funcrion called <code>relpipe-in-atom</code>
			that reads Atom XML on STDIN and generates relational data on STDOUT.
			And then do any of these:
		</p>
		
		<m:pre jazyk="bash"><![CDATA[wget … | relpipe-in-atom | relpipe-out-tabular
wget … | relpipe-in-atom | relpipe-out-csv
wget … | relpipe-in-atom | relpipe-out-gui
wget … | relpipe-in-atom | relpipe-out-nullbyte | while read_nullbyte published title url; do echo "$title"; done
wget … | relpipe-in-atom | relpipe-out-recfile
]]></m:pre>

		<p>
			There are several implementations of XQuery.
			<a href="http://galax.sourceforge.net/">Galax</a> is one of them. 
			<a href="http://xqilla.sourceforge.net/">XQilla</a> or
			<a href="http://basex.org/basex/xquery/">BaseX</a> are another ones (and support newer versions of the standard).
			There are also XSLT processors like <a href="http://xmlsoft.org/XSLT/xsltproc2.html">xsltproc</a>.
			BaseX can be used instead of Galax – we just replace
			<code>galax-run -context-item /dev/stdin</code> with <code>basex -i /dev/stdin</code>.
		</p>
		
		<p>
			Reading Atom feeds in a terminal might not be the best way to get news from a website,
			but this simple example learns us how to convert arbitrary XML to relational data.
			And of course, we can generate multiple relations from a single XML using a single XQuery script.
			XQuery can be also used for operations like JOIN or UNION and for filtering and other transformations
			as will be shown in further examples.
		</p>
		
	</text>

</stránka>