我有一个xml输入,如下所示:
<food>
<fruit>Orange</fruit>
isGood
<fruit>Kiwi</fruit>
isGood
<fruit>Durian</fruit>
isBad
</food>
我想将其转换为如下所示的html语句:
橙色很好。 Kiwi 很好。 榴莲 isBad。
请注意,水果元素都是斜体。
我拥有的代码如下所示。但它有问题。
<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>
答案 0 :(得分:1)
您的XSLT似乎正在尝试重现原始输入,而不是像您想要的那样生成HTML输出。
以下是一种方法的例子......
XML输入
<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输出(原始)
<html>
<p><i>Orange</i> isGood. <i>Kiwi</i> isGood. <i>Durian</i> isBad.
</p>
</html>
HTML输出(浏览器显示)
橙色很好。 Kiwi 很好。 榴莲 isBad。