xsl-list
[Top] [All Lists]

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

2019-03-15 03:43:07
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
Saxonica

On 15 Mar 2019, at 06:07, Mukul Gandhi 
gandhi(_dot_)mukul(_at_)gmail(_dot_)com 
<xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com> 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 
language,

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="http://www.w3.org/1999/XSL/Transform 
<http://www.w3.org/1999/XSL/Transform>"    
                         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/..)}" 
parentsParentName="{local-name($yVar1/../..)}">
              <xsl:copy-of select="$yVar1"/>
           </parent>
           <xsl:variable name="yVar2" select="snapshot($yTemp)" 
as="element(y)"/>
           <parent parentsName="{local-name($yVar2/..)}" 
parentsParentName="{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 <http://www.mulberrytech.com/xsl/xsl-list>
EasyUnsubscribe <http://lists.mulberrytech.com/unsub/xsl-list/293509> (by 
email <>)
--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--
<Prev in Thread] Current Thread [Next in Thread>