From bdysonsmith at gmail.com Thu Feb 15 09:04:11 2018 From: bdysonsmith at gmail.com (Bridger Dyson-Smith) Date: Thu, 15 Feb 2018 12:04:11 -0500 Subject: [xquery-talk] Help understanding typeswitch-based Identity Transform Message-ID: 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 := ABC: The Alphabet Some Person First Field Second Field ; 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) { {local:dispatch($node/node())} }; declare function local:title($node) { {local:dispatch($node/node())} }; declare function local:author($node) { {local:dispatch($node/node())} }; local:dispatch($input) Output should look something like: ABC: The Alphabet Some Person First Field Second Field -------------- next part -------------- An HTML attachment was scrubbed... URL: From joewiz at gmail.com Fri Feb 16 08:01:59 2018 From: joewiz at gmail.com (Joe Wicentowski) Date: Fri, 16 Feb 2018 11:01:59 -0500 Subject: [xquery-talk] Help understanding typeswitch-based Identity Transform In-Reply-To: References: Message-ID: Hi Bridger, To be able to treat your field elements differently based on their @name, you'd have to add some conditional logic to the return clause of your "element(field)" case. You could place this logic right in local:dispatch, or you could create a new function, like local:field, which would take a field element and check the value of @name. You're right that typeswitch is limited to relatively simple sequence patterns, and doesn't accommodate full XPath expressions. Typically, typeswitch performs the high level node type or element name check, and then delegates more complex logic to functions. Joe On Thu, Feb 15, 2018 at 12:04 PM, Bridger Dyson-Smith wrote: > 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 := > > > ABC: The Alphabet > Some Person > > First Field > Second Field > > > ; > > 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) { > {local:dispatch($node/node())} > }; > > declare function local:title($node) { > {local:dispatch($node/node())} > }; > > declare function local:author($node) { > {local:dispatch($node/node())} > }; > > local:dispatch($input) > > Output should look something like: > > > ABC: The Alphabet > Some Person > First Field > > Second Field > > > > > _______________________________________________ > talk at x-query.com > http://x-query.com/mailman/listinfo/talk > -------------- next part -------------- An HTML attachment was scrubbed... URL: From bdysonsmith at gmail.com Fri Feb 16 08:12:16 2018 From: bdysonsmith at gmail.com (Bridger Dyson-Smith) Date: Fri, 16 Feb 2018 11:12:16 -0500 Subject: [xquery-talk] Help understanding typeswitch-based Identity Transform In-Reply-To: References: Message-ID: Hi Joe, thanks for the response. I'll explore moving the XPath checks and see where that gets me. Appreciatively, Bridger On Fri, Feb 16, 2018 at 11:01 AM, Joe Wicentowski wrote: > Hi Bridger, > > To be able to treat your field elements differently based on their @name, > you'd have to add some conditional logic to the return clause of your > "element(field)" case. You could place this logic right in local:dispatch, > or you could create a new function, like local:field, which would take a > field element and check the value of @name. > > You're right that typeswitch is limited to relatively simple sequence > patterns, and doesn't accommodate full XPath expressions. Typically, > typeswitch performs the high level node type or element name check, and > then delegates more complex logic to functions. > > Joe > > On Thu, Feb 15, 2018 at 12:04 PM, Bridger Dyson-Smith < > bdysonsmith at gmail.com> wrote: > >> 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 := >> >> >> ABC: The Alphabet >> Some Person >> >> First Field >> Second Field >> >> >> ; >> >> 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) { >> {local:dispatch($node/node())} >> }; >> >> declare function local:title($node) { >> {local:dispatch($node/node())} >> }; >> >> declare function local:author($node) { >> {local:dispatch($node/node())} >> }; >> >> local:dispatch($input) >> >> Output should look something like: >> >> >> ABC: The Alphabet >> Some Person >> First Field >> >> Second Field >> >> >> >> >> _______________________________________________ >> talk at x-query.com >> http://x-query.com/mailman/listinfo/talk >> > > -------------- next part -------------- An HTML attachment was scrubbed... URL: