How to transform XML into HTML using XSL? -
How to transform XML into HTML using XSL? -
i need transform xml document html using xsltprocessor.
my xml document is:
<?xml version="1.0" encoding="utf-8"?> <items> <item> <property title="title1"><![cdata[data1]]></property> <property title="title2"><![cdata[data3]]></property> <property title="title3"><![cdata[data3]]></property> </item> <item> <property title="title4"><![cdata[data4]]></property> <property title="title5"><![cdata[data5]]></property> <property title="title6"><![cdata[data6]]></property> </item> </items>
what have is:
<html> <table border="1"> <tr bgcolor="#eee"><td colspan="2">title1: data1</td></tr> <tr><td> title2</td> <td>data2</td></tr> <tr><td> title3</td> <td>data3</td></tr> <tr bgcolor="#eee"><td colspan="2">title4: data4</td></tr> <tr><td> title5</td> <td>data5</td></tr> <tr><td> title6</td> <td>data6</td></tr> </table> </html>
my xsl file is:
<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:php="http://php.net/xsl" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output method="html" encoding="utf-8" indent="yes"/> <xsl:template match="/"> <html> <xsl:apply-templates select="items"/> </html> </xsl:template> <xsl:template match="items"> <table border="1"> <xsl:apply-templates select="item"/> </table> </xsl:template> <xsl:template match="item"> <tr bgcolor="#eee"> <td colspan="2"> <xsl:value-of select="/descendant::*/@*"/>: <xsl:value-of select="property"/> </td> </tr> </xsl:template> </xsl:stylesheet>
but returns first tag "property". new in xslt, have list of "property" tag?
you have used templates fine @ beginning, go on that, writing templates , applying them:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output method="html" encoding="utf-8" indent="yes"/> <xsl:template match="/"> <html> <xsl:apply-templates select="items"/> </html> </xsl:template> <xsl:template match="items"> <table border="1"> <xsl:apply-templates/> </table> </xsl:template> <xsl:template match="item/property[1]"> <tr bgcolor="#eee"> <td colspan="2"> <xsl:value-of select="@title"/>: <xsl:value-of select="."/> </td> </tr> </xsl:template> <xsl:template match="item/property[not(position() = 1)]"> <tr> <td><xsl:value-of select="@title"/></td> <td><xsl:value-of select="."/></td> </tr> </xsl:template> </xsl:stylesheet>
html xml xslt
Comments
Post a Comment