[xquery-talk] Help understanding typeswitch-based Identity Transform
Bridger Dyson-Smith
bdysonsmith at gmail.com
Thu Feb 15 09:04:11 PST 2018
Hi list --
I'm trying to get a better understanding of identity transforms in Xquery
and I don't understand the finer points of the approaches for dealing with
specific elements.
I've finally to become used to the XSLT approach, using a matching template
to process `field[@name='first']`, e.g. would someone be willing to shine
some light on different methods for dealing with this?
Thanks very much for your time and trouble.
Best,
Bridger
Example:
declare variable $input :=
<docs>
<doc>
<title>ABC: The Alphabet</title>
<author>Some Person</author>
<fields>
<field name="first">First Field</field>
<field name="second">Second Field</field>
</fields>
</doc>
</docs>;
declare function local:passthru( $node as node()* ){
local:dispatch($node/node())
};
declare function local:dispatch(
$nodes as node()*
) as item()* {
for $node in $nodes
return
typeswitch($node)
case text() return $node
case element(docs) return local:passthru($node)
case element(doc) return local:docf($node)
case element(title) return local:title($node)
case element(author) return local:author($node)
case element(fields) return local:passthru($node)
case element(field) return () (:return empty sequence:)
default return local:passthru($node)
};
declare function local:docf($node) {
<new>{local:dispatch($node/node())}</new>
};
declare function local:title($node) {
<new-title>{local:dispatch($node/node())}</new-title>
};
declare function local:author($node) {
<new-author>{local:dispatch($node/node())}</new-author>
};
local:dispatch($input)
Output should look something like:
<new>
<new-title>ABC: The Alphabet</new-title>
<new-author>Some Person</new-author>
<new-subject>First Field</new-subject>
<new-section>
<new-entry>Second Field</new-entry>
</new-section>
</new>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://x-query.com/pipermail/talk/attachments/20180215/c8f26b20/attachment.html>
More information about the talk
mailing list