2016-09-06 7 views
0

各学生のデータが与えられたStudents.xmlをコースごとに構成されたCourses.xmlに変換する必要があります。 1つのコースに複数のコースがある場合は、1つのコースに複数のコースがある場合、結果はすべて間違っています。私はすべてのコース名を配列で取得してから、xslファイル内のこれらの値に基づいてループする方法がありますか?XSLTを使用してXMLを別のXMLに再編成する

Students.xml

<?xml version="1.0" encoding="UTF-8"?> 
<Students> 
    <Student> 
    <name>A</name> 
    <rollNo>1</rollNo> 
    <course>Music</course> 
    <course>Computers</course> 
</Student> 
<Student> 
    <name>B</name> 
    <rollNo>2</rollNo> 
    <course>Sports</course> 
</Student> 
<Student> 
    <name>C</name> 
    <rollNo>3</rollNo> 
    <course>Sports</course> 
      <course>Music</course> 
</Student> 
<Student> 
    <name>D</name> 
    <rollNo>4</rollNo> 
    <course>GK</course> 
</Student> 
<Student> 
    <name>E</name> 
    <rollNo>5</rollNo> 
    <course>Computers</course> 
      <course>Maths</course> 
</Student> 
<Student> 
    <name>F</name> 
    <rollNo>6</rollNo> 
    <course>Physics</course> 
</Student> 
<Student> 
    <name>G</name> 
    <rollNo>7</rollNo> 
    <course>Drama</course> 
</Student> 
<Student> 
    <name>H</name> 
    <rollNo>8</rollNo> 
    <course>Communication</course> 
      <course>Computers</course> 
</Student> 
<Student> 
    <name>I</name> 
    <rollNo>9</rollNo> 
    <course>Arts</course> 
</Student> 
<Student> 
    <name>J</name> 
    <rollNo>10</rollNo> 
    <course>Computers</course> 
</Student> 
</Students> 

Transform.xsl

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="xml" indent="yes"/> 
<xsl:key name="students-by-course" match="Student" use="course" /> 
<xsl:template match="Students"> 
<Courses> 
<xsl:for-each select = "Student[count(. | key('students-by-course', course)[1]) = 1]"> 
<xsl:sort select="course" /> 
<course> 
<name> 
    <xsl:value-of select="course"/> 
</name> 
    <xsl:for-each select="key('students-by-course', course)"> 
<Student> 
<name> 
    <xsl:value-of select="name"/> 
</name> 
<rollNo> 
    <xsl:value-of select="rollNo"/> 
</rollNo> 
</Student> 
</xsl:for-each> 
</course> 
</xsl:for-each> 
</Courses> 
</xsl:template> 
</xsl:stylesheet> 

1は結果がすべて間違っている見ることができるようにこれは私に次のような出力

<?xml version="1.0" encoding="UTF-8"?> 
<Courses> 
<course> 
    <name>Arts</name> 
    <Student> 
     <name>I</name> 
     <rollNo>9</rollNo> 
    </Student> 
</course> 
<course> 
    <name>Drama</name> 
    <Student> 
     <name>G</name> 
     <rollNo>7</rollNo> 
    </Student> 
</course> 
<course> 
    <name>GK</name> 
    <Student> 
     <name>D</name> 
     <rollNo>4</rollNo> 
    </Student> 
</course> 
<course> 
    <name>Music</name> 
    <Student> 
     <name>A</name> 
     <rollNo>1</rollNo> 
    </Student> 
    <Student> 
     <name>C</name> 
     <rollNo>3</rollNo> 
    </Student> 
    <Student> 
     <name>E</name> 
     <rollNo>5</rollNo> 
    </Student> 
    <Student> 
     <name>H</name> 
     <rollNo>8</rollNo> 
    </Student> 
    <Student> 
     <name>J</name> 
     <rollNo>10</rollNo> 
    </Student> 
</course> 
<course> 
    <name>Physics</name> 
    <Student> 
     <name>F</name> 
     <rollNo>6</rollNo> 
    </Student> 
</course> 
<course> 
    <name>Sports</name> 
    <Student> 
     <name>B</name> 
     <rollNo>2</rollNo> 
    </Student> 
    <Student> 
     <name>C</name> 
     <rollNo>3</rollNo> 
    </Student> 
</course> 
</Courses> 

を与えます。一部のコースはリストされていません。コンピュータと一部の学生情報が間違ったコースに割り当てられています。誰も私にこれを助けることができますか?

+1

これは*のグループ化*問題です。 XSLT 1.0を使用している場合は、ここから始めてください:http://www.jenitennison.com/xslt/grouping/muenchian.html –

+0

ありがとう@ michael.hor257k私はxsltの新機能です。私はリンクを試して、私はそれのための部分的な解決を得た。しかし、1つの学生につき複数のコースを指定すると、結果は正しくありません。例えば生徒Aはコース1とコース2に登録され、生徒Bはコース2に登録されます。出力では、コース1の両方の生徒とコース2の生徒の両方を取得しています。理想的には、Course1はStudentAを、Course2は両方の生徒を持つ必要があります。手伝ってくれますか? – Neelam

+0

'student'ノードを' course'でグループ化する必要があります。次に、それぞれのユニークな「コース」の下に、そのグループの学生をリストアップします。あなたがまだそれを動作させることができない場合は、私たちがそれを修正できるようにあなたの試みを投稿してください。 –

答えて

0

私は、コースに生徒を登録するたびに別のStudent要素があると思いました。それ以外の場合、rollNoは何を表していますか?

とにかく、あなたは今示した構造で、あなたのスタイルシートは次のようになります。

XSLT 1.0

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

<xsl:key name="course" match="course" use="." /> 

<xsl:template match="Students"> 
    <Courses> 
     <xsl:for-each select="Student/course[count(. | key('course', .)[1]) = 1]"> 
      <xsl:sort select="." /> 
      <course> 
       <name> 
        <xsl:value-of select="."/> 
       </name> 
       <xsl:for-each select="key('course', .)"> 
        <Student> 
         <xsl:copy-of select="../name | ../rollNo"/> 
        </Student> 
       </xsl:for-each> 
      </course> 
     </xsl:for-each> 
    </Courses> 
</xsl:template> 

</xsl:stylesheet> 
+0

それは私の問題を解決しました。 xsltでこのようなグループ化の問題についてもっと読むことができるリンクをいくつか教えてください。 – Neelam

+0

私は知らないのが怖いです。しかし、ここに掲載されている例から多くを学ぶことができます。 –

関連する問題