2011-11-20 249 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>