2012-03-20 10 views
2

最初の一意の2つの属性を取得しようとしています。XSLT最初の一意の値を取得する

私は最初のユニークなグループで各学生の名前を探しています。ある生徒のために最初のグループの生徒がすでにいる場合、次の一意のグループがリストされます。 XMLと期待される結果XMLを投稿しました。

この結果(バージョン1.0)を取得するには、XSLTステートメントが必要です。ありがとう。

は、ここに私のXML構造です

<Socrates> 
<Student name='Aristotle' group='1' /> 
<Student name='Aristotle' group='2' /> 
<Student name='Aristotle' group='3' /> 
<Student name='Aristotle' group='4' /> 

<Student name='Plato' group='1' /> 
<Student name='Plato' group='2' /> 
<Student name='Plato' group='3' /> 

<Student name='Xenophon' group='4' /> 
<Student name='Xenophon' group='5' /> 
<Student name='Xenophon' group='6' /> 

<Student name='Critias' group='1' /> 
<Student name='Critias' group='2' /> 
<Student name='Critias' group='3' />  
<Student name='Critias' group='4' /> 
<Student name='Critias' group='5' /> 
<Student name='Critias' group='6' /> 
    </Socrates> 

結果XML

<Socrates> 
    <Student name='Aristotle' group='1' /> 
<Student name='Plato' group='2' /> 
<Student name='Xenophon' group='4' /> 
<Student name='Critias' group='3' /> 
</Socrates> 
+0

最後の項目を '@ group =" 5 "'にしませんか? –

+0

@MadsHansen:コメントと答えをありがとう。私はあなたの答えをまだテストしていませんが、他の学生がまだ拾っていないので、最後のものはグループ3にする必要があります。私はあなたの答えをテストすることができたらあなたを更新します。ありがとう! –

+1

このような複雑な質問には、少なくともさらに多くのサンプルケースとxsltターゲットバージョンが指定されている必要があります。 –

答えて

2

別のわずかに異なるアプローチ(必ずしもより良いアプローチはないが)含まれている各学生試合にパラメータを渡すことです既に出力されている属性グループのコンマ区切り。 の学生と一致するたびに、そのグループが自分のパラメータに含まれているかどうかを確認し、生徒を出力しない場合は、次のグループを取得して現在のグループをパラメータに追加します。ここで

はあなたのサンプルXMLに適用された場合、私は試してみて

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output indent="yes"/> 

    <xsl:template match="Socrates"> 
     <Socrates> 
     <xsl:apply-templates select="Student[1]"/> 
     </Socrates> 
    </xsl:template> 

    <xsl:template match="Student"> 
     <!-- Parameter containin comma-delimited list of currently output groups --> 
     <xsl:param name="groupList" select="','" /> 
     <xsl:choose> 
     <!-- Has the group already been output? --> 
     <xsl:when test="contains($groupList, concat(',', @group, ','))"> 
      <!-- If so, move on to next student record --> 
      <xsl:apply-templates select="following-sibling::Student[1]"> 
       <xsl:with-param name="groupList" select="$groupList" /> 
      </xsl:apply-templates> 
     </xsl:when> 
     <!-- Group has not already been output --> 
     <xsl:otherwise> 
      <!-- Output the record --> 
      <xsl:copy-of select="." /> 

      <!-- Get the next student with a different name --> 
      <xsl:apply-templates select="following-sibling::Student[@name!=current()/@name][1]"> 
       <xsl:with-param name="groupList" select="concat($groupList, @group, ',')" /> 
      </xsl:apply-templates> 
     </xsl:otherwise> 
     </xsl:choose> 
    </xsl:template> 
</xsl:stylesheet> 

より良いものを説明するためにコメントしているXSLT、ある、次はこれを前提としないことを出力

<Socrates> 
    <Student name="Aristotle" group="1" /> 
    <Student name="Plato" group="2" /> 
    <Student name="Xenophon" group="4" /> 
    <Student name="Critias" group="3" /> 
</Socrates> 

注意です受講者の要素は、常に入力XMLの名前順に並べられます。

+0

ありがとう!!これは確かに正しいように見える。私はまだそれをテストすることができませんでした。それはXSLT 1.0で動作しますか? –

+0

これは確かにXSLT 1.0ソリューションです。 –

関連する問題