Subject: [xsl] best practices for using XSLT modes From: "Mukul Gandhi gandhi.mukul@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> Date: Wed, 4 Dec 2019 06:42:22 -0000 |
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)? -- Regards, Mukul Gandhi
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] Why does this XPath expre, Mukul Gandhi gandhi. | Thread | Re: [xsl] best practices for using , Martin Honnen martin |
Re: [xsl] Why does this XPath expre, Mukul Gandhi gandhi. | Date | Re: [xsl] best practices for using , Martin Honnen martin |
Month |