<div dir="ltr">Hi Bridger,<div><br></div><div>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.  </div><div><br></div><div>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.  </div><div><br></div><div>Joe</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Feb 15, 2018 at 12:04 PM, Bridger Dyson-Smith <span dir="ltr"><<a href="mailto:bdysonsmith@gmail.com" target="_blank">bdysonsmith@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div><div>Hi list --<br><br></div>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. <br><br></div>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?<br><br></div><div>Thanks very much for your time and trouble.<br></div><div>Best,<br></div><div>Bridger<br></div><div><br></div><div>Example:<br></div><div><div><pre style="background-color:rgb(255,255,255);color:rgb(0,0,0);font-family:"PT Mono";font-size:9.4pt"><span style="color:rgb(0,0,255)">declare variable </span>$input :=<br>  <<span style="color:rgb(142,73,179);font-weight:bold">docs</span>><br>    <<span style="color:rgb(142,73,179);font-weight:bold">doc</span>><br>      <<span style="color:rgb(142,73,179);font-weight:bold">title</span>>ABC: The Alphabet</<span style="color:rgb(142,73,179);font-weight:bold">title</span>><br>      <<span style="color:rgb(142,73,179);font-weight:bold">author</span>>Some Person</<span style="color:rgb(142,73,179);font-weight:bold">author</span>><br>      <<span style="color:rgb(142,73,179);font-weight:bold">fields</span>><br>        <<span style="color:rgb(142,73,179);font-weight:bold">field </span><span style="color:rgb(255,96,46)">name=</span><span style="color:rgb(0,128,0)">"first"</span>>First Field</<span style="color:rgb(142,73,179);font-weight:bold">field</span>><br>        <<span style="color:rgb(142,73,179);font-weight:bold">field </span><span style="color:rgb(255,96,46)">name=</span><span style="color:rgb(0,128,0)">"second"</span>>Second Field</<span style="color:rgb(142,73,179);font-weight:bold">field</span>><br>      </<span style="color:rgb(142,73,179);font-weight:bold">fields</span>><br>    </<span style="color:rgb(142,73,179);font-weight:bold">doc</span>><br>  </<span style="color:rgb(142,73,179);font-weight:bold">docs</span>>;<br><br><span style="color:rgb(0,0,255)">declare function </span>local:passthru( $node <span style="color:rgb(0,0,255)">as node</span>()* ){<br>  local:dispatch($node/<span style="color:rgb(0,0,255)">node</span>())<br>};<br><br><span style="color:rgb(0,0,255)">declare function </span>local:dispatch(<br>  $nodes <span style="color:rgb(0,0,255)">as node</span>()*<br>) <span style="color:rgb(0,0,255)">as item</span>()* {<br>  <span style="color:rgb(0,0,255)">for </span>$node <span style="color:rgb(0,0,255)">in </span>$nodes<br>  <span style="color:rgb(0,0,255)">return<br></span><span style="color:rgb(0,0,255)">    typeswitch</span>($node)<br>      <span style="color:rgb(0,0,255)">case text</span>() <span style="color:rgb(0,0,255)">return </span>$node<br>      <span style="color:rgb(0,0,255)">case element</span>(docs) <span style="color:rgb(0,0,255)">return </span>local:passthru($node)<br>      <span style="color:rgb(0,0,255)">case element</span>(doc) <span style="color:rgb(0,0,255)">return </span>local:docf($node)<br>      <span style="color:rgb(0,0,255)">case element</span>(title) <span style="color:rgb(0,0,255)">return </span>local:title($node)<br>      <span style="color:rgb(0,0,255)">case element</span>(author) <span style="color:rgb(0,0,255)">return </span>local:author($node)<br>      <span style="color:rgb(0,0,255)">case element</span>(fields) <span style="color:rgb(0,0,255)">return </span>local:passthru($node)<br>      <span style="color:rgb(0,0,255)">case element</span>(field) <span style="color:rgb(0,0,255)">return </span>() <span style="color:rgb(128,128,128);font-style:italic">(:return empty sequence:)<br></span><span style="color:rgb(128,128,128);font-style:italic">      </span><span style="color:rgb(0,0,255)">default return </span>local:passthru($node)<br>};<br><br><span style="color:rgb(0,0,255)">declare function </span>local:docf($node) {<br>  <<span style="color:rgb(142,73,179);font-weight:bold">new</span>>{local:dispatch($node/<span style="color:rgb(0,0,255)">nod<wbr>e</span>())}</<span style="color:rgb(142,73,179);font-weight:bold">new</span>><br>};<br><br><span style="color:rgb(0,0,255)">declare function </span>local:title($node) {<br>  <<span style="color:rgb(142,73,179);font-weight:bold">new-title</span>>{local:dispatch($<wbr>node/<span style="color:rgb(0,0,255)">node</span>())}</<span style="color:rgb(142,73,179);font-weight:bold">new-title</span>><br>};<br><br><span style="color:rgb(0,0,255)">declare function </span>local:author($node) {<br>  <<span style="color:rgb(142,73,179);font-weight:bold">new-author</span>>{local:dispatch($<wbr>node/<span style="color:rgb(0,0,255)">node</span>())}</<span style="color:rgb(142,73,179);font-weight:bold">new-author</span>><br>};<br><br>local:dispatch($input)<br><br></pre><pre style="background-color:rgb(255,255,255);color:rgb(0,0,0);font-family:"PT Mono";font-size:9.4pt"><font face="arial,helvetica,sans-serif">Ou<font face="arial,helvetica,sans-serif">tput should look some<font face="arial,helvetica,sans-serif">thing like:<br></font></font></font><br><font face="arial,helvetica,sans-serif"><font face="arial,helvetica,sans-serif"><font face="arial,helvetica,sans-serif"><<span style="color:rgb(142,73,179);font-weight:bold">new</span>><br>  <<span style="color:rgb(142,73,179);font-weight:bold">new-title</span>>ABC: The Alphabet</<span style="color:rgb(142,73,179);font-weight:bold">new-title</span>><br>  <<span style="color:rgb(142,73,179);font-weight:bold">new-author</span>>Some Person</<span style="color:rgb(142,73,179);font-weight:bold">new-author</span>><br>  <<span style="color:rgb(142,73,179);font-weight:bold">new-subject</span>>First Field</<span style="color:rgb(142,73,179);font-weight:bold">new-subject</span>><br>  <<span style="color:rgb(142,73,179);font-weight:bold">new-section</span>><br>    <<span style="color:rgb(142,73,179);font-weight:bold">new-entry</span>>Second Field</<span style="color:rgb(142,73,179);font-weight:bold">new-entry</span>><br>  </<span style="color:rgb(142,73,179);font-weight:bold">new-section</span>><br></<span style="color:rgb(142,73,179);font-weight:bold">new</span>></font></font></font></pre></div></div></div>
<br>______________________________<wbr>_________________<br>
<a href="mailto:talk@x-query.com">talk@x-query.com</a><br>
<a href="http://x-query.com/mailman/listinfo/talk" rel="noreferrer" target="_blank">http://x-query.com/mailman/<wbr>listinfo/talk</a><br></blockquote></div><br></div>