使用xsl:when

时间:2011-04-05 09:51:05

标签: xslt

我已经使用了xsl:何时显示我的详细信息。我需要显示的细节是书的标题。现在我想通过一次更改来显示标题。如果标题是'Cpp .....',我需要用'C ++ ....'替换它,并在浏览器中显示带有此标题的书。 这适用于所有在其标题中都有Cpp的书籍。

我编辑了我的问题。 你能帮我解决这个问题吗?

   <?xml version="1.0"?>
   <xsl:stylesheet version="1.0" exclude-result-prefixes="rdf rss"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"      

     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:rss="http://my.netscape.com/rdf/simple/0.9/"    

     xmlns:kb="http://protege.stanford.edu/kb#">

<xsl:output method="html" indent="yes" />
<xsl:template match="wt:sparql/wt:results/wt:result"
    xmlns:wt="http://www.w3.org/2005/sparql-results#">
    <script type="text/javascript"></script>
    <h3>
    <xsl:choose>
    <xsl:when test="wt:binding[@name='count']/wt:literal &lt;= 5">
    <a style="color:red">
    <xsl:attribute name="onclick">
prepareEditValue('<xsl:value-of select="wt:binding[@name='x']/wt:uri"></xsl:value-of>', false);
    </xsl:attribute>
    <xsl:value-of select="wt:binding[@name='title']/wt:literal" />
    </a>
    </xsl:when>
    <xsl:when test="wt:binding[@name='count']/wt:literal &lt;= 50">
    <a style="color:blue">
    <xsl:attribute name="onclick">
    prepareEditValue('<xsl:value-of select="wt:binding[@name='x']/wt:uri"></xsl:value-of>', false);
    </xsl:attribute>
    <xsl:value-of select="wt:binding[@name='title']/wt:literal" />
    </a>
    </xsl:when>
    <xsl:otherwise>
    <a>
    <xsl:attribute name="onclick">
    prepareEditValue('<xsl:value-of select="wt:binding[@name='x']/wt:uri"></xsl:value-of>', false);
    </xsl:attribute>
    <xsl:value-of select="wt:binding[@name='title']/wt:literal" />
    </a>
    </xsl:otherwise>
    </xsl:choose>
    </h3>
    <div>
    <xsl:value-of select="wt:binding[@name='description']/wt:literal" />            Price:
    <xsl:value-of select="wt:binding[@name='price']/wt:literal" />
    <xsl:value-of select="wt:binding[@name='currency']/wt:literal" />
    <div style="float:right;">
    <input type="button" value="Edit">
    <xsl:attribute name="onclick">
    prepareEditValue('<xsl:value-of select="wt:binding[@name='x']/wt:uri"></xsl:value-of>', true);
    </xsl:attribute>
    </input>
    <input type="button" value="Delete">
    <xsl:attribute name="onclick">
    deleteBookDialog('<xsl:value-of select="wt:binding[@name='x']/wt:uri"></xsl:value-of>');
    </xsl:attribute>
    </input>
    </div>
    </div>
</xsl:template>

</xsl:stylesheet>

包含数据的rdf文件是

    欢迎来到C.     YY     $     8     8   

    Cpp Primer Plus     斯蒂芬普拉塔     â,¬     36.94     88   

<rdf:Description rdf:about="http://protege.stanford.edu/kb#Linux Programming Bible">
<kb:Title>Linux Programming Bible</kb:Title>
<kb:Author>John Goerzen</kb:Author>
<kb:Currency>$</kb:Currency>
<kb:Price>77</kb:Price>
<kb:Count>34</kb:Count>
</rdf:Description>

<rdf:Description rdf:about="http://protege.stanford.edu/kb#CSharp-The Basics">
<kb:Title>CSharp-The Basics</kb:Title>
<kb:Author>Vijay Mukhi</kb:Author>
<kb:Currency>€</kb:Currency>
<kb:Price>56</kb:Price>
<kb:Count>6</kb:Count>
</rdf:Description>

<rdf:Description rdf:about="http://protege.stanford.edu/kb#Cpp Primer Answer Book">
<kb:Title>Cpp Primer Answer Book</kb:Title>
<kb:Author>Clovis L.Tondo</kb:Author>
<kb:Currency>$</kb:Currency>
<kb:Price>24.40</kb:Price>
<kb:Count>100</kb:Count>
</rdf:Description>

