xsl-list
[Top] [All Lists]

Re: [xsl] best practices for using XSLT modes

2019-12-04 12:42:21
I also use modes for templates that implement functions, which I guess is 
really just a special case of reprocessing the same elements in a different 
way, now that I think about it.

I also try to follow the practice of putting modes (or sets of related modes) 
in separate XSLT modules, in part to make it easier to find the templates for a 
specific mode. So if I have a mode "generate-toc" I will probably have module 
generate-toc.xsl that implements just that mode.

Cheers,

E.

--
Eliot Kimber
http://contrext.com
 

On 12/4/19, 10:58 AM, "Dimitre Novatchev dnovatchev(_at_)gmail(_dot_)com" 
<xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com> wrote:

    I agree with Martin.
    I also use modes when performing multi-pass processing -- typically the XML 
document that is the result of pass-N-1 and is to be processed by pass-N has 
different structure and meaning than any of the documents produced by previous 
passes , or future documents, to be produced by future passes.
    
    So we have M sets of transformations, each possibly having template(s) 
matching the root node or identically named/typed items and processing these in 
a different, pass-specific way.
    
    Without using modes I wonder if it would be possible at all to do this 
processing in a single transformation -- and even if this is somehow (crazy) 
possible, the code would be a torture to read, understand and maintain.
    
    Cheers,
    Dimitre
    
    
    On Tue, Dec 3, 2019 at 10:51 PM Martin Honnen 
martin(_dot_)honnen(_at_)gmx(_dot_)de 
<xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com> wrote:
    
    
    Am 04.12.2019 um 07:42 schrieb Mukul Gandhi 
gandhi(_dot_)mukul(_at_)gmail(_dot_)com:
    > Hi all,
    >    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>
    >    <a val="-1"/>
    >    <a val="-4"/>
    >    <a val="5"/>
    >    <a val="3"/>
    >    <a val="2"/>
    > </root>
    >
    > Stylesheet 1:
    >
    > <?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">
    >        <result>
    >           <xsl:apply-templates select="a[number(@val) gt 0]" mode="gt0"/>
    >           <xsl:apply-templates select="a[number(@val) lt 0]" mode="lt0"/>
    >        </result>
    >     </xsl:template>
    >
    >     <xsl:template match="a" mode="gt0">
    >       <val><xsl:value-of select="@val"/>: positive</val>
    >     </xsl:template>
    >
    >     <xsl:template match="a" mode="lt0">
    >       <val><xsl:value-of select="@val"/>: negative</val>
    >     </xsl:template>
    >
    > </xsl:stylesheet>
    >
    > Stylesheet 2:
    >
    > <?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">
    >        <result>
    >           <xsl:apply-templates select="a[number(@val) gt 0]"/>
    >           <xsl:apply-templates select="a[number(@val) lt 0]"/>
    >        </result>
    >     </xsl:template>
    >
    >     <xsl:template match="a[number(@val) gt 0]">
    >       <val><xsl:value-of select="@val"/>: positive</val>
    >     </xsl:template>
    >
    >     <xsl:template match="a[number(@val) lt 0]">
    >       <val><xsl:value-of select="@val"/>: negative</val>
    >     </xsl:template>
    >
    > </xsl:stylesheet>
    >
    > Both of above stylesheets, achieve the same thing and generate
    > following output:
    >
    > <?xml version="1.0" encoding="UTF-8"?>
    > <result>
    >    <val>5: positive</val>
    >    <val>3: positive</val>
    >    <val>2: positive</val>
    >    <val>-1: negative</val>
    >    <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.
    
    
    
    
    
    
    
    
    XSL-List info and archive 
<http://www.mulberrytech.com/xsl/xsl-list>EasyUnsubscribe 
<http://lists.mulberrytech.com/unsub/xsl-list/1278982>
    (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>