2015-10-13 116 views
0

您好我想使用xslt更改source.xml到destination.xml但我的代码不起作用。好心帮为每个循环的另一个循环内的每个循环的XSLT

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> 

这是我的XSLT,但它不工作,我非常新的XSLT,请帮助。

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

不工作怎么样?一般来说,XSLT不像BASIC,用FOR循环来循环。设置模板来处理元素,并让XSLT进行循环,这就是它为了生活所做的。此外,正确缩进你的代码,否则你和其他人看着它会把头发拉出来。 – 2015-10-13 13:55:05

回答

0

路径表达式StudentList/Student的背景是一个主题元素,但你的主题元素没有StudentList孩子。您需要先升级几个级别:../../StudentList/Student。但更好的是,对于这种问题,请阅读xsl:keykey()函数。