2012-11-21 78 views
2

lecturer.xslxsl排序不排序?

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format"> 
    <xsl:output method="html" version="4.0"/> 
     <xsl:template match="/"> 
      <html> 
      <head> 
       <title>Lecturer Information</title> 
      </head> 
      <body> 

       <table border="1"> 

        <tr bgcolor="#9acd32"> 
         <th>Name</th> 
         <th>Teaching</th> 
         <th>Research</th> 
        </tr> 

        <xsl:for-each select="lecturers/lecturer"> 
         <tr> 
          <td> 
           <xsl:apply-templates select="name"> 
            <xsl:sort select="@last" data-type="text" order="descending"/> 
           </xsl:apply-templates> 
          </td> 
          <td><xsl:apply-templates select="teaching/course" /></td> 
          <td><xsl:value-of select="research"/></td> 
         </tr> 
        </xsl:for-each> 

       </table> 

      </body> 
     </html> 
    </xsl:template> 

    <!-- Templates HERE --> 
    <xsl:template match="name"> 
     <xsl:value-of select="@title"/><xsl:text> </xsl:text> 
     <xsl:value-of select="@first"/><xsl:text> </xsl:text> 
     <xsl:value-of select="@last"/> 
    </xsl:template> 

    <xsl:template match="teaching/course"> 
     <xsl:for-each select="."> 
       <xsl:value-of select="concat(. , '(',@code, ')')"/> <br /> 
      <!-- <xsl:value-of select="."/> (<xsl:value-of select="@code"/>) --> 
     </xsl:for-each> 
    </xsl:template> 


</xsl:stylesheet> 

lecturer.xml

<?xml version="1.0" encoding="UTF-8"?> 
<?xml-stylesheet type="text/xsl" href="lecturer.xsl" ?> 

<!DOCTYPE lecturers [ 

<!ELEMENT lecturers (lecturer+)> 
<!ELEMENT lecturer (name, teaching, research)> 

<!-- Element name must contain attribute title, first and last --> 
<!ELEMENT name (#PCDATA)> 
<!ATTLIST name 
    title CDATA #REQUIRED 
    first CDATA #REQUIRED 
    last CDATA #REQUIRED 
> 

<!-- Teaching can have more than one course--> 
<!ELEMENT teaching (course+)> 

<!ELEMENT course (#PCDATA)> 
<!ATTLIST course 
    code CDATA #REQUIRED 
> 

<!ELEMENT research (#PCDATA)> 

]> 
<lecturers> 
    <lecturer> 
     <name title="Professor" first="Peter" last="Quirk"/> 
     <teaching> 
       <course code="CO3070">XML and the Web</course> 
       <course code="CO3300">Web Server Architectures</course> 
     </teaching> 
     <research> 
       The application of Web protocols to Biology 
     </research> 
    </lecturer> 
    <lecturer> 
     <name title="Mr" last="Abdi" first="Ahmet"/> 
     <teaching> 
       <course code="CO1337">Ahmet's Course</course> 
     </teaching> 
     <research> 
       The Best Research In the world. 
     </research> 
    </lecturer> 
</lecturers> 

的XSL的这一部分似乎无法排序

<xsl:sort select="@last" data-type="text" order="descending"/> 
+0

显示预期输出与实际输出会有帮助。 – LarsH

回答

3

您将<xsl:sort>元素作为<xsl:apply-templates select="name">的子元素,但您不需要进行任何排序,因为在给定的上下文中只有一个名称。

相反,你需要做<xsl:sort><xsl:for-each>的孩子:

<xsl:sort select="name/@last" data-type="text" order="descending"/> 

确保您更改select属性如图所示,这样的排序将能够从的情况下找到姓氏<lecturer>元素。

+0

感谢回复+1 – ahmet

+2

@ahmet:不客气。请“接受”马修的回答,而不是我的回答......他首先回答,他比我更需要积分。 – LarsH

+0

+1是一个很好的答案,是一个很好的和慷慨的人。 Matthew也是+1。 –

4

我相信行

<xsl:sort select="name/@last" data-type="text" order="descending"/> 

需求是for-each声明

<xsl:for-each select="lecturers/lecturer"> 

那应该按姓氏排序所有演讲者的下右。

+0

+1是的,除了不要忘记上下文的变化!所以排序选择属性需要调整。 – LarsH

+0

我爱你。谢谢 – ahmet

+0

@LarsH对。我把它分开了。我会更新我的答案,即使它现在最终会与您的答案匹配。 :P –