簡體   English   中英

XSL:SORT不起作用?

[英]XSL:SORT doesn't work?

我有一個包含價格值的XML文件。我想按價格排序我的表:這是我的代碼,但在IE中它不起作用,其他元素的值一起顯示在price tabledata中:

        <td>
          <xsl:apply-templates >
            <xsl:sort select="price" data-type="number" order="descending" />
          </xsl:apply-templates>
        </td>

和我的xml文件( book.xml

這是我的XSLT文件:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
  <xsl:template match="/">
    <xsl:apply-templates />
  </xsl:template>
  <xsl:template match="catalog">
    <html>
      <head>
        <title>
           <xsl:text>Buchhandlung</xsl:text> 
        </title> 
      </head>
      <table border="1">
        <tr>
          <th>id</th>
          <th>author</th>
          <th>titel</th>
          <th>price</th>
          <th>description</th>
        </tr>
        <xsl:apply-templates />
  </table>
  </html>
  </xsl:template>
  <xsl:template match="book">
      <tr>
        <td>
          <xsl:apply-templates select="@id" />
        </td>

        <td>
          <xsl:apply-templates select="author" />
        </td>

        <td>
          <xsl:apply-templates select="title" />
        </td>

        <td>
          <xsl:apply-templates select="book" >
            <xsl:sort select="price" data-type="number" order="descending"  />
          </xsl:apply-templates>
        </td>
        <!--<td>
          <xsl:apply-templates select="price" />
        </td>-->

        <td>
          <xsl:apply-templates select="description" />
        </td>
      </tr>
  </xsl:template>
<

/ XSL:樣式>

謝謝你的幫助

sort不會產生輸出。 它規定了排序節點集傳遞給當前范圍內任何內容的順序 - 所以你需要在它下面添加類似<xsl:value-of select="price"/>東西 - 指令值將按大小而不是文件的自然順序打印出價格。

編輯#1:我現在已經注意到你在apply-templates使用它而不是for-each 在這種情況下,您需要定義與price節點匹配的模板。 然后,將在排序順序中的每個價格節點上調用此模板,而不是在文件中的順序。

編輯#2:一個例子; 要按照書的價格打印書籍詳細信息列表,您可能需要以下內容:

  <xsl:template match="/">
    <xsl:apply-templates />
  </xsl:template>
  <xsl:template match="catalog">
    <html>
      <head>
        <title>
           <xsl:text>Buchhandlung</xsl:text> 
        </title> 
      </head>
      <table border="1">
        <tr>
          <th>id</th>
          <th>author</th>
          <th>titel</th>
          <th>price</th>
          <th>description</th>
        </tr>
        <xsl:apply-templates select="book">
          <xsl:sort select="price"/>
        </xsl:apply-templates>
  </table>
  </html>
  </xsl:template>
  <xsl:template match="book">
      <tr>
        <td>
          <xsl:apply-templates select="@id" />
        </td>

        <td>
          <xsl:apply-templates select="author" />
        </td>

        <td>
          <xsl:apply-templates select="title" />
        </td>  

        <td>
          <xsl:apply-templates select="description" />
        </td>
      </tr>
  </xsl:template>

請注意,目錄模板現在包含排序邏輯,我已從書模板中刪除了類似的邏輯。 書籍模板無權對傳遞給它的節點進行排序,您必須指定目標節點傳遞給書籍模板的順序,以獲得您所追求的結果。 目錄模板確保每個book元素按價格順序傳遞到template match="book" ,而不是文件中的順序。 我假設您不想在圖書詳細信息中打印圖書價格,因為您提供的示例也不會這樣做,但如果您想要包含此信息,則將以與完全相同的方式完成其他字段的td輸出。

你需要代碼

<xsl:template match="catalog">
  <table>
    <tbody>
     <xsl:apply-templates select="book">
       <xsl:sort select="price" data-type="number" order="descending"/>
     </xsl:appl-templates>
   </tbody>
  </table>
</xsl:template>

<xsl:template match="book">
  <tr>
    <xsl:apply-templates/>
  </tr>
</xsl:template>

<xsl:template match="book/*">
 <td>
  <xsl:value-of select="."/>
 </td>
</xsl:template>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM