我需要將其中包含每個學生數據的Students.xml轉換爲Courses.xml,其中每個課程都組織這些數據。 當我爲每個學生開設一門課程時,我可以達到這個目標,但如果每個學生提供一門以上的課程,結果都是錯誤的。有沒有一種方法可以讓我獲得數組中的所有課程名稱,然後根據這些值在我的xsl文件中進行循環?使用XSLT將XML重組爲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>
這給了我下面的輸出
<?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>
正如人們所看到的結果是完全錯誤的。一些課程甚至沒有列出,例如計算機和一些學生的信息被分配到錯誤的課程。任何人都可以幫助我嗎?
這是一個*分組*問題。如果您使用XSLT 1.0,請從此處開始:http://www.jenitennison.com/xslt/grouping/muenchian.html –
謝謝@ michael.hor257k我是xslt的新手。我嘗試了鏈接,併爲它提供了部分解決方案。但是,如果我爲每個學生指定多個課程,則結果不正確。例如學生A參加課程1和課程2,學生B參加課程2。在輸出結果中,我讓兩個學生在課程1之下,並且沒有課程2的列表。理想情況下,Course1應該有StudentA和Course2應該有兩個學生。你能幫我嗎? – Neelam
你需要通過'course'對你的'Student'節點進行分組。然後,在每個獨特的「課程」下,列出該組中的學生。如果您仍然無法完成工作,請發佈您的嘗試,以便我們修復它。 –