2013-03-04 3 views
0

в основном я хочу сортировать данные по уровню, и я получаю сообщение об ошибке. Я новичок в xslt. Я попробовал код ниже, но не работал.xslt сортировка не работает

вот мой xml-файл.

<?xml version="1.0" encoding="UTF-8"?> 
<?xml-stylesheet type="text/xsl" href="XSLTEx1WT.xsl"?> 
<Modules> 
    <module code="CSE2041"> 
     <name>Web Technologies II</name> 
     <credit>3</credit> 
     <level>2</level> 
    </module> 
    <module code="CSE2031Y"> 
     <name>Object Oriented Software Development</name> 
     <credit>6</credit> 
     <level>2</level> 
    </module> 
    <module code="CSE1041"> 
     <name>Web Technologies I</name> 
     <credit>3</credit> 
     <level>1</level> 
    </module> 
</Modules> 

вот мой XSL файл

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    version="1.0"> 
    <xsl:variable name="deptCredit" select="99"/> 
    <xsl:variable name="myBorder" select="1"/> 
    <xsl:template match="/"> 
     <html> 
      <head> 
       <title>XSLT Exercise 1</title></head> 
      <body> 
       <table border="{$myBorder}"> 
        <thead><tr><th>Module Name</th><th>No. of Credits</th><th>Level</th></tr></thead> 
        <tbody> 

         <xsl:apply-templates> 
          **<xsl:sort select="level" data-type="number"/>** 
         </xsl:apply-templates> 
        <tr><td colspan="3">Departmental credits needed: <xsl:value-of select="$deptCredit"/></td></tr> 
        <tr><td colspan="3">Percentage cleared: <strong> <xsl:value-of select="format-number(sum(//credit/text()) div $deptCredit,'##.##%')"/></strong> 
        </td></tr> 
        </tbody> 
       </table> 
      </body> 
     </html> 
    </xsl:template> 

    <xsl:template match="module"> 
     <xsl:apply-templates select="@code"/> 
      <tr><xsl:apply-templates select="*"/></tr> 
    </xsl:template> 

    <xsl:template match="name"> 
     <xsl:call-template name="sjName"/> 
    </xsl:template> 

    <xsl:template match="@code"> 
     <tr style="background-color:silver;"> 
      <td colspan="3" style="text-align:center;"> 
       <xsl:value-of select="."/> 
      </td> 
     </tr> 
    </xsl:template> 

    <xsl:template match="credit"> 
     <td style="color:blue;"><strong><xsl:value-of select="text()"/></strong></td> 
    </xsl:template> 

    <xsl:template match="level"> 
     <td style="color:blue;"><strong><xsl:value-of select="text()"/></strong></td> 
    </xsl:template> 

    <xsl:template name="sjName"> 
      <xsl:choose> 
       <xsl:when test="contains(../@code,'Y')"> 
        <td style="color:orange;font-weight:bold;"><xsl:value-of select="."/></td> 
       </xsl:when> 
       <xsl:otherwise> 
        <td style="color:lime;font-weight:bold;"><xsl:value-of select="."/></td> 
       </xsl:otherwise> 
      </xsl:choose>    
    </xsl:template> 

</xsl:stylesheet> 

я попытался выше, но он не работает

ответ

1

Вам просто нужно изменить:

<xsl:apply-templates> 
    <xsl:sort select="level" data-type="number"/> 
</xsl:apply-templates> 

к этому:

<xsl:apply-templates select="Modules/module"> 
    <xsl:sort select="level" data-type="number"/> 
</xsl:apply-templates> 
+0

Ваш ответ лучше, я удалил свою. +1 – kamituel

+0

спасибо за помощь –

Смежные вопросы