I am trying to flatten an XML hierarchy to the text nodes where the output tag names are the hyphen concatenated tags on the path of each text node. I amusing the sample XML provided at http://www.chilkatsoft.com/xml-samples/bookstore.xml as input.
So far I have created this XQuery:
declare function local:flatten($prefix as xs:string*, $nodes as node()*) as node()*
{
for $node in $nodes
return
typeswitch($node)
case element() return
local:flatten(insert-before($prefix, 1, $node/name()), $node/node())
case text() return
element {string-join(fn:reverse($prefix), '-')} {string($node)}
default return
'oops'
};
for $b in //bookstore/book return
local:flatten((), $b)
This mostly works but the output looks like:
<?xml version="1.0" encoding="UTF-8"?>
<book>
</book>
<book-title>The Iliad and The Odyssey</book-title>
<book>
</book>
<book-price>12.95</book-price>
<book>
</book>
<book-comments>
</book-comments>
<book-comments-userComment> Best translation I've read. </book-comments-userComment>
<book-comments>
</book-comments>
<book-comments-userComment> I like other versions better. </book-comments-userComment>
<book-comments>
</book-comments>
<book>
</book>
...
I don't care about the lack of a root element at this time (one step at a time) but I don't want the spurious empty parent tags between the correctly rendered text nodes. What am I doing wrong? I am very new to XQuery and may have made a silly error.
for $book in bookstore/book, $text in $book//text()[normalize-space()] return element { string-join(($text/ancestor-or-self::* except $book/ancestor::*)/name(), '-') } { data($text) }, see xqueryfiddle.liberty-development.net/jyyiVhi - Martin Honnen