2017-07-07 9 views
1

特定の文字列レコードを最初に表示する必要があります。たとえば、ここではすべてのRehmanレコードを最初に表示し、次に他のレコードは特に指定しません。最初に特定のレコードを表示する方法xsl

XML

<?xml version="1.0" encoding="UTF-8"?> 
<catalog> 
    <cd> 
    <title>Empire Burlesque</title> 
    <artist>Bob Dylan</artist> 
    <country>USA</country> 
    <company>Columbia</company> 
    <price>10.90</price> 
    <year>1985</year> 
    </cd> 
    <cd> 
    <title>Hide your heart</title> 
    <artist>Bonnie Tyler</artist> 
    <country>UK</country> 
    <company>CBS Records</company> 
    <price>9.90</price> 
    <year>1988</year> 
    </cd> 
    <cd> 
    <title>Roja</title> 
    <artist>Rehman</artist> 
    <country>USA</country> 
    <company>RCA</company> 
    <price>9.90</price> 
    <year>1982</year> 
    </cd> 
    <cd> 
    <title>Still got the blues</title> 
    <artist>Gary Moore</artist> 
    <country>UK</country> 
    <company>Virgin records</company> 
    <price>10.20</price> 
    <year>1990</year> 
    </cd> 
    <cd> 
    <title>Eros</title> 
    <artist>Zack</artist> 
    <country>EU</country> 
    <company>BMG</company> 
    <price>9.90</price> 
    <year>1997</year> 
    </cd> 
    <cd> 
    <title>Rockstar</title> 
    <artist>Rehman</artist> 
    <country>UK</country> 
    <company>Polydor</company> 
    <price>10.90</price> 
    <year>1998</year> 
    </cd> 
    <cd> 
    <title>Sylvias Mother</title> 
    <artist>Dr.Hook</artist> 
    <country>UK</country> 
    <company>CBS</company> 
    <price>8.10</price> 
    <year>1973</year> 
    </cd> 

</catalog> 

XSL

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

<xsl:template match="/"> 
    <html> 
    <body> 
    <h2>My CD Collection</h2> 
    <table border="1"> 
     <tr bgcolor="#9acd32"> 
     <th>Title</th> 
     <th>Artist</th> 
     </tr> 
     <xsl:for-each select="catalog/cd"> 
     <xsl:sort select="artist" order ="descending"/> 
     <tr> 
     <td><xsl:value-of select="title"/></td> 
     <td><xsl:value-of select="artist"/></td> 
     </tr> 
     </xsl:for-each> 
    </table> 
    </body> 
    </html> 
</xsl:template> 

</xsl:stylesheet> 

結果

私のCDコレクション

タイトルアーティスト

Empire Burlesque Bob Dylan 
Hide your heart Bonnie Tyler 
Sylvias Mother Dr.Hook 
Still got the blues Gary Moore 
Roja Rehman 
Rockstar Rehman 
Eros Zack 

答えて

0

あなたはすでにあなたが必要とする作品のほとんどを持っているように見えるこの

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

    <xsl:template match="/"> 
     <html> 
      <body> 
       <h2>My CD Collection</h2> 
       <table border="1"> 
        <tr bgcolor="#9acd32"> 
         <th>Title</th> 
         <th>Artist</th> 
        </tr> 
        <xsl:for-each select="catalog/cd[artist='Rehman']"> 
         <xsl:sort select="artist" order ="descending"/> 
         <tr> 
          <td><xsl:value-of select="title"/></td> 
          <td><xsl:value-of select="artist"/></td> 
         </tr> 
        </xsl:for-each> 
        <xsl:for-each select="catalog/cd[artist!='Rehman']"> 
         <!-- You can use this for sorting of rest --> 
         <!-- <xsl:sort select="artist" order ="descending"/> --> 
         <tr> 
          <td><xsl:value-of select="title"/></td> 
          <td><xsl:value-of select="artist"/></td> 
         </tr> 
        </xsl:for-each> 

       </table> 
      </body> 
     </html> 
    </xsl:template> 

</xsl:stylesheet> 
+0

ここで、私たちは一般的に、私たちのコードに付随する説明的解説が好きです。結局のところ、実際にはその解答に実際に答えるコメントであるからです。添付の - 非常に歓迎されたコード*は明らかに*します。 –

0

を使用することができます。それは動作するようにそれらを組み立てることの単なる問題です。特に、よく選択されたxsl:sortキーを使用してこれを行うことができます。

あなたはアーティストレーマンとCDは他のすべての前に出力され、それでは、現在のノードとして<cd>要素で評価されたときにその状態を表現するXPath式で始めるようにしたい:

artist = 'Rehman' 

ソートkeyは任意のXPath式です。その値は文字列に変換されます(そしておそらく後で数字として再解釈されます)。上記の式はブール値に評価され、'true'または'false'のいずれかになる文字列に変換されたときに評価されます。 'true'以来辞書順'false'の後に来る、とあなたは式が真であるCDは第一の出力になりたい、あなたはそのキーの降順指定したいと思う:あなたはさらに順位を洗練したい場合

 <xsl:for-each select="catalog/cd"> 
     <xsl:sort select="artist = 'Rehman'" order="descending"/> 
     <tr> 
     <td><xsl:value-of select="title"/></td> 
     <td><xsl:value-of select="artist"/></td> 
     </tr> 
     </xsl:for-each> 

、例えば残りのレコードをアーティスト別にアルファベット順に表示するには、任意の数の追加のソートキーを追加できます。同じスコープに適用される複数のキーは、表示される順序に従って最大から最小に帰属されます。

関連する問題