为什么for-each不能在我的xslt文件中工作

时间:2015-01-14 15:54:22

标签: xml xslt foreach

我不知道为什么我的for-each声明不起作用。我检查了其他帖子,但我找不到原因。如果有人可以帮助我?
这是我的xml文件:

<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/xsl" href="Student.xslt"?>
<students xmlns = "urn:students">
  <student>
    <name>Pesho</name>
    <sex>Both</sex>
    <birthdate>1990-01-01</birthdate>
    <phone>0888112233</phone>
    <email>azsympesho@mail.bg</email>
    <course>Beginners For Dummies</course>
    <specialty>Dummy</specialty>
    <facultyN>99</facultyN>
    <exams>
      <name>Math</name>
      <tutor>Gosho</tutor>
      <score>90</score>
    </exams>
    <enrollment>
      <date>2015-11-01</date>
      <examScore>5</examScore>
    </enrollment>
    <endorsements>some</endorsements>
  </student>
  <student>
    <name>Gosho</name>
    <sex>Female</sex>
    <birthdate>1970-01-01</birthdate>
    <phone>0888332211</phone>
    <email>azsymgosho@mail.bg</email>
    <course>Beginners For Dummies</course>
    <specialty>Beginner</specialty>
    <facultyN>66</facultyN>
    <exams>
      <name>Biology</name>
      <tutor>Pesho</tutor>
      <score>90</score>
    </exams>
    <enrollment>
      <date>2015-12-01</date>
      <examScore>4</examScore>
    </enrollment>
    <endorsements>other</endorsements>
  </student>
</students>

这是xslt文件:

<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:template match="/">
      <html>
        <body>
          <h1>Students Info</h1>
          <table bgcolor="#E0E0E0" cellspacing="1">
            <tr bgcolor="#EEEEEE">
              <td>Name</td>
              <td>Sex</td>
              <td>Birth Date</td>
              <td>Phone</td>
              <td>Email</td>
              <td>Course</td>
              <td>Specialty</td>
              <td>Faculty №</td>
              <td>Exams
                <td>Name</td>
                <td>Tutor</td>
                <td>Score</td>
              </td>                
              <td>Enrollment
                <td>Date</td>
                <td>Exam Score</td>
              </td>
              <td>Endorsement</td>
              </tr>
           <xsl:for-each select ="/students/student">
          <tr>
            <td>
              <xsl:value-of select="name"/>
            </td>
            <td>
              <xsl:value-of select="sex"/>
            </td>
            <td>
              <xsl:value-of select="birthdate"/>
            </td>
            <td>
              <xsl:value-of select="phone"/>
            </td>
            <td>
              <xsl:value-of select="email"/>
            </td>
            <td>
              <xsl:value-of select="course"/>
            </td>
            <td>
              <xsl:value-of select="specialty"/>
            </td>
            <td>
              <xsl:value-of select="facultyN"/>
            </td>
            <td>
              <xsl:for-each select="exams">
                <td>
                  <xsl:value-of select="name"/>
                </td>
                <td>
                  <xsl:value-of select="tutor"/>
                </td>
                <td>
                  <xsl:value-of select="score"/>
                </td>
              </xsl:for-each>
            </td>
            <td>
              <xsl:for-each select="enrollment">
                <td>
                  <xsl:value-of select="date"/>
                </td>
                <td>
                  <xsl:value-of select="examScore"/>
                </td>
              </xsl:for-each>
            </td>
            <td>
              <xsl:value-of select="endorsement"/>
            </td>
          </tr>
        </xsl:for-each>
      </table>
    </body>
  </html>
</xsl:template>

............................................... .................................................. ....

1 个答案:

答案 0 :(得分:1)

这是名称空间问题。

urn:students的名称空间前缀声明添加到样式表中:

<xsl:stylesheet version="2.0"
                xmlns:s="urn:students"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

然后在xsl:for-each/@select XPath:

中使用它
       <xsl:for-each select ="/s:students/s:student">

对样式表中的所有XPath执行此操作。