html - How do you move an attribute in the first child of a parent to the parent in XSLT? -
i'm beginner xslt, i'm trying convert flash text format html based format
there <li></li>
blocks in source xml , <li>
blocks contains 1 or many <font>
nodes. need apply styles of <font>
in inline css <li>
, remove <font>
node (first font child).
( example explanation - start ) from:
<li> <font face="lato" size="24" color="#f7941d" letterspacing="0" kerning="0"> <i>ertrr</i> <font size="12" color="#4b4b4b">sdfsd</font> </font> </li>
to:
<li style="font-family:lato; font-size:24px; color:#f7941d;"> <i>ertrr</i> <span style="font-size:12px; color:#4b4b4b;">sdfsd</span> </li>
( example explanation - end )
xml source
<root> <textformat leading="2"> <li> <font face="lato" size="24" color="#f7941d" letterspacing="0" kerning="0"> <i>ertrr</i> <font size="12" color="#4b4b4b"></font> </font> </li> </textformat> <textformat leading="2"> <li> <font face="lato" size="24" color="#000000" letterspacing="0" kerning="0"> <i><u>ert</u></i> <font size="12" color="#4b4b4b"></font> </font> </li> </textformat> <textformat leading="2"> <li> <font face="system" size="16" color="#4b4b4b" letterspacing="0" kerning="0"> <b>hgjgj</b> <font face="lato" size="12"></font> </font> </li> </textformat> <textformat leading="2"> <li> <font face="system" size="16" color="#4b4b4b" letterspacing="0" kerning="0">ghjghj <font face="lato" size="12"></font> </font> </li> </textformat> <textformat leading="2"> <li> <font face="lato" size="12" color="#4b4b4b" letterspacing="0" kerning="0">@#dgsdg <font face="gabriola">sdfgdfg</font> dsfg df <font size="16">gdsfg</font>sd s <font face="lucida console">d <i>fg df</i> gs <font face="verdana">dg sdgfgsd</font> </font> gdfg </font> </li> </textformat> <textformat leading="2"> <li> <font face="lato" size="24" color="#000000" letterspacing="0" kerning="0"> <i><u>ert</u></i> <font size="12" color="#4b4b4b">sdfsd</font> </font> </li> </textformat> </root>
expected output
<div> <li style="font-family:lato; font-size:24px; color:#f7941d;"> <i>ertrr</i><span style="font-size:12px; color:#4b4b4b;"></span> </li> <li style="font-family:lato; font-size:24px; color:#000000;"> <i><u>ert</u></i><span style="font-size:12px; color:#4b4b4b;"></span> </li> <li style="font-family:system; font-size:16px; color:#4b4b4b;"> <b>hgjgj</b><span style="font-family:lato; font-size:12px; "></span> </li> <li style="font-family:system; font-size:16px; color:#4b4b4b;"> ghjghj <span style="font-family:lato; font-size:12px; "></span> </li> <li style="font-family:lato; font-size:12px; color:#4b4b4b;"> @#dgsdg <span style="font-family:gabriola; ">sdfgdfg</span> dsfg df <span style="font-size:16px; ">gdsfg</span>sd s <span style="font-family:lucida console; ">d <i>fg df</i> gs <span style="font-family:verdana; ">dg sdgfgsd</span></span> gdfg </li> <li style="font-family:lato; font-size:24px; color:#000000;"> <i><u>ert</u></i><span style="font-size:12px; color:#4b4b4b;">sdfsd</span> </li> </div>
my code:
<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform" version="1.0"> <xsl:strip-space elements="*"/> <xsl:output indent="yes" method="html"/> <!-- identity template --> <xsl:template match="node()|@*"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template> <xsl:template match="root"> <div> <xsl:apply-templates/> </div> </xsl:template> <xsl:template match="font"> <span> <xsl:attribute name="style"> <!-- collect attributes --> <xsl:variable name="styles"> <xsl:if test="@face"> <xsl:value-of select="concat('font-family:', @face)"/> <xsl:text>; </xsl:text> </xsl:if> <xsl:if test="@size"> <xsl:value-of select="concat('font-size:', @size, 'px')"/> <xsl:text>; </xsl:text> </xsl:if> <xsl:if test="@color"> <xsl:value-of select="concat('color:', @color)"/> <xsl:text>;</xsl:text> </xsl:if> </xsl:variable> <xsl:value-of select="$styles"/> </xsl:attribute> <xsl:apply-templates/> </span> </xsl:template> <!-- remove unwanted attributes --> <xsl:template match="@letterspacing|@kerning"/> <!-- replace <li> <li> --> <xsl:template match="li"> <li> <xsl:attribute name="style"> <!-- collect attributes --> <xsl:variable name="styles"> <xsl:if test="font/@face"> <xsl:value-of select="concat('font-family:', font/@face)"/> <xsl:text>; </xsl:text> </xsl:if> <xsl:if test="font/@size"> <xsl:value-of select="concat('font-size:', font/@size, 'px')"/> <xsl:text>; </xsl:text> </xsl:if> <xsl:if test="font/@color"> <xsl:value-of select="concat('color:', font/@color)"/> <xsl:text>;</xsl:text> </xsl:if> </xsl:variable> <!-- delete trailing spaces --> <xsl:value-of select="$styles"/> </xsl:attribute> <xsl:apply-templates/> </li> </xsl:template> <!-- remove textformat --> <xsl:template match="textformat"> <xsl:apply-templates/> </xsl:template> </xsl:stylesheet>
current output
<div> <li style="font-family:lato; font-size:24px; color:#f7941d;"> <span style="font-family:lato; font-size:24px; color:#f7941d;"><i>ertrr</i><span style="font-size:12px; color:#4b4b4b;"></span></span> </li> <li style="font-family:lato; font-size:24px; color:#000000;"> <span style="font-family:lato; font-size:24px; color:#000000;"><i><u>ert</u></i><span style="font-size:12px; color:#4b4b4b;"></span></span> </li> <li style="font-family:system; font-size:16px; color:#4b4b4b;"> <span style="font-family:system; font-size:16px; color:#4b4b4b;"><b>hgjgj</b><span style="font-family:lato; font-size:12px; "></span></span> </li> <li style="font-family:system; font-size:16px; color:#4b4b4b;"> <span style="font-family:system; font-size:16px; color:#4b4b4b;">ghjghj <span style="font-family:lato; font-size:12px; "></span></span> </li> <li style="font-family:lato; font-size:12px; color:#4b4b4b;"> <span style="font-family:lato; font-size:12px; color:#4b4b4b;">@#dgsdg <span style="font-family:gabriola; ">sdfgdfg</span> dsfg df <span style="font-size:16px; ">gdsfg</span>sd s <span style="font-family:lucida console; ">d <i>fg df</i> gs <span style="font-family:verdana; ">dg sdgfgsd</span></span> gdfg </span> </li> <li style="font-family:lato; font-size:24px; color:#000000;"> <span style="font-family:lato; font-size:24px; color:#000000;"><i><u>ert</u></i><span style="font-size:12px; color:#4b4b4b;">sdfsd</span></span> </li> </div>
you should create named template process attributes. following stylesheet need
<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:strip-space elements="*"/> <xsl:output indent="yes" omit-xml-declaration="yes"/> <!-- identity template --> <xsl:template match="node()|@*"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template> <xsl:template match="li"> <li> <xsl:attribute name="style"> <xsl:call-template name="collect_attributes"> <xsl:with-param name="face" select="*[1][name()='font']/@face"/> <xsl:with-param name="size" select="*[1][name()='font']/@size"/> <xsl:with-param name="color" select="*[1][name()='font']/@color"/> </xsl:call-template> </xsl:attribute> <xsl:apply-templates/> </li> </xsl:template> <xsl:template match="li/font[1]"> <xsl:apply-templates/> </xsl:template> <xsl:template match="font[not(parent::li)]"> <span> <xsl:attribute name="style"> <xsl:call-template name="collect_attributes"> <xsl:with-param name="face" select="@face"/> <xsl:with-param name="size" select="@size"/> <xsl:with-param name="color" select="@color"/> </xsl:call-template> </xsl:attribute> </span> </xsl:template> <!-- named template process attributes --> <xsl:template name="collect_attributes"> <xsl:param name="color"/> <xsl:param name="face"/> <xsl:param name="size"/> <!-- collect attributes --> <xsl:variable name="styles"> <xsl:if test="string-length($face) > 0"> <xsl:value-of select="concat('font-family:', $face)"/> <xsl:text>; </xsl:text> </xsl:if> <xsl:if test="string-length($size) > 0"> <xsl:value-of select="concat('font-size:', $size, 'px')"/> <xsl:text>; </xsl:text> </xsl:if> <xsl:if test="string-length($color) > 0"> <xsl:value-of select="concat('color:', $color)"/> <xsl:text>;</xsl:text> </xsl:if> </xsl:variable> <!-- delete trailing spaces --> <xsl:value-of select="normalize-space($styles)"/> </xsl:template> <xsl:template match="root"> <div> <xsl:apply-templates/> </div> </xsl:template> <xsl:template match="textformat"> <xsl:apply-templates/> </xsl:template> </xsl:stylesheet>
Comments
Post a Comment