xsl-list
[Top] [All Lists]

[xsl] XSLT 3.0 schema awareness vs non schema awareness

2019-02-27 00:57:11
Hi all,
    Please consider following XSLT examples (I'm using the XSLT 3.0
version), and my questions thereafter,
(in the presented stylesheets, I'm using XPath 3.0's mapping expressions
[using the operator !]. But my question is not about the ! operator)

I'm using Saxon EE 9.9. Saxon EE 9.7 also produces similar results.

Input XML:

<?xml version="1.0" encoding="UTF-8"?>
<test>
   <val>5</val>
   <val>4</val>
   <val>3</val>
   <val>0</val>
   <val>2</val>
   <val>0</val>
   <val>1</val>
</test>

Stylesheet 1:
(in this stylesheet, I have an embedded schema & I'm performing schema
aware transformation)

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                        xmlns:xs="http://www.w3.org/2001/XMLSchema";
                        xmlns:fn="http://xslt-functions";
                        exclude-result-prefixes="xs fn"
                        version="3.0">

    <xsl:output method="xml" indent="yes"/>

    <!-- an XML schema imported into the stylesheet -->
    <xsl:import-schema>
       <xs:schema>
          <xs:element name="test">
             <xs:complexType>
                <xs:sequence>
                   <xs:element name="val" type="xs:integer"
maxOccurs="unbounded"/>
                </xs:sequence>
             </xs:complexType>
          </xs:element>
       </xs:schema>
    </xsl:import-schema>

    <xsl:template match="/">
       <result>
          <xsl:variable name="x" select="test/val"/>
          <xsl:copy-of select="$x!fn:sum(., 5)"/>
       </result>
    </xsl:template>

    <!-- function to add two numbers -->
    <xsl:function name="fn:sum" as="xs:integer">
       <xsl:param name="num" as="xs:integer" />
       <xsl:param name="incr" as="xs:integer" />

       <xsl:sequence select="$num + $incr"/>
    </xsl:function>

</xsl:stylesheet>

The above stylesheet, when applied to the input XML, produces following
output,

<?xml version="1.0" encoding="UTF-8"?>
<result>10 9 8 5 7 5 6</result>

Stylesheet 2:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                         xmlns:xs="http://www.w3.org/2001/XMLSchema";
                         xmlns:fn="http://xslt-functions";
                         exclude-result-prefixes="xs fn"
                         version="3.0">

    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="/">
       <result>
          <xsl:variable name="x" select="test/val" as="xs:integer*"/>
          <xsl:copy-of select="$x!fn:sum(., 5)"/>
       </result>
    </xsl:template>

    <!-- function to add two numbers -->
    <xsl:function name="fn:sum" as="xs:integer">
       <xsl:param name="num" as="xs:integer" />
       <xsl:param name="incr" as="xs:integer" />

       <xsl:sequence select="$num + $incr"/>
    </xsl:function>

</xsl:stylesheet>

In above stylesheet, I don't use an embedded schema and don't use schema
aware transformation. In the above stylesheet, I add the syntax
*as="xs:integer*"* to variable declaration <xsl:variable name="x" ...
The stylesheet above, produces same output as Stylesheet 1 when applied to
the same input XML document.

Stylesheet 3:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                         xmlns:xs="http://www.w3.org/2001/XMLSchema";
                         xmlns:fn="http://xslt-functions";
                         exclude-result-prefixes="xs fn"
                         version="3.0">

    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="/">
       <result>
          <xsl:variable name="x" select="test/val" />
          <xsl:copy-of select="$x!fn:sum(., 5)"/>
       </result>
    </xsl:template>

    <!-- function to add two numbers -->
    <xsl:function name="fn:sum" as="xs:integer">
       <xsl:param name="num" as="xs:integer" />
       <xsl:param name="incr" as="xs:integer" />

       <xsl:sequence select="$num + $incr"/>
    </xsl:function>

</xsl:stylesheet>

In the above stylesheet, I don't use *as="xs:integer*"* on the mentioned
variable. Otherwise, the above stylesheet is same as Stylesheet 2.
The stylesheet above, produces same output as stylesheets 1 and 2 when
applied to the same input XML document.

That is, all above three stylesheets produce same output with the same
input XML document.

I wish to know the, pros and cons when comparing above three mentioned
stylesheets, in terms of functionality, performance and may be other
factors. I guess, schema aware transformations in general, are more suited
when rigorous static type checking is needed.




-- 
Regards,
Mukul Gandhi

alt address : mukulgandhi(_at_)acm(_dot_)org
--~----------------------------------------------------------------
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>