2012-10-31 55 views
0

我有一个XML输入象下面这样:XSLT信息采集

<food> 
    <fruit>Orange</fruit> 
    isGood 
    <fruit>Kiwi</fruit> 
    isGood 
    <fruit>Durian</fruit> 
    isBad 
</food> 

我想将它转化为一个HTML语句象下面这样:

isGood。 奇异果 isGood。 榴莲 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

您输入XML不好看在文本之间似乎不属于水果节点 –

+0

喜瑜珈,yes文本isGood和isBad是不是里面的水果node.it是食品节点内。 – setiasetia

回答

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输出(RAW)

为例
<html> 
    <p><i>Orange</i> isGood. <i>Kiwi</i> isGood. <i>Durian</i> isBad. 
    </p> 
</html> 

HTML输出(浏览器显示)

isGood。 猕猴桃 isGood。 榴莲 isBad。