Reputation: 51
Hi I would like to change source.xml into destination.xml using xslt but my code is not working. Kindly help
source.xml
<Programme>
<SubjectList>
<Subject>Maths</Subject>
<Subject>Science</Subject>
<Subject>History</Subject>
<Subject>Language</Subject>
</SubjectList>
<StudentList>
<Student>
<Name>Jack</Name>
<Class>5</Class>
<Subjects>
<Course>Maths</Course>
<Course>Language</Course>
</Subjects>
</Student>
<Student>
<Name>John</Name>
<Class>4</Class>
<Subjects>
<Course>Maths</Course>
<Course>Science</Course>
</Subjects>
</Student>
<Student>
<Name>Anna</Name>
<Class>4</Class>
<Subjects>
<Course>Science</Course>
<Course>History</Course>
</Subjects>
</Student>
<Student>
<Name>Tana</Name>
<Class>5</Class>
<Subjects>
<Course>History</Course>
<Course>Language</Course>
</Subjects>
</Student>
</StudentList>
</Programme>
destination.xml
<ProgramList>
<Subject>
<title>Maths</title>
<Students>
<Name>Jack</Name>
<Name>John</Name>
<Students>
</Subject>
<Subject>
<title>Science</title>
<Students>
<Name>John</Name>
<Name>Anna</Name>
<Students>
</Subject>
<Subject>
<title>History</title>
<Students>
<Name>Anna</Name>
<Name>Tana</Name>
<Students>
</Subject>
<Subject>
<title>Language</title>
<Students>
<Name>Jack</Name>
<Name>Tana</Name>
<Students>
</Subject>
</ProgramList>
This is my xslt but it is not working, I am very much new to xslt, kindly help.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<subjects>
<xsl:for-each select="SubjectList/Subject">
<xsl:variable name="var1" select="."/>
<subject>
<title><xsl:value-of select="text()"/></title>
<xsl:for-each select="StudentList/Student">
<xsl:variable name="student" select="."/>
<xsl:for-each select="Subjects/Course">
<xsl:variable name="var2">
<xsl:value-of select="."/>
</xsl:variable>
<xsl:if test= "$var2=$var1">
<student>
<name><xsl:value-of select="$student/Name"/></name>
<class><xsl:value-of select="$student/Class"/></class>
</student>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
<subject>
<xsl:value-of select="$newline"/>
</xsl:for-each>
</subjects>
</xsl:template>
</xsl:stylesheet>
Upvotes: 1
Views: 1924
Reputation: 46
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" /> <xsl:key name="NameOfTheKey" match="Student" use="Subjects/Course" /> <xsl:template match="/">
<ProgramList>
<xsl:for-each select="Programme/SubjectList/Subject">
<Subject>
<xsl:variable name="title" select="."/>
<title>
<xsl:value-of select= "$title"/>
</title>
<Student>
<xsl:for-each select="key('NameOfTheKey', $title)">
<fromRate>
<xsl:value-of select="Name"></xsl:value-of>
</fromRate>
</xsl:for-each>
</Student>
</Subject>
</xsl:for-each>
</ProgramList>
</xsl:template>
</xsl:stylesheet>
Upvotes: 1
Reputation: 163262
The context for the path expression StudentList/Student
is a Subject element, but your Subject elements have no StudentList
child. You need to go up a couple of levels first: ../../StudentList/Student
. But better, for this kind of problem, read about xsl:key
and the key()
function.
Upvotes: 1