2017-04-21 17 views
0

私はスコアボードの種類があり、勝利チームを計算したいと思います。しかし、残念ながら私はid値の出現回数を数えることはできません。 [私は、結果の目標を比較することによって、ゲームの勝者を設定し、チームを選択し選択要素内のXSLカウント()

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 

<xsl:template match="wm/game/result"> 
<xsl:variable name="tore1" select="substring-before(.,':')"/> 
<xsl:variable name="tore2" select="substring-after(.,':')"/> 

<xsl:choose> 
    <xsl:when test="$tore1 &gt; $tore2"> 
     <xsl:value-of select="../team[1]/@id"/><br /> 
    </xsl:when> 
    <xsl:otherwise> 
     <xsl:value-of select="../team[2]/@id"/><br /> 
    </xsl:otherwise> 
</xsl:choose> 

1:

<?xml version="1.0" encoding="UTF-8"?> 
<?xml-stylesheet type="text/xsl" href="winner.xsl" ?> 
<wm> 
    <game> 
     <team id='de' /> 
     <team id='fr' /> 
     <result>4:2</result> 
    </game> 
    <game> 
     <team id='us' /> 
     <team id='de' /> 
     <result>4:2</result> 
    </game> 
    <game> 
     <team id='de' /> 
     <team id='fr' /> 
     <result>3:2</result> 
    </game> 
    <game> 
     <team id='de' /> 
     <team id='fr' /> 
     <result>1:3</result> 
    </game> 
</wm> 

とXSL:ここでは例のXML文書があります]またはチーム[2]。出力は次のとおりです。

しかし、今では国の外見を数えてしまいました。同じ出力をした選択ステートメントの外に

1 
1 
1 
1 

また

<xsl:value-of select="count(../team[@id = 'de'])"/> 

:私は

<xsl:value-of select="count(../team[1]/@id)" /> 

のようなXPath式に()のカウントを設定した場合には、その結果。以下の出力をしたいと思います。

2 de 
1 us 
1 fr 

ありがとうございます。

+1

これは、問題が発生する前にグループ化の問題です。あなたは参加チームのリストを持っていますか?または、入力に記載されているチームがあればグループ化する必要がありますか? –

答えて

1

知られているチームの勝ち数を数えるのは簡単です。次の例を考えてみましょう:

<xsl:template match="/wm"> 
    <xsl:variable name="id" select="'de'"/> 
    <xsl:variable name="home-wins" select="count(game[team[1]/@id=$id][substring-before(result, ':') > substring-after(result, ':')])"/> 
    <xsl:variable name="away-wins" select="count(game[team[2]/@id=$id][substring-after(result, ':') > substring-before(result, ':')])"/> 
    <xsl:value-of select="$home-wins + $away-wins"/> 
    <xsl:text> </xsl:text> 
    <xsl:value-of select="$id"/> 
</xsl:template> 

結果

2 de 
+0

それをMeunchianグループと組み合わせて入力文書に表示されているチームをすべて特定し、それぞれの勝利数を出力するのはそれほど難しいことではありません。 –

0

感謝。 @ michael.hor257kの答えはうまくいきます。私はまた、Meunchianグループを適用しようとしました。私はそれを得たと思う。ご協力いただきありがとうございます。

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 


<xsl:key name="teamsByID" match="game/team" use="@id"/> 

<xsl:template match="wm"> 

    <xsl:for-each select="game/team[count(. | key('teamsByID', @id)[1]) = 1]"> 
    <xsl:variable name="id" select="@id"/> 
    <xsl:variable name="tore1" select="count(../../game[team[1]/@id=$id][substring-before(result,':') > substring-after(result, ':')])" /> 
    <xsl:variable name="tore2" select="count(../../game[team[2]/@id=$id][substring-after(result,':') > substring-before(result, ':')])" /> 
    <xsl:value-of select="$tore1 + $tore2"/> - <xsl:value-of select="@id"/><br /> 
    </xsl:for-each> 

</xsl:template>