![]() |
![]() | ![]() | ![]() | Altova Mailing List Archives>Archive Index >comp.text.xml Archive Home >Recent entries >Thread Prev - XSL problem >Thread Next - Re: XSL problem Re: XSL problemTo: NULL Date: 5/5/2004 4:25:00 PM Hi Andy, ajfish@b... (Andy Fish) writes: > I'm stuck with an XSL problem - can anyone give me any hints? > > I have some XML with nested formatting tags like this: > > <text> > this is plain > <bold> > this is bold > <italic> > this is bold-italic > </italic> > </bold> > this is plain > </text> > > which I need to 'flatten out' into something like this: > > <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> > > It doesn't have to work with any arbitrary tags - there are only a few > possible ones - but I'm not sure how to "remember" the outer level > formatting nodes when processing the text inside. It seems to be > crying out for some kind of state variable Here's a brute force version that uses XPath to look at the ancestor axis. Perhaps not as elegant as Martin's solution, but shorter and easier to extend (Martin's script will grow as the factorial of the number of options, I think, whereas this is only linear). This transformation: <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()"/> </doc> </xsl:template> <xsl:template match="text()"> <xsl:if test="normalize-space(.)"> <text> <xsl:if test="ancestor::bold"> <xsl:attribute name="bold">true</xsl:attribute> </xsl:if> <xsl:if test="ancestor::italic"> <xsl:attribute name="italic">true</xsl:attribute> </xsl:if> <xsl:value-of select="normalize-space(.)"/> </text> </xsl:if> </xsl:template> </xsl:stylesheet> with this input <text> this is plain <bold> this is bold <italic> this is bold-italic </italic> </bold> this is plain </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> </doc> 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 | |||||
|
