📜 ⬆️ ⬇️

ShortXSLT: simplified syntax for XSLT with insert statements, if, else, etc.

The Dklab_ShortXSLT library is a system for supporting simplified XSLT syntax for the PHP classes XSLTProcessor and DOMDocument built into PHP. In fact, this is a compiler from the XSLT dialect to standard XSLT, which is run on the fly and transparent for the calling code (of course, there is the possibility of caching so that the compilation starts only in the next template change). Where you use XSLT in PHP scripts, you can connect ShortXSLT by writing a few extra lines of code.

The standard XSLT syntax is very cumbersome, which is inconvenient when used in web programming. The library allows to alleviate this problem.

Abbreviated versions for the following constructions are supported: inserting values ​​out of tags, inserting a language constant, inserting a sprintf-like constant, if-then-elseif, foreach.
')
Example on ShortXSLT:
 ...
 <xsl: template match = "/">
   {if / some / node = 1}
     {#hello}, world!  {#my_name_is (/ my / name)}.
   {elseif / other / node = / some / node}
     <p> You have {/ money} dollars. </ p>
   {else}
     {foreach / nodes / *}
       Node {.} <br/>
     {/ foreach}
   {/ if}
 </ xsl: template>
 ...


And here is the same, but on pure XSLT:
 ...
 <xsl: template match = "/">
   <xsl: choose>
     <xsl: when test = "/ some / node = 1">
       <xsl: value-of select = "h: const ('hello')" />, world! 
       <xsl: value-of select = "h: const ('my_name_is', / my / name)" />.
     </ xsl: when>
     <xsl: when test = "/ other / node = / some / node">
       <p> You have <xsl: value-of select = "/ money" /> dollars. </ p>
     </ xsl: when>
     <xsl: otherwise>
       <xsl: for-each select = "/ nodes / *">
         Node <xsl: value-of select = "."  /> <br/>
       </ xsl: for-each>
     </ xsl: otherwise>
   </ xsl: choose>
 </ xsl: template>
 ...

ShortXSLT only expands the XSLT instruction set. All standard XSLT constructs remain available. For example, you can use <xsl: value-of select = “node” disable-output-escaping = “yes” /> to insert raw HTML into the resulting document (the {node} construct always inserts junk data).

The library is distributed under the LGPL license and is available for download on dklab.ru/lib/Dklab_ShortXSLT

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


All Articles