xml - How to output text node value line by line using xslt 1.0? -
i have xml file like:
<messages> error message 1 error message 2 </messages>
i have output as:
error 1: error message 1 error 2: error message 2
i'm using xslt 1.0, , tried:
<?xml version="1.0" encoding="iso-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output method="html" omit-xml-declaration="yes"/> <xsl:template match="messages"> <h3>error 1:</h3> <xsl:value-of select="substring-before(./text(), ' ')"/> <h3>error 2:</h3> <xsl:value-of select="substring-after(./text(), ' ')"/> </xsl:template> </xsl:stylesheet>
but returned me nothing...could me this? thanks!
you can use recursive template achieve this:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output method="html" omit-xml-declaration="yes"/> <xsl:template match="messages" name="outputerror"> <xsl:param name="index" select="1"/> <xsl:param name="source" select="text()"/> <xsl:variable name="thisline" select="normalize-space(substring-before($source, ' '))"/> <xsl:variable name="therest" select="substring-after($source, ' ')"/> <xsl:choose> <xsl:when test="$thisline"> <h3>error <xsl:value-of select="$index"/>:</h3> <span><xsl:value-of select="$thisline"/></span> <xsl:call-template name="outputerror"> <xsl:with-param name="index" select="$index + 1"/> <xsl:with-param name="source" select="$therest"/> </xsl:call-template> </xsl:when> <xsl:when test="$therest"> <xsl:call-template name="outputerror"> <xsl:with-param name="index" select="$index"/> <xsl:with-param name="source" select="$therest"/> </xsl:call-template> </xsl:when> </xsl:choose> </xsl:template> </xsl:stylesheet>
i wrapped actual errors in <span>
element separate them whitespace, can of course <p>
, <div>
or no element @ if prefer.
Comments
Post a Comment