📜 ⬆️ ⬇️

Add sugar to XSLT

A simple template that transforms XSLT with a slight addition of syntactic sugar to regular XSLT. Instead of the description, I give a small example in which all new constructions are used.

<px:each="item" x:if-attr="position() mod 2" x:attr-class="'odd'" xmlns:x="xslt-sugar"> <span class="date" x:value="date"/> <xsl:value-of select="title"/> <x:block x:if="description" x:value="concat(' ', description)" x:doe="yes"/> </p> 

After converting this code, we get the usual XSLT:
 <xsl:for-each select="item"> <p> <xsl:if test="position() mod 2"> <xsl:attribute name="class"><xsl:value-of select="'odd'"/></xsl:attribute> </xsl:if> <span class="date"><xsl:value-of select="date"/></span> <xsl:value-of select="title"/> <xsl:if test="description"> <xsl:value-of select="concat(' ', description)" disable-output-escaping="yes"/> </xsl:if> </p> </xsl:for-each> 
Note that the x:attr-name="val" parameters are output via <xsl:value-of select="val"/> , so enclose the string parameters in quotes, as in the example.

Actually the template xslsugar.xslt
 <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:x="xslt-sugar" version="1.0"> <xsl:output method="xml" indent="yes" encoding="utf-8"/> <xsl:template match="*|@*|text()|processing-instruction()|comment()"> <xsl:copy> <xsl:apply-templates select="*|@*|text()|processing-instruction()|comment()"/> </xsl:copy> </xsl:template> <xsl:template match="x:*|@x:*"/> <xsl:template match="x:block"> <xsl:apply-templates select="*|@*|text()|processing-instruction()|comment()"/> </xsl:template> <xsl:template match="@x:value"> <xsl:element name="xsl:value-of" namespace="http://www.w3.org/1999/XSL/Transform"> <xsl:attribute name="select"><xsl:value-of select="."/></xsl:attribute> <xsl:if test="../@x:doe"><xsl:attribute name="disable-output-escaping"><xsl:value-of select="../@x:doe"/></xsl:attribute></xsl:if> </xsl:element> </xsl:template> <xsl:template match="*[@x:if]"> <xsl:element name="xsl:if" namespace="http://www.w3.org/1999/XSL/Transform"> <xsl:attribute name="test"><xsl:value-of select="@x:if"/></xsl:attribute> <xsl:apply-templates select="." mode="element"/> </xsl:element> </xsl:template> <xsl:template match="*[@x:each]"> <xsl:element name="xsl:for-each" namespace="http://www.w3.org/1999/XSL/Transform"> <xsl:attribute name="select"><xsl:value-of select="@x:each"/></xsl:attribute> <xsl:apply-templates select="." mode="element"/> </xsl:element> </xsl:template> <xsl:template match="@x:if-attr"> <xsl:element name="xsl:if" namespace="http://www.w3.org/1999/XSL/Transform"> <xsl:attribute name="test"><xsl:value-of select="."/></xsl:attribute> <xsl:for-each select="../@x:*[starts-with(local-name(), 'attr-')]"> <xsl:element name="xsl:attribute" namespace="http://www.w3.org/1999/XSL/Transform"> <xsl:attribute name="name"><xsl:value-of select="substring(local-name(), 6)"/></xsl:attribute> <xsl:element name="xsl:value-of" namespace="http://www.w3.org/1999/XSL/Transform"> <xsl:attribute name="select"><xsl:value-of select="."/></xsl:attribute> </xsl:element> </xsl:element> </xsl:for-each> </xsl:element> </xsl:template> <xsl:template match="*" mode="element"> <xsl:copy> <xsl:apply-templates select="*|@*|text()|processing-instruction()|comment()"/> </xsl:copy> </xsl:template> <xsl:template match="x:block" mode="element"> <xsl:apply-templates select="*|@*|text()|processing-instruction()|comment()"/> </xsl:template> </xsl:stylesheet> 


How to use it and how to expand opportunities decide for yourself, here is just an idea.
')
PS: Script to transform your Sugar XSLT:
 #!/bin/sh cp $1 $1.bak xsltproc -o $1 xslsugar.xslt $1 
On Windows, this can be done through:
 msxsl.exe -o template.xslt template.xslt xslsugar.xslt 
(did not try)

PPS: If you want to use an existing xmlns: xsl, which is not correct, but convenient, then simply replace the xmlns:x="xslt-sugar" namespace in xslsugar.xslt with xmlns:x="http://www.w3.org/1999/XSL/Transform" and remove x:* from the empty template: <xsl:template match="@x:*"/> , as a result you can write without an additional namespace: <p xsl:each="item">

For the next version, and further development, look at code.google.com/p/xslt-sugar

Source: https://habr.com/ru/post/143103/


All Articles