2013-10-27 40 views
0

我正在努力做一個有條件的櫃檯。我的XML是:依情況而計XSLT

<comments> 
    <comment> 
     <name>Jonh</name> 
     <num>4/7</num> 
    </comment> 
    <comment> 
     <name>Mary</name> 
     <num>2/9</num> 
    </comment> 
    <comment> 
     <name>Catie</name> 
     <num>12/2</num> 
    </comment> 
    <comment> 
     <name>Stefen</name> 
     <num>127/300</num> 
    </comment> 
</comments> 

標籤的結構如下:

number1/number2 

而且我想知道的是數字1的頻率比在所有標籤的NUMBER2更大

我試着與計數:

count(tokenize(//comment/num, '/')[1] &gt; tokenize(//comment/num, '/')[2]) 

但沒有結果。我想過使用變量作爲計數器,但它們是不可變的。我該如何解決這個問題?

回答

1

可以使用substring-beforesubstring-after計算這些值:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
    <xsl:template match="/"> 
     number1 is greater than the number2 
     <xsl:value-of select="count(//num[substring-before(.,'/') &gt; substring-after(.,'/')])" /> times. 
    </xsl:template> 
</xsl:stylesheet> 
0

你就要成功了,因爲它是,關鍵是你需要的GT檢查移至謂語

count(//comment/num[tokenize(., '/')[1] &gt; tokenize(., '/')[2]]) 

選擇了num元素,你有興趣再算上那些。

+0

坎我嘗試使用可變這樣的: 「的」但不返回結果! –

+0

@JohnSoart你確定你的處理器支持XSLT 2.0而不僅僅是1.0嗎? 'tokenize'函數在1.0中不可用,但如其他答案中的建議,您可以使用'substring-before(。,'/')'和'substring-after(。,'/')'而不是'tokenize (...)[1]'和[2]'。 –