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

Re: best practices for using XSLT modes

Subject: Re: best practices for using XSLT modes
From: "Martin Honnen martin.honnen@xxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Wed, 4 Dec 2019 06:51:04 -0000
Re:  best practices for using XSLT modes
Am 04.12.2019 um 07:42 schrieb Mukul Gandhi gandhi.mukul@xxxxxxxxx:
Hi all,
B  B I imagine that, using XSLT modes is useful. I've been trying
different XSLT approaches for solving a class of XML transformation
problems.

Below is an example of what I've tried (I present an XML document, two
different XSLT stylesheets [non schema aware] to process the XML
document, and an identical transformation output with both the
presented stylesheets):

XML document:

<?xml version="1.0" encoding="UTF-8"?>
<root>
B  B <a val="-1"/>
B  B <a val="-4"/>
B  B <a val="5"/>
B  B <a val="3"/>
B  B <a val="2"/>
</root>

Stylesheet 1:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
B  B  B  B  B  B  B  B  B  B  B  B  B version="3.0">

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

B  B  <xsl:template match="root">
B  B  B  B <result>
B  B  B  B  B  <xsl:apply-templates select="a[number(@val) gt 0]"
mode="gt0"/>
B B B B B <xsl:apply-templates select="a[number(@val) lt 0]"
mode="lt0"/>
B  B  B  B </result>
B  B  </xsl:template>

B  B  <xsl:template match="a" mode="gt0">
B  B  B  <val><xsl:value-of select="@val"/>: positive</val>
B  B  </xsl:template>

B  B  <xsl:template match="a" mode="lt0">
B  B  B  <val><xsl:value-of select="@val"/>: negative</val>
B  B  </xsl:template>

</xsl:stylesheet>

Stylesheet 2:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
B  B  B  B  B  B  B  B  B  B  B  B  B version="3.0">

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

B  B  <xsl:template match="root">
B  B  B  B <result>
B  B  B  B  B  <xsl:apply-templates select="a[number(@val) gt 0]"/>
B  B  B  B  B  <xsl:apply-templates select="a[number(@val) lt 0]"/>
B  B  B  B </result>
B  B  </xsl:template>

B  B  <xsl:template match="a[number(@val) gt 0]">
B  B  B  <val><xsl:value-of select="@val"/>: positive</val>
B  B  </xsl:template>

B  B  <xsl:template match="a[number(@val) lt 0]">
B  B  B  <val><xsl:value-of select="@val"/>: negative</val>
B  B  </xsl:template>

</xsl:stylesheet>

Both of above stylesheets, achieve the same thing and generate
following output:

<?xml version="1.0" encoding="UTF-8"?>
<result>
B  B <val>5: positive</val>
B  B <val>3: positive</val>
B  B <val>2: positive</val>
B  B <val>-1: negative</val>
B  B <val>-4: negative</val>
</result>

The intent of mentioned transformations, is that the result is little
reorganization of the input.

My questions are following,
Which of above mentioned XSLT transformations, is better over the
other, particularly considering the use of modes (conceptually &
possibly wrt to any other factors)?


That example seems to be too simple or artificial to show the value of
modes. In general I think modes have their value if you need to process
the same type of nodes twice e.g. once for generating a table of
contents and the second time for splitting into result documents. Or, in
the context of XSLT 3, if you need to separate processing steps working
with streamed input nodes from ones using grounded ones.

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.