如何对for-each循环结果的输出进行排序,以便具有特定条件的元素(某些具有值的子元素)被排序到结果的顶部,在这些元素之后,按XML的自上而下的顺序排序。
我的示例XML如下所示
<waybill>
<shipment>
<parcel>
<sscc>SSCC1</sscc>
<consignee>Receiver1</consignee>
<date>Date1</date>
<status>Status1</status>
</parcel>
<parcel>
<sscc>SSCC2</sscc>
<consignee>Receiver2</consignee>
<attention>Note2</attention>
</parcel>
<parcel>
<sscc>SSCC3</sscc>
<consignee>Receiver3</consignee>
</parcel>
<parcel>
<sscc>SSCC4</sscc>
<consignee>Receiver4</consignee>
</parcel>
<parcel>
<sscc>SSCC5</sscc>
<consignee>Receiver5</consignee>
<attention>Note5</attention>
<date>Date2</date>
<status>Status2</status>
</parcel>
<parcel>
<sscc>SSCC6</sscc>
<consignee>Receiver6</consignee>
<attention>Note6</attention>
</parcel>
</shipment>
</waybill>
这里的with是将for-each循环生成到层中,这样它将首先列出元素中具有值的循环,然后列出rest。
因此,期望的输出将是:
SSCC2;Receiver2;Note2
SSCC5:Receiver5;Note5
SSCC6;Receiver6;Note6
SSCC1;Receiver1
SSCC3;Receiver3
SSCC4;Receiver4
我一般知道如何构建for-each循环,但是如何定义输出的排序呢?
我只有XSLT 1.0,所以解决方案应该只使用它...
编辑:我的XSLT如下所示:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
<xsl:output method="text" version="1.0" encoding="ISO-8859-1" indent="yes"/>
<xsl:template match="/">
<xsl:for-each select="waybill/shipment/parcel">
<xsl:value-of select="sscc"/>
<xsl:text>;</xsl:text>
<xsl:value-of select="consignee"/>
<xsl:text>;</xsl:text>
<xsl:value-of select="attention"/>
<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
发布于 2020-01-20 16:16:57
尝试:
<xsl:sort select="number(boolean(attention))" data-type="number" order="descending"/>
或者:
<xsl:sort select="count(attention)" data-type="number" order="descending"/>
https://stackoverflow.com/questions/59819012
复制相似问题