Reputation: 113
i have a xml input like below:
<food>
<fruit>Orange</fruit>
isGood
<fruit>Kiwi</fruit>
isGood
<fruit>Durian</fruit>
isBad
</food>
i want to transform it to a html statement like below:
Orange isGood. Kiwi isGood. Durian isBad.
please note that the fruits element are all in italic.
the code that i have is like below.but its having problem.
<xsl:template match="/" >
<food>
<xsl:apply-templates select="food"/>
</food>
</xsl:template>
<xsl:template match="food">
<xsl:element name="fruit">
<xsl:value-of select="fruit" />
</xsl:element>
</xsl:template>
Upvotes: 0
Views: 29
Reputation: 52878
It seems like your XSLT is trying to reproduce the original input instead of producing HTML output like you want.
Here's an example of one way to do it...
XML Input
<food>
<fruit>Orange</fruit>
isGood
<fruit>Kiwi</fruit>
isGood
<fruit>Durian</fruit>
isBad
</food>
XSLT 1.0
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" method="html"/>
<xsl:strip-space elements="*"/>
<xsl:template match="food">
<html>
<p><xsl:apply-templates/></p>
</html>
</xsl:template>
<xsl:template match="fruit">
<i><xsl:value-of select="."/></i>
<xsl:value-of select="concat(' ',normalize-space(following-sibling::text()),'. ')"/>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
HTML Output (raw)
<html>
<p><i>Orange</i> isGood. <i>Kiwi</i> isGood. <i>Durian</i> isBad.
</p>
</html>
HTML Output (browser display)
Orange isGood. Kiwi isGood. Durian isBad.
Upvotes: 1