[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

are there non streaming use cases of fn:snapshot funct

Subject: are there non streaming use cases of fn:snapshot function
From: "Mukul Gandhi gandhi.mukul@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Fri, 15 Mar 2019 06:06:51 -0000
 are there non streaming use cases of fn:snapshot funct
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"
                         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

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.