2011-11-20 161 views
2

我想通過查詢xml文檔來創建一個html表格。我正在使用xslt。xslt按子元素排序

這是問題所在。 「父」節點包含許多「子」節點。我必須按排序順序(降序)包含父節點的@name和「子節點」節點的數量。所以我在做

<?xml version="1.0" encoding="ISO-8859-1"?> 

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

<xsl:template match="parent[count(child) &gt; 3]"> 

    <html> 
    <table border="1"> 
     <xsl:for-each select="."> 
     <xsl:sort select="{count(child)}" data-type="number" order="descending"/> 
     <tr> 
     <td><b><xsl:value-of select="@name" /></b></td> 
     <td><xsl:value-of select="count(child)" /></td> 
     </tr> 
     </xsl:for-each> 
    </table> 
    </html> 

    </xsl:template> 
    <xsl:template match="text()" /> 
    </xsl:stylesheet> 

我得到的HTML,但唯一的問題是我沒有得到它按照子元素的排序順序。我懷疑我使用的計數不正確:xsl:sort?你能幫我嗎?

輸入XML

<outer> 
<parent name="abc" attr1="22664136" attr2="647500"> 
<child percentage="11">aaa</child> 
<child percentage="35">bbb</child> 
<child percentage="50">ccc</child> 
</parent> 

<parent name="ggg" attr1="3249136" attr2="28750"/> 

<parent name="ghi" attr1="29183032" attr2="2381740"> 
<child2> 
<name>ppp</name> 
<attr1>1507241</attr1> 
</child2> 
</parent> 


<parent name="qwe" attr1="10342899" attr2="1246700"/> 

<parent name="lkj" attr1="65647" attr2="440"> 
<child percentage="100">jjj</child> 
</parent> 

</outer> 
+0

提供您輸入XML太 –

+0

@SivaCharan增加I/P XML。 – abc

+0

我不確定是否有意爲之,但除了文本元素外,您提供的輸入文檔沒有任何匹配模板:您的所有「父」元素都沒有「count(child)> 3」。 – btlachance

回答

3

有在所提供的XSLT代碼頻頻失誤!

最大的問題是在這裏:

<xsl:for-each select="."> 
     <xsl:sort select="{count(child)}" data-type="number" order="descending"/> 
     <tr> 
     <td><b><xsl:value-of select="@name" /></b></td> 
     <td><xsl:value-of select="count(child)" /></td> 
     </tr> 
    </xsl:for-each> 

這將不進行任何有意義的排序,因爲節點的節點集進行排序只包含一個節點 - 當前節點。

接下來的問題是在這裏

<xsl:sort select="{count(child)}" data-type="number" order="descending"/> 

不應該有在XSLT指令的任何select屬性的任何AVT - 你需要刪除大括號。

第三個問題是排序被指定得太晚 - 模板內部爲parent父母沒有任何子女,他們自己有child子女。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="/*"> 
     <html> 
      <table border="1"> 
       <xsl:for-each select="parent"> 
        <xsl:sort select="count(child)" data-type="number" order="descending"/> 
        <tr> 
         <td> 
          <b> 
           <xsl:value-of select="@name" /> 
          </b> 
         </td> 
         <td> 
          <xsl:value-of select="count(child)" /> 
         </td> 
        </tr> 
       </xsl:for-each> 
      </table> 
     </html> 
    </xsl:template> 
    <xsl:template match="text()" /> 
</xsl:stylesheet> 

當這個變換所提供的XML文檔施加:

<outer> 
    <parent name="abc" attr1="22664136" attr2="647500"> 
     <child percentage="11">aaa</child> 
     <child percentage="35">bbb</child> 
     <child percentage="50">ccc</child> 
    </parent> 
    <parent name="ggg" attr1="3249136" attr2="28750"/> 
    <parent name="ghi" attr1="29183032" attr2="2381740"> 
     <child2> 
      <name>ppp</name> 
      <attr1>1507241</attr1> 
     </child2> 
    </parent> 
    <parent name="qwe" attr1="10342899" attr2="1246700"/> 
    <parent name="lkj" attr1="65647" attr2="440"> 
     <child percentage="100">jjj</child> 
    </parent> 
</outer> 
糾正所有主要問題,以上所討論的,可以在下面的代碼到達

通緝排序結果產生

<html> 
    <table border="1"> 
     <tr> 
     <td><b>abc</b></td> 
     <td>3</td> 
     </tr> 
     <tr> 
     <td><b>lkj</b></td> 
     <td>1</td> 
     </tr> 
     <tr> 
     <td><b>ggg</b></td> 
     <td>0</td> 
     </tr> 
     <tr> 
     <td><b>ghi</b></td> 
     <td>0</td> 
     </tr> 
     <tr> 
     <td><b>qwe</b></td> 
     <td>0</td> 
     </tr> 
    </table> 
</html> 
+0

看到for-each/sort在一組大小爲1 。你應該被標記爲正確的! – btlachance

+0

@btlachance:謝謝。請告訴OP :) –

+0

@Dimitre在閱讀答案之前,我想到了這一點。但我現在把你的標記標記爲正確。希望你快樂:) – abc

0

它已經有一段時間了,所以我可能記錯,但我相信,計數(子)應計(子::節點())。

+0

引發錯誤:「{count(child :: node})中第10行char 6上的XPath語法錯誤: 未知的軸名稱:」 – abc

1

差不多。你不需要在你的xsl:sort select="..." 您的,每個會再看看像大括號:

<xsl:for-each select="."> 
    <xsl:sort select="count(child)" data-type="number" order="descending"/> 
    <tr> 
    <td><b><xsl:value-of select="@name" /></b></td> 
    <td><xsl:value-of select="count(child)" /></td> 
    </tr> 
</xsl:for-each> 

編輯:正如信息的添加位,你只能在文字使用花括號,導致要素。 From the XSLT2.0 spec on attribute value templates

The following example creates an img result element from a photograph element in the source; the value of the src and width attributes are computed using XPath expressions enclosed in attribute value templates:

<xsl:variable name="image-dir" select="'/images'"/> 
<xsl:template match="photograph"> 
    <img src="{$image-dir}/{href}" width="{size/@width}"/> 
</xsl:template>