2017-08-31 23 views
0

我有下面的XML:XSLT:带有属性的文本显示仅

<text> 
    <sentence type="grocery">I bought <fruit> apples</fruit> at the grocery store.</sentence> 
    <sentence type="grocery">I also bought <fruit> bananas</fruit> at the store.</sentence> 
    <sentence>Then, I bought a basket at another store.</sentence> 
</text> 

我现在的XSLT看起来像这样(非常感谢michael.hor257k):

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

    <xsl:template match="/text"> 
     <html> 
      <body> 
       <xsl:apply-templates select="sentence"/> 
      </body> 
     </html> 
    </xsl:template> 

    <xsl:template match="sentence"> 
     <p> 
      <xsl:apply-templates/> 
     </p> 
    </xsl:template> 

    <xsl:template match="fruit"> 
     <span style="color:red;"> 
      <xsl:apply-templates/> 
     </span> 
    </xsl:template> 

</xsl:stylesheet> 

,但我需要输出如下面的示例(仅用于特定属性<sentence type="grocery">):

<html> 
    <body> 
     <p>I bought <span style="color:red;"> apples</span> at the grocery store.</p> 
     <p>I also bought <span style="color:red;"> bananas</span> at the story.</p> 
    </body> 
</html> 

请ħ elp与XSLT。谢谢。

+2

将''改为''然后[获得一个好的XSLT/XPath book](http://a.co/b9bi5Wd)来学习基础知识。 –

回答

0

,您应该使用的

<xsl:apply-templates select="sentence[@type='grocery']"/> 

代替

<xsl:apply-templates select="sentence"/> 

它消除了不具有属性值“杂货”“类型”属性的所有句子。