xsl-list
[Top] [All Lists]

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

2019-03-15 01:07:17
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
--~----------------------------------------------------------------
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>