|
From: Joe W. <jo...@gm...> - 2021-07-06 15:29:49
|
Hi Alfredo,
It's always fun to see different approaches to the same problem. To add one
more approach to the mix, this uses XQuery 3.0's fold-right() function
(which has been available in eXist since v2.2+) and arrow operator
(available since v3.1.0+):
"teiHeader/fileDesc/titleStmt/title"
=> tokenize("/")
=> fold-right((), function($segment, $results) { element { $segment } {
$results } })
If you're not familiar with the arrow operator (
https://www.w3.org/TR/xquery-3/#id-arrow-operator), you can use it to pipe
the results of an expression into a follow-on function as its first
parameter.
On the final line, we use the fold-right function (
https://www.w3.org/TR/xpath-functions-31/#func-fold-right) to take the path
segments, from right-to-left, and cumulatively build up the element
structure from innermost to outermost:
title --> <title/>
titleStmt --> <titleStmt><title/></titleStmt>
fileDesc --> <fileDesc><titleStmt><title/></titleStmt></fileDesc>
teiHeader -->
<teiHeader><fileDesc><titleStmt><title/></titleStmt></fileDesc></teiHeader>
The result:
<teiHeader>
<fileDesc>
<titleStmt>
<title/>
</titleStmt>
</fileDesc>
</teiHeader>
Another use of fold-right and discussion of this function is in my article
on parsing Roman numerals:
https://joewiz.org/2021/05/30/converting-roman-numerals-with-xquery-xslt/.
Joe
On Tue, Jul 6, 2021 at 5:26 AM Alfredo Cosco <alf...@gm...>
wrote:
> Hi all,
> I'm working on eXist 4.7.
> I have a string like this:
> teiHeader/fileDesc/titleStmt/title
>
> Is there a way to transform it and store in a node like:
> <teiHeader>
> <fileDesc>
> <titleStmt>
> <title/>
> <titleStmt>
> </fileDesc>
> </teiHeader>
>
> Thanks,
> Alfredo
> _______________________________________________
> Exist-open mailing list
> Exi...@li...
> https://lists.sourceforge.net/lists/listinfo/exist-open
>
|