XML和XSL连接

时间:2010-05-02 12:52:34

标签: xml xslt

我在XML和XSL文件之间有问题。在XML文件中,有一些元素,例如
*< school>
< student studentID =“12345”>
< name> Stud I< / name>
<优先> CMPE471< /取>
<优先> CMPE412< /取>
<优先> CMPE100< /取>
< /学生>
< br&>&lt; student studentID =“67890”&gt;
&lt; name&gt; Stud II&lt; / name&gt;
&lt; take&gt; CMPE471&lt; / take&gt;
&lt; take&gt; CMPE412&lt; / take&gt;
&lt; / student&gt;

&lt; course courseCode =“CMPE471”&gt;
&lt; courseName&gt; NAME I&lt; / courseName&gt;
&lt; description&gt; DESC I&lt; / description&gt;
&lt; / course&gt;

&lt; course courseCode =“CMPE412”&gt;
&lt; courseName&gt; NAME II&lt; / courseName&gt;
&lt; description&gt; DESC II&lt; / description&gt;
&lt; / course&gt;

&lt; course courseCode =“CMPE100”&gt;
&lt; courseName&gt; NAME III&lt; / courseName&gt;
&lt; description&gt; DESC III&lt; / description&gt;
&lt; / course&gt;

在XSL文件中,我想要达到我指定“courseCode”的“description”元素。
输出应该是这样的,
1. Stud I
a。 CMPE471描述I b。 CMPE412描述II
c。 CMPE100描述III

2。 Stud II
a。 CMPE471描述I b。 CMPE412 Desc II


在XSL文件中,我试着写一些东西:



&LT;醇&GT;
&lt; xsl:for-each select =“/ school / student”&gt;
&lt; xsl:sort data-type =“text”order =“ascending”select =“name”/&gt;

&lt; li&gt;&lt; xsl:value-of select =“name”/&gt;

&lt; ol type =“a”&gt;
&lt; xsl:for-each select =“take”&gt;
&lt; xsl:sort data-type =“text”select =“text()”order =“ascending”/&gt;
&LT;李&GT;

&lt; xsl:for-each select =“/ school / course”&gt; //问题
&lt; xsl:value-of select =“description [@cordCode = text()]”/&gt; //问题
&LT; /的xsl:for-每个&GT; //问题

&LT; /锂&GT; &LT; /的xsl:for-每个&GT; &LT; /醇&GT; &LT; /的xsl:for-每个&GT; &LT; /醇&GT;
感谢。

1 个答案:

答案 0 :(得分:0)

请参阅下面的我的XSLT 1.0和2.0解决方案。代码中的问题是:

<xsl:value-of select="description [@courseCode = text()]"/>

这意味着您正在寻找属性description与其文本值相同的元素courseCode。我所做的是将课程代码保存在变量中,并检查属性与此变量相同的course元素。

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

  <xsl:template match="/">
    <ol>
      <xsl:for-each select="/school/student">
        <xsl:sort data-type="text" order="ascending" select="name" />
        <li>
          <xsl:value-of select="name" />
          <ol type="a">
            <xsl:for-each select="takes">
              <xsl:sort data-type="text" select="." order="ascending" />
              <li>
                <xsl:variable name="coursecode" select="." />
                <xsl:value-of select="concat('(',.,') ')" />
                <xsl:for-each select="/school/course[@courseCode = $coursecode]">
                  <xsl:value-of select="description" />
                </xsl:for-each>

              </li>
            </xsl:for-each>
          </ol>
        </li>
      </xsl:for-each>
    </ol>
  </xsl:template>
</xsl:stylesheet>