1

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>
1
  • Not working how? In general, XSLT is not like BASIC, where you loop over things with a FOR loop. Set up templates to handle elements, and let XSLT do the looping, that's what it does for a living. Also, indent your code properly, otherwise you and everyone else looking it will pull their hair out. Commented Oct 13, 2015 at 13:55

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

<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>

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.