<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>
<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. <?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>
#!/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)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">
Source: https://habr.com/ru/post/143103/
All Articles