I'm new to XSLT, and I'm trying to write XSLT that will smooth out any given XML, so a new line appears whenever the nesting level changes. My input can be any XML document with any number of nested levels so that the structure is not known by XSLT. Thanks to the tools I have, my solution should use XSLT version 1.0.
For instance.
<?xml version="1.0"?> <ROWSET> <ROW> <CUSTOMER_ID>0</CUSTOMER_ID> <NAME>Default Company</NAME> <BONUSES> <BONUSES_ROW> <BONUS_ID>21</BONUS_ID> <DESCRIPTION>Performance Bonus</DESCRIPTION> </BONUSES_ROW> <BONUSES_ROW> <BONUS_ID>26</BONUS_ID> <DESCRIPTION>Special Bonus</DESCRIPTION> </BONUSES_ROW> </BONUSES> </ROW> <ROW> <CUSTOMER_ID>1</CUSTOMER_ID> <NAME>Dealer 1</NAME> <BONUSES> <BONUSES_ROW> <BONUS_ID>27</BONUS_ID> <DESCRIPTION>June Bonus</DESCRIPTION> <BONUS_VALUES> <BONUS_VALUES_ROW> <VALUE>10</VALUE> <PERCENT>N</PERCENT> </BONUS_VALUES_ROW> <BONUS_VALUES_ROW> <VALUE>11</VALUE> <PERCENT>Y</PERCENT> </BONUS_VALUES_ROW> </BONUS_VALUES> </BONUSES_ROW> </BONUSES> </ROW> </ROWSET>
should become ....
0, Default Company 21, Performance Bonus 26, Special Bonus 1, Dealer 1 27, June Bonus 10, N 11, Y
XSLT, which I have written so far, ...
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text" encoding="iso-8859-1"/> <xsl:strip-space elements="*" /> <xsl:template match="/*/child::*"> <xsl:apply-templates select="*"/> </xsl:template> <xsl:template match="*"> <xsl:value-of select="text()" /> <xsl:if test="position()!= last()"><xsl:text>,</xsl:text></xsl:if> <xsl:if test="position()= last()"><xsl:text>
</xsl:text></xsl:if> <xsl:apply-templates select="./child::*"/> </xsl:template> </xsl:stylesheet>
but my result is simply incorrect, with spaces and unnecessary data.
0,Default Company, ,21,Performance Bonus 26,Special Bonus 1,Dealer 1, 27,June Bonus, ,10,N 11,Y
There seems to be a check to see if node text can contain text, but I was stuck and could do it using XSLT help.