2016-05-07 1 views
1

'sum()'関数としてこれを行う方法は、 'if'ステートメントで選択されたものではなく、XMLページのすべての値を追加することです。ここでXSLT IF文の後の本の価格の追加

はXMLである:ここでは

<book> 
    <title type="non-fiction">Harry Potter and the Philosophers Stone</title> 
    <author>J.K Rowling</author> 
    <publisher>Bloomsbury</publisher> 
    <year>1997</year> 
    <price>12.99</price> 
</book> 

<book> 
    <title type="fiction">The Lord of the Rings</title> 
    <author>J. R. R. Tolkien</author> 
    <publisher>George Allen and Unwin</publisher> 
    <year>1954</year> 
    <price>39.99</price> 
</book> 

<book> 
    <title type="non-fiction">The Right Stuff</title> 
    <author>Tom Wolfe</author> 
    <publisher>Farra, Staus and Giroux</publisher> 
    <year>1979</year> 
    <price>29.99</price> 
</book> 

はXSLTです:

<xsl:output 
method="html" 
indent="yes" 
version="4.0" 
doctype-public="-//W3C//DTD HTML 4.01//EN" 
doctype-system="http://www.w3.org/TR/html4/strict.dtd"/> 

<xsl:template match="/library"> 
<html> 
<body> 
    <xsl:for-each select="book"> 
     <xsl:if test="title[@type='non-fiction']"> 
      <xsl:if test="price&lt;30" > 
       <p class="title"> <xsl:value-of select="title"/></p> 
       <p class="author"> <xsl:value-of select="author"/> </p> 
       <p class="price"> <xsl:value-of select="price"/> </p> 
      </xsl:if> 
     </xsl:if> 
    </xsl:for-each>  
</body> 

私はすべての条件に該当する図書の合計を追加したいと思います。私はsum関数がこれを行うと仮定しましたが、if文を渡しても、すべての本を追加します。

+0

あなたの代わりにSUM()関数のカウント()関数を使用することはできませんか? – Nitish

+0

私はその数がリストの項目の量を合計することでしたが。私はいくつかの研究をします。 – Tauntaunwampa

答えて

3

一つの可能​​性は

  • 印刷したいすべての書籍を保持しているノードセットの変数を定義することです
  • 、ループオーバーそのノードセットブックを印刷するには、
  • とで最終的に合計関数を使用して合計価格を計算します。

など。

<body> 
    <xsl:variable name="books" select="book[title[@type='non-fiction']][price&lt;30]" /> 
    <xsl:for-each select="$books"> 
     <p class="title"> <xsl:value-of select="title"/></p> 
     <p class="author"> <xsl:value-of select="author"/> </p> 
     <p class="price"> <xsl:value-of select="price"/> </p> 
    </xsl:for-each>  
    <p class="total-price"> <xsl:value-of select="sum($books/price)"/> </p> 
</body> 
+0

お返事ありがとうございました!あなたの推理を説明したので、私はこれを手元に置いていました。 – Tauntaunwampa

3

あなたはこの試みることがあります。ここでは

<xsl:template match="/library" > 
    <html> 
    <body> 
     <xsl:variable name="fiction_lt30" 
      select="book[title[@type='non-fiction'] and price &lt; 30] " /> 
     <xsl:for-each select="$fiction_lt30"> 
     <p class="title"> <xsl:value-of select="title"/></p> 
     <p class="author"> <xsl:value-of select="author"/> </p> 
     <p class="price"> <xsl:value-of select="price"/> </p> 
     </xsl:for-each>  
     <p>total: <xsl:value-of select="sum($fiction_lt30/price)" /></p> 
    </body> 
</html> 
</xsl:template> 
1

を完了し、短いソリューションです:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<xsl:template match="/*"> 
    <xsl:variable name="vBooks" 
       select="book[title[@type='non-fiction'] and not(price >= 30)]"/> 
    <html> 
    <body> 
     <xsl:apply-templates select="$vBooks"/> 
     <p><strong>Total Cost:</strong><span><xsl:value-of 
               select="sum($vBooks/price)"/></span></p> 
    </body> 
    </html> 
    </xsl:template> 

    <xsl:template match="book/*[self::title or self::author or self::price]"> 
    <p class="{name()}"> <xsl:value-of select="."/></p> 
    </xsl:template> 
</xsl:stylesheet> 
関連する問題