xml - Display elements and respective value in XSLT -
i xslt beginner, requirement getting element , respective value in table format, tried did not expectation, kindly suggest solution requirement.
my input xml is
<group> <elementgroup1> <element1>value1</element1> <element2/> <element3>value3</element3> </elementgroup1> <elementgroup2> <elementsubgroup2> <element4>value4</element4> <element5>value5</element5> </elementsubgroup2> </elementgroup2> </group>
my xslt is
<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform" xmlns:xs="http://www.w3.org/2001/xmlschema" exclude-result-prefixes="xs" version="2.0"> <xsl:template match="/"> <xsl:element name="table"> <xsl:attribute name="border" select="'1'"/> <xsl:element name="tr"> <xsl:element name="th">xml viewer</xsl:element> </xsl:element> <xsl:for-each select="/group//*"> <xsl:call-template name="mytemplate"/> </xsl:for-each> </xsl:element> </xsl:template> <xsl:template name="mytemplate"> <xsl:element name="tr"> <xsl:element name="td"> <xsl:value-of select="name()"></xsl:value-of> </xsl:element> <xsl:for-each select="child::*"> <xsl:element name="td"> <xsl:value-of select="."></xsl:value-of> </xsl:element> </xsl:for-each> </xsl:element> /xsl:template> </xsl:stylesheet>
my required output
<table border="1"> <tr> <th> output </th> </tr> <tr> <td> group </td> <td/> </tr> <tr> <td> elementgroup1 </td> <td/> </tr> <tr> <td> element1 </td> <td> value1 </td> </tr> <tr> <td> element2 </td> <td/> </tr> <td> element3 </td> <td> value3 </td> </tr> </table>
display output output should in table format, please see below
output group elementgroup1 element1 value1 element2 element3 value3 . . . go on till end
when understand right, need transformation this
<?xml version="1.0" encoding="utf-8" ?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output method="html" version="4.0" indent="yes"/> <xsl:template match="/"> <table> <xsl:apply-templates select="*"/> </table> </xsl:template> <xsl:template match="*"> <tr> <td><xsl:value-of select="name()"/></td> <td><xsl:value-of select="normalize-space(text())"/></td> </tr> <xsl:apply-templates select="*"/> </xsl:template> </xsl:stylesheet>
that produces following output of example xml
<table> <tr> <td>group</td> <td></td> </tr> <tr> <td>elementgroup1</td> <td></td> </tr> <tr> <td>element1</td> <td>value1</td> </tr> <tr> <td>element2</td> <td></td> </tr> <tr> <td>element3</td> <td>value3</td> </tr> <tr> <td>elementgroup2</td> <td></td> </tr> <tr> <td>elementsubgroup2</td> <td></td> </tr> <tr> <td>element4</td> <td>value4</td> </tr> <tr> <td>element5</td> <td>value5</td> </tr> </table>
= edit =
to make elements bold don't include text (you need css span-class font-bold
):
<?xml version="1.0" encoding="utf-8" ?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output method="html" version="4.0" indent="yes"/> <xsl:template match="/"> <table> <xsl:apply-templates select="*"/> </table> </xsl:template> <xsl:template match="*"> <xsl:variable name="bold"> <xsl:value-of select="'font'"/> <xsl:if test="not(string-length(normalize-space(text())))"> <xsl:value-of select="'-bold'"/> </xsl:if> </xsl:variable> <tr> <td><span class="{$bold}"><xsl:value-of select="name()"/></span></td> <td><xsl:value-of select="normalize-space(text())"/></td> </tr> <xsl:apply-templates select="*"/> </xsl:template> </xsl:stylesheet>