Re: [xsl] are there non streaming use cases of fn:snapshot function

Subject: Re: [xsl] are there non streaming use cases of fn:snapshot function
From: "Michael Kay mike@xxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Fri, 15 Mar 2019 08:42:42 -0000
I can't think of any persuasive use cases. If your original data fits in
memory, then there is little reason to make a snapshot copy of it, you might
as well use the original. The point about snapshot is that it is designed to
capture the content of a node and its immediate "relatives" (the ones you are
most likely to need access to) without the memory overhead of holding the
whole document; so it's all about memory use.

I guess there could be cases where the output of snapshot() happens to be
exactly the data that you want to put into a serialized message and send to
some other system, but it's more likely in general that you would want to
control the message content more precisely than this.

Michael Kay

> On 15 Mar 2019, at 06:07, Mukul Gandhi gandhi.mukul@xxxxxxxxx
<xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:
> Hi all,
>    I've read the XSLT 3.0 spec for fn:snapshot function. As stated in the
spec, this function has lots of uses while using streaming, which is great.
> I've come up with following XSLT 3.0 example (that runs fine even with Saxon
HE 9.8) using fn:snapshot function, when not using streaming features of XSLT
> XML input:
> <?xml version="1.0" encoding="UTF-8"?>
> <root>
>    <x id="a">
>      <y>5</y>
>    </x>
>    <x id="b">
>      <y>4</y>
>    </x>
>    <x id="c">
>      <y>3</y>
>    </x>
>    <x id="d">
>      <y>2</y>
>    </x>
>    <x id="e">
>      <y>1</y>
>    </x>
> </root>
> XSLT stylesheet:
> <?xml version="1.0" encoding="UTF-8"?>
> <xsl:stylesheet xmlns:xsl="
>                          version="3.0">
>     <xsl:output method="xml" indent="yes"/>
>     <xsl:template match="root">
>        <xsl:variable name="yTemp" select="x[2]/y"/>
>         <result>
>            <xsl:variable name="yVar1" as="element(y)">
>               <xsl:copy-of select="$yTemp"/>
>            </xsl:variable>
>            <parent parentsName="{local-name($yVar1/..)}"
>               <xsl:copy-of select="$yVar1"/>
>            </parent>
>            <xsl:variable name="yVar2" select="snapshot($yTemp)"
>            <parent parentsName="{local-name($yVar2/..)}"
>               <one>
>                  <xsl:copy-of select="$yVar2"/>
>               </one>
>               <two>
>                  <xsl:copy-of select="$yVar2/../.."/>
>               </two>
>            </parent>
>         </result>
>     </xsl:template>
> </xsl:stylesheet>
> The output of above XSLT transformation is:
> <?xml version="1.0" encoding="UTF-8"?>
> <result>
>    <parent parentsName="" parentsParentName="">
>        <y>4</y>
>    </parent>
>    <parent parentsName="x" parentsParentName="root">
>       <one>
>          <y>4</y>
>       </one>
>       <two>
>          <root>
>             <x id="b">
>                <y>4</y>
>             </x>
>          </root>
>       </two>
>    </parent>
> </result>
> In this example, I was also trying to understand the difference between
output of xsl:copy-of and fn:snapshot. The spec of fn:snapshot says, "Returns
a copy of a sequence, retaining copies of the ancestors and descendants of any
node in the input sequence, together with their attributes and namespaces".
> Particularly, the ability of fn:snapshot function to retain ancestors and
descendants of nodes (which xsl:copy-of can't do) in the input sequence amazed
me. The contents of element "two" in above XML output, reflects this. I
particularly like, the projection of "root" element ('root' and output below
it) in above example.
> I'm curious to know, what could be good non streaming use cases of
fn:snapshot function ?
> --
> Regards,
> Mukul Gandhi
> XSL-List info and archive <>
> EasyUnsubscribe <> (by
email <>)

Current Thread