2016-12-21 11 views
0

タイトル、カテゴリ、リーグを持つこのフットボールゲームのXMLドキュメントを検討してください。MuenchianグループXSLT 1.0の要素の選択

<?xml version='1.0'?> 
<games> 
    <game> 
     <title>Manchester City - Arsenal</title> 
     <category>Live</category> 
     <league>Premier League</league> 
    </game> 
    <game> 
     <title>Barcelona - Real Madrid</title> 
     <category>Live</category> 
     <league>Primera Division</league> 
    </game> 
    <game> 
     <title>Arsenal - Hull City</title> 
     <category>Recap</category> 
     <league>Premier League</league> 
    </game> 
    <game> 
     <title>Everton - Liverpool</title> 
     <category>Live</category> 
     <league>Premier League</league> 
    </game> 
    <game> 
     <title>Zaragoza - Deportivo</title> 
     <category>Short Recap</category> 
     <league>Primera Division</league> 
    </game> 
</games> 

は私がカテゴリによってグループにこれらのゲームをしようとしているが、私は唯一の<league>要素は「プレミアリーグ」であるため、レコードを保持したいです。カテゴリには、対応するレコードの数をリストするcount属性も必要です。

次のXSLファイルは機能しますが、「プレミアリーグ」のゲームが見つからないカテゴリも表示されるという欠点があります。理想的には、それらのためのカテゴリタグはありません。

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

<xsl:key name="prod-cat" match="game" use="category"/> 

    <xsl:template match="/"> 
     <root> 
      <xsl:for-each select="games/game[count(. | key('prod-cat', category)[1]) = 1]"> 

       <category> 

       <xsl:variable name="cat"> 
        <xsl:value-of select="category"/> 
       </xsl:variable> 

       <xsl:variable name="count"> 
        <xsl:value-of select="count(//game[category = $cat][league = 'Premier League'])"/> 
       </xsl:variable> 

       <xsl:attribute name="name"> 
        <xsl:value-of select="category"/> 
       </xsl:attribute> 

       <xsl:attribute name="count"> 
        <xsl:value-of select="$count"/> 
       </xsl:attribute> 

        <xsl:for-each select="key('prod-cat', $cat)[league = 'Premier League']"> 
         <game> 
          <title> 
           <xsl:value-of select="title"/> 
          </title> 
          <cat> 
           <xsl:value-of select="category"/> 
          </cat> 
          <series> 
           <xsl:value-of select="league"/> 
          </series> 
         </game> 
        </xsl:for-each> 
       </category> 
      </xsl:for-each> 
     </root> 
    </xsl:template> 
</xsl:transform> 

結果:ここで

<?xml version="1.0" encoding="UTF-8"?> 
<root> 
    <category name="Live" count="2"> 
     <game> 
     <title>Manchester City - Arsenal</title> 
     <cat>Live</cat> 
     <series>Premier League</series> 
     </game> 
     <game> 
     <title>Everton - Liverpool</title> 
     <cat>Live</cat> 
     <series>Premier League</series> 
     </game> 
    </category> 
    <category name="Recap" count="1"> 
     <game> 
     <title>Arsenal - Hull City</title> 
     <cat>Recap</cat> 
     <series>Premier League</series> 
     </game> 
    </category> 
    <category name="Short Recap" count="0"/> <!--This one needs to go--> 
</root> 

答えて

1

私はキー定義に条件を置く:http://xsltransform.net/ejivdHq/1

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
    <xsl:output method="xml" encoding="UTF-8" indent="yes" /> 
    <xsl:key name="prod-cat" match="game[league = 'Premier League']" use="category"/> 
    <xsl:template match="/"> 
     <root> 
      <xsl:for-each select="games/game[league = 'Premier League'][count(. | key('prod-cat', category)[1]) = 1]"> 
       <category name="{category}" count="{count(key('prod-cat', category))}"> 
        <xsl:for-each select="key('prod-cat', category)"> 
         <game> 
          <title> 
           <xsl:value-of select="title"/> 
          </title> 
          <cat> 
           <xsl:value-of select="category"/> 
          </cat> 
          <series> 
           <xsl:value-of select="league"/> 
          </series> 
         </game> 
        </xsl:for-each> 
       </category> 
      </xsl:for-each> 
     </root> 
    </xsl:template> 
</xsl:transform> 

オンライン。

0

は、最適化され、修正されたバージョンです。必要な要素のみをグループ化し、残りは除外することができます。
EDIT: Martin Honnenに感謝します。代わりに出力に無関係な要素を取り除くためにxsl:ifを追加しました。

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
    <xsl:output method="xml" encoding="UTF-8" indent="yes" /> 
    <xsl:key name="prod-cat" match="game" use="category"/> 
    <xsl:template match="/"> 
     <root> 
      <xsl:for-each select="games/game[count(. | key('prod-cat', category)[1]) = 1]"> 
       <xsl:if test="key('prod-cat', category)[league = 'Premier League']"> 
        <category name="{category}" count="{count(key('prod-cat', category)[league = 'Premier League'])}"> 
         <xsl:for-each select="key('prod-cat', category)[league = 'Premier League']"> 
          <game> 
           <title> 
            <xsl:value-of select="title"/> 
           </title> 
           <cat> 
            <xsl:value-of select="category"/> 
           </cat> 
           <series> 
            <xsl:value-of select="league"/> 
           </series> 
          </game> 
         </xsl:for-each> 
        </category> 
       </xsl:if> 
      </xsl:for-each> 
     </root> 
    </xsl:template> 
</xsl:transform> 
+1

私はあなたの現在のアプローチが要素を欠いているかもしれないので、 'league = 'Premier League''をキー定義の' match'属性に入れる必要があると思います。例えばhttp://xsltransform.net/ejivdHqを見てください私は入力要素の順序を変更しただけで、あなたのキーによって確立されたグループの最初の項目が別のリーグにあるので、あなたのコードは 'live'カテゴリをもう見つけません。 –

関連する問題