<rdf:Description rdf:about="http://protege.stanford.edu/kb#Ajax for Dummies">
<kb:Title>Ajax for Dummies</kb:Title>
<kb:Author>Steve Holzner</kb:Author>
<kb:Currency>$</kb:Currency>
<kb:Price>21.89</kb:Price>
<kb:Count>3</kb:Count>
</rdf:Description>

<rdf:Description rdf:about="http://protege.stanford.edu/kb#Beginning Programming with 
Cpp For Dummies">
<kb:Title>Beginning Programming with Cpp For Dummies</kb:Title>
<kb:Author>Stephen R.Davis</kb:Author>
<kb:Currency>$</kb:Currency>
<kb:Price>19.69</kb:Price>
<kb:Count>99</kb:Count>
</rdf:Description>

<rdf:Description rdf:about="http://protege.stanford.edu/kb#Programming in ANSI C">
<kb:Title>Programming in ANSI C</kb:Title>
<kb:Author>Stephen G.Kochan</kb:Author>
<kb:Currency>$</kb:Currency>
<kb:Price>99</kb:Price>
<kb:Count>9</kb:Count>
</rdf:Description>

<rdf:Description rdf:about="http://protege.stanford.edu/kb#Introduction to Programming 
 Using Java">
<kb:Title>Introduction to Programming Using Java</kb:Title>
<kb:Author>David J.Eck</kb:Author>
<kb:Currency>$</kb:Currency>
<kb:Price>89</kb:Price>
<kb:Count>23</kb:Count>
</rdf:Description>

<rdf:Description rdf:about="http://protege.stanford.edu/kb#Crafting a Compiler with C">
<kb:Title>Crafting a Compiler with C</kb:Title>
<kb:Author>Charles N.Fischer</kb:Author>
<kb:Currency>$</kb:Currency>
<kb:Price>95</kb:Price>
<kb:Count>8</kb:Count>
</rdf:Description>

<rdf:Description rdf:about="http://protege.stanford.edu/kb#Sams Teach Yourself C Sharp 
 in 24 Hours">
<kb:Title>Sams Teach Yourself C Sharp in 24 Hours</kb:Title>
<kb:Author>Sams</kb:Author>
<kb:Currency>$</kb:Currency>
<kb:Price>89</kb:Price>
<kb:Count>9</kb:Count>
</rdf:Description>

<rdf:Description rdf:about="http://protege.stanford.edu/kb#Compiler Constructions">
<kb:Title>Compiler Constructions</kb:Title>
<kb:Author>Jeremy Perk</kb:Author>
<kb:Currency>$</kb:Currency>
<kb:Price>88</kb:Price>
<kb:Count>9</kb:Count>
</rdf:Description>

<rdf:Description rdf:about="http://protege.stanford.edu/kb#Real Time Concepts for   
Embedded Systems">
<kb:Title>Real Time Concepts for Embedded Systems</kb:Title>
<kb:Author>Qing Li</kb:Author>
<kb:Currency>€</kb:Currency>
<kb:Price>109</kb:Price>
<kb:Count>88</kb:Count>
</rdf:Description>

<rdf:Description rdf:about="http://protege.stanford.edu/kb#Introduction to Algorithms">
<kb:Title>Introduction to Algorithms</kb:Title>
<kb:Author>Thomas Cormen</kb:Author>
<kb:Currency>$</kb:Currency>
<kb:Price>33</kb:Price>
<kb:Count>6</kb:Count>
 </rdf:Description>

 <rdf:Description rdf:about="http://protege.stanford.edu/kb#Begineer to Cpp  ">
<kb:Count>7</kb:Count>
<kb:Price>77</kb:Price>
<kb:Currency>$</kb:Currency>
<kb:Author>Harry</kb:Author>
<kb:Title>Begineer to Cpp  </kb:Title>
</rdf:Description>
</rdf:RDF>

阿奇纳。

1 个答案:

答案 0 :(得分:0)

你可以尝试这样的事情......

<xsl:if test="@title">
  <xsl:variable name="CPP">Cpp</xsl:variable>
  <xsl:when test="contains(@Title, $CPP)">
    <xsl:value-of select="replace(@title,'.cpp','')"/>
    <xsl:text>C++</xsl:text>
  </xsl:when>
  <xsl:othersise>
     <xsl:value-of select="@title"/>
  </xsl:othersise>
</xsl:if>

它检查标题属性。如果存在,则将其与变量CPP的值进行比较。如果匹配则打印我们的“C ++”。