From: Michael W. <wes...@ja...> - 2021-07-06 10:26:04
|
The return statement can be shortened to just: return local:append-child($tokens) Let it all be done in the function. 2021年7月6日(火) 19:19 Michael Westbay <wes...@ja...>: > Hi Alfredo, > > This is a simple one, recursively adding the tokenized strings as the > element names: > > xquery version "3.0"; > > declare function local:append-child($tokens as xs:string*) as element()? { > if (count($tokens) ge 1) > then element {$tokens[1]} { > local:append-child(subsequence($tokens, 2)) > } > else () > }; > > let $string := 'teiHeader/fileDesc/titleStmt/title' > let $tokens := tokenize($string, '/') > return element {$tokens[1]} { > local:append-child(subsequence($tokens, 2)) > } > > > Drop that into a new eXide XQuery and this is the result: > > [image: image.png] > > > It helps to understand Functional Programming to really grasp XQuery. The > pseudo-code version of the above is: > > - Declare a recursive function named local:append-child that takes an > array of strings (0 or more) as the parameter > - If there are any tokens, create an element names with the first > string token > - Append child elements starting with the second token > - Otherwise, return the empty set > > The main part of the program is: > > - Assign "teiHeader/fileDesc/titleStmt/title" to $string > - Break $string into tokens by dividing the string at each "/" > - Create an element with the name of the first token > - Append child elements starting with the second token > > Hope this helps give you an idea of how to approach this. > > Take care. > > 2021年7月6日(火) 18:26 Alfredo Cosco <alf...@gm...>: > >> 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 >> > > > -- > Michael Westbay > Writer/System Administrator > http://www.japanesebaseball.com/ > -- Michael Westbay Writer/System Administrator http://www.japanesebaseball.com/ |