I have some following XML:
test
test
test
I recommend using xal:for-each-group
as suggested in the first comment by Maring Honnen. The other XPath approaches allow you to select the content between two processing instructions, but it will be hard to embed that in an xslt stylesheet that should do other things as well, like copying existing structure at the same time. Here a minimal approach using for-each-group:
xquery version "1.0-ml";
let $xml :=
before
test
test
test
after
let $xsl :=
return xdmp:xslt-eval($xsl, $xml)
Use a second for-each-group with group-ending-by as suggested by Martin if you like to get the end PI inside the tag. The above might be sufficient though.
HTH!