Altova Mailing List Archives>Archive Index >comp.text.xml Archive Home >Recent entries >Thread Prev - Re: XSL problem >Thread Next - Re: XSL problem Re: XSL problemTo: NULL Date: 5/6/2004 7:48:00 PM "Andy Fish" <ajfish@b...> writes: > although there are "only a few" formatting elements, I certainly don't fancy > having to enumerate every possible combination of them. > > Fortunately performance will not be an issue so I can use your idea. You don't need to choose! Whilst watching television with my two-year-old this morning I realised that, of course, recursion is the proper way to maintain state-information in XSLT. (The challenge presented by the Fimbles is limited, you understand.) This solution combines the best of Martin's and mine: it maintains state information without recalculating it from scratch every time, and its size is only linear in the number of options considered. This XSLT, <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" > <xsl:output indent="yes"/> <xsl:template match="/text"> <doc> <xsl:apply-templates select="text()|node()"> <xsl:with-param name="italic" select="0"/> <xsl:with-param name="bold" select="0"/> </xsl:apply-templates> </doc> </xsl:template> <xsl:template match="text()"> <xsl:param name="italic"/> <xsl:param name="bold"/> <xsl:if test="normalize-space(.)"> <text> <xsl:if test="$bold"> <xsl:attribute name="bold">true</xsl:attribute> </xsl:if> <xsl:if test="$italic"> <xsl:attribute name="italic">true</xsl:attribute> </xsl:if> <xsl:value-of select="normalize-space(.)"/> </text> </xsl:if> </xsl:template> <xsl:template match="bold"> <xsl:param name="italic"/> <xsl:apply-templates select="text()|node()"> <xsl:with-param name="italic" select="$italic"/> <xsl:with-param name="bold" select="1"/> </xsl:apply-templates> </xsl:template> <xsl:template match="italic"> <xsl:param name="bold"/> <xsl:apply-templates select="text()|node()"> <xsl:with-param name="italic" select="1"/> <xsl:with-param name="bold" select="$bold"/> </xsl:apply-templates> </xsl:template> </xsl:stylesheet> with this XML <text> this is plain <bold> this is bold <italic> this is bold-italic </italic> </bold> this is plain <italic>this is italic <bold>this is italic-bold</bold> </italic> </text> gives this output <?xml version="1.0"?> <doc> <text>this is plain</text> <text bold="true">this is bold</text> <text bold="true" italic="true">this is bold-italic</text> <text>this is plain</text> <text italic="true">this is italic</text> <text bold="true" italic="true">this is italic-bold</text> </doc> Hope you enjoy it. Ben -- Ben Edgington Mail to the address above is discarded. Mail to ben at that address might be read. http://www.edginet.org/ | ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
