XSLT排序子元素跟踪 [英] XSLT sort subelement followup
问题描述
类似于先前的子元素排序,但是我还有一些额外的节点层.我无法找出对先前有效答案的简单扩展. 如何使用XSLT对XML的子元素进行排序
Similar to the previous subelement sort, but I have a few extra layers of nodes. I can't figure out a simple extension to the previous answer that works. How to sort a subelement of XML with XSLT
简化的输入示例:
<Iteration>
<Iteration_query-ID>NODE_10008</Iteration_query-ID>
<Iteration_query-def>NODE_10008</Iteration_query-def>
<Iteration_query-len>339</Iteration_query-len>
<Iteration_hits>
<Hit>
<Hit_id>110679166</Hit_id>
<Hit_def>[Roseobacter litoralis Och 149]</Hit_def>
<Hit_len>68</Hit_len>
<Hit_hsps>
<Hsp>
<Hsp_score>300.0</Hsp_score>
<Hsp_evalue>4.94413E-26</Hsp_evalue>
<Hsp_query-from>69</Hsp_query-from>
<Hsp_query-to>272</Hsp_query-to>
</Hsp>
</Hit_hsps>
</Hit>
<Hit>
<Hit_id>114767284</Hit_id>
<Hit_def>[Roseovarius sp. HTCC2601]</Hit_def>
<Hit_len>68</Hit_len>
<Hit_hsps>
<Hsp>
<Hsp_bit-score>127.487</Hsp_bit-score>
<Hsp_score>319.0</Hsp_score>
<Hsp_evalue>3.0968E-28</Hsp_evalue>
<Hsp_query-from>69</Hsp_query-from>
<Hsp_query-to>272</Hsp_query-to>
</Hsp>
</Hit_hsps>
</Hit>
</Iteration_hits>
</Iteration>
我想按Hsp属性之一(例如Hsp_score)进行排序.我已经能够对它进行排序,但是我不知道如何防止它丢失某些Hit节点:
I want to sort by one of the Hsp attributes, such as Hsp_score. I've been able to get it to sort, but I can't figure out how to keep it from dropping out some of the Hit nodes:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:import href="identity.xsl"/>
<!--
Iteration/Iteration_hits/Hit/Hit_hsps/Hsp
-->
<xsl:template match="Iteration_hits">
<xsl:copy>
<xsl:apply-templates select="Hit/Hit_hsps/Hsp">
<xsl:sort select="Hsp_score" order="descending"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
推荐答案
大卫的代码存在轻微问题:
There is a slight problem with David's code:
按照书面规定,尽管排序顺序指定为descending
,但是120将排在 之后.
As written, 120 will come after 90, although the sort order is specified as descending
.
使用数字排序键进行排序的正确方法如下所示:
The proper way to sort with numeric sort keys is illustrated below:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<!-- -->
<xsl:param name="pNewType" select="'myNewType'"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<!-- -->
<xsl:template match="Iteration_hits">
<xsl:copy>
<xsl:apply-templates select="Hit">
<xsl:sort select="Hit_hsps/Hsp/Hsp_score"
data-type="number"
order="descending"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
应用于此XML文档:
<Iteration>
<Iteration_query-ID>NODE_10008</Iteration_query-ID>
<Iteration_query-def>NODE_10008</Iteration_query-def>
<Iteration_query-len>339</Iteration_query-len>
<Iteration_hits>
<Hit>
<Hit_id>110679166</Hit_id>
<Hit_def>[Roseobacter litoralis Och 149]</Hit_def>
<Hit_len>68</Hit_len>
<Hit_hsps>
<Hsp>
<Hsp_score>120.0</Hsp_score>
<Hsp_evalue>4.94413E-26</Hsp_evalue>
<Hsp_query-from>69</Hsp_query-from>
<Hsp_query-to>272</Hsp_query-to>
</Hsp>
</Hit_hsps>
</Hit>
<Hit>
<Hit_id>114767284</Hit_id>
<Hit_def>[Roseovarius sp. HTCC2601]</Hit_def>
<Hit_len>68</Hit_len>
<Hit_hsps>
<Hsp>
<Hsp_bit-score>127.487</Hsp_bit-score>
<Hsp_score>90.0</Hsp_score>
<Hsp_evalue>3.0968E-28</Hsp_evalue>
<Hsp_query-from>69</Hsp_query-from>
<Hsp_query-to>272</Hsp_query-to>
</Hsp>
</Hit_hsps>
</Hit>
</Iteration_hits>
</Iteration>
产生了正确的结果:
<Iteration>
<Iteration_query-ID>NODE_10008</Iteration_query-ID>
<Iteration_query-def>NODE_10008</Iteration_query-def>
<Iteration_query-len>339</Iteration_query-len>
<Iteration_hits><Hit>
<Hit_id>110679166</Hit_id>
<Hit_def>[Roseobacter litoralis Och 149]</Hit_def>
<Hit_len>68</Hit_len>
<Hit_hsps>
<Hsp>
<Hsp_score>120.0</Hsp_score>
<Hsp_evalue>4.94413E-26</Hsp_evalue>
<Hsp_query-from>69</Hsp_query-from>
<Hsp_query-to>272</Hsp_query-to>
</Hsp>
</Hit_hsps>
</Hit><Hit>
<Hit_id>114767284</Hit_id>
<Hit_def>[Roseovarius sp. HTCC2601]</Hit_def>
<Hit_len>68</Hit_len>
<Hit_hsps>
<Hsp>
<Hsp_bit-score>127.487</Hsp_bit-score>
<Hsp_score>90.0</Hsp_score>
<Hsp_evalue>3.0968E-28</Hsp_evalue>
<Hsp_query-from>69</Hsp_query-from>
<Hsp_query-to>272</Hsp_query-to>
</Hsp>
</Hit_hsps>
</Hit></Iteration_hits>
</Iteration>
请注意使用 查看全文