How To Apply More Then One Xsl-templates On One Xml Node
here is my problem: I have HTML Document with CSS Styles. I need to move these styles into the elements in the body and remove them from style tag. I made helper that makes xpath f
Solution 1:
Here is one way you could do it, by use of mode
attributes, to apply each template one after another
<xsl:stylesheetxmlns:xsl="http://www.w3.org/1999/XSL/Transform"version="1.0"><xsl:templatematch="@*|node()"><xsl:copy><xsl:apply-templatesselect="@*|node()" /></xsl:copy></xsl:template><xsl:templatematch="@class"><xsl:attributename="style"><xsl:iftest="@style"><xsl:value-ofselect="@style" /><xsl:text>;</xsl:text></xsl:if><xsl:apply-templatesselect="."mode="right-aligned" /></xsl:attribute></xsl:template><xsl:templatematch="@style" /><xsl:templatematch="@class"mode="right-aligned"><xsl:iftest="contains(.,'right-aligned')"><xsl:text>text-align: right !important;</xsl:text></xsl:if><xsl:apply-templatesselect="."mode="full-width" /></xsl:template><xsl:templatematch="@class"mode="full-width"><xsl:iftest="contains(.,'full-width')"><xsl:text>width: 100% !important; display: inline-block;</xsl:text></xsl:if></xsl:template></xsl:stylesheet>
If you could use XSLT 2.0, you could simplify this by use of priorities, instead of modes, and xsl:next-match
to find the next matching template with a lower priority.
<xsl:stylesheetxmlns:xsl="http://www.w3.org/1999/XSL/Transform"version="1.0"><xsl:templatematch="@*|node()"><xsl:copy><xsl:apply-templatesselect="@*|node()" /></xsl:copy></xsl:template><xsl:templatematch="@class"priority="10"><xsl:attributename="style"><xsl:iftest="@style"><xsl:value-ofselect="@style" /><xsl:text>;</xsl:text></xsl:if><xsl:next-match /></xsl:attribute></xsl:template><xsl:templatematch="@style" /><xsl:templatematch="@class[contains(.,'right-aligned')]"priority="9"><xsl:text>text-align: right !important;</xsl:text><xsl:next-match /></xsl:template><xsl:templatematch="@class[contains(.,'full-width')]"priority="8"><xsl:text>width: 100% !important; display: inline-block;</xsl:text><xsl:next-match /></xsl:template><xsl:templatematch="@class" /><!-- Stop the built-in template applying --></xsl:stylesheet>
Post a Comment for "How To Apply More Then One Xsl-templates On One Xml Node"