gpt4 book ai didi

xml - 递归排序 XML - 仅对内部节点进行排序

转载 作者:行者123 更新时间:2023-12-04 06:31:31 25 4
gpt4 key购买 nike

我需要对示例 XML 进行排序,首先对所有 ShippingPoint 进行排序,然后根据其第一个 ShippingPoint 对 Cargos 进行排序,最后根据其第一个 Cargo 中的第一个 ShippingPoint 对 Transports 进行排序。所以基本上我试图根据它们应该开始的日期对所有传输进行排序。

现在,我找到了一个使用 XSL 递归的解决方案,除了只有 Cargos 和 ShippingPoints 按预期排序 - 最外部的 Transport 节点没有。我想知道我在这里做错了什么。 MSXML (VS2008) 和 Saxon 解析器都给了我完全相同的结果。

XML 代码示例:

<?xml version="1.0" encoding="utf-8"?>
<Transports>
<Transport ID="1893">
<Cargos>
<Cargo ID="1532" >
<ShippingPoints>
<ShippingPoint ID="1600" ArrivesOn="2011-04-07T12:00:00" />
<ShippingPoint ID="1601" ArrivesOn="2011-04-08T12:00:00" />
</ShippingPoints>
</Cargo>
<Cargo ID="1532">
<ShippingPoints>
<ShippingPoint ID="1601" ArrivesOn="2011-03-08T12:00:00" />
<ShippingPoint ID="1600" ArrivesOn="2011-02-07T12:00:00" />
</ShippingPoints>
</Cargo>
</Cargos>
</Transport>

<Transport ID="1891" >
<Cargos>
<Cargo ID="1529" >
<ShippingPoints>
<ShippingPoint ID="1594" ArrivesOn="2011-04-14T12:00:00" />
<ShippingPoint ID="1595" ArrivesOn="2011-04-04T13:00:00" />
</ShippingPoints>
</Cargo>
<Cargo ID="1530" >
<ShippingPoints>
<ShippingPoint ID="1597" ArrivesOn="2011-04-09T18:00:00" />
<ShippingPoint ID="1596" ArrivesOn="2011-04-04T12:00:00" />
</ShippingPoints>
</Cargo>
</Cargos>
</Transport>

<Transport ID="1892">
<Description/>
<Cargos>
<Cargo ID="1531" >
<ShippingPoints>
<ShippingPoint ID="1599" ArrivesOn="2011-04-06T18:00:00" />
<ShippingPoint ID="1598" ArrivesOn="2011-04-05T12:00:00" />
</ShippingPoints>
</Cargo>
<Cargo ID="1531" >
<ShippingPoints>
<ShippingPoint ID="1599" ArrivesOn="2011-04-02T18:00:00" />
<ShippingPoint ID="1598" ArrivesOn="2011-04-03T12:00:00" />
</ShippingPoints>
</Cargo>
</Cargos>
</Transport>
</Transports>

XSLT 代码:
<?xml version="1.0" encoding="UTF-8"?>

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

<xsl:output method="xml" version="1.0" encoding="UTF-8" />

<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>

<xsl:template match="ShippingPoints">
<xsl:copy>
<xsl:apply-templates select="ShippingPoint">
<xsl:sort select="@ArrivesOn" />
</xsl:apply-templates>
</xsl:copy>
</xsl:template>

<xsl:template match="Cargos">
<xsl:copy>
<xsl:apply-templates select="Cargo">
<xsl:sort select="ShippingPoints/ShippingPoint[1]/@ArrivesOn" />
</xsl:apply-templates>
</xsl:copy>
</xsl:template>

<xsl:template match="Transports">
<xsl:copy>
<xsl:apply-templates select="Transport">
<xsl:sort select="Cargos/Cargo[1]/ShippingPoints/ShippingPoint[1]/@ArrivesOn"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

最佳答案

除非我不明白这一点,否则您正在寻找一种按最小值排序的 @ArrivesOn后裔。最短的样式表:

<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates select="node()">
<xsl:sort select="min(.//@ArrivesOn/xs:dateTime(.))"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

输出:
<Transports>
<Transport ID="1893">
<Cargos>
<Cargo ID="1532">
<ShippingPoints>
<ShippingPoint ID="1600" ArrivesOn="2011-02-07T12:00:00"/>
<ShippingPoint ID="1601" ArrivesOn="2011-03-08T12:00:00"/>
</ShippingPoints>
</Cargo>
<Cargo ID="1532">
<ShippingPoints>
<ShippingPoint ID="1600" ArrivesOn="2011-04-07T12:00:00"/>
<ShippingPoint ID="1601" ArrivesOn="2011-04-08T12:00:00"/>
</ShippingPoints>
</Cargo>
</Cargos>
</Transport>
<Transport ID="1892">
<Description/>
<Cargos>
<Cargo ID="1531">
<ShippingPoints>
<ShippingPoint ID="1599" ArrivesOn="2011-04-02T18:00:00"/>
<ShippingPoint ID="1598" ArrivesOn="2011-04-03T12:00:00"/>
</ShippingPoints>
</Cargo>
<Cargo ID="1531">
<ShippingPoints>
<ShippingPoint ID="1598" ArrivesOn="2011-04-05T12:00:00"/>
<ShippingPoint ID="1599" ArrivesOn="2011-04-06T18:00:00"/>
</ShippingPoints>
</Cargo>
</Cargos>
</Transport>
<Transport ID="1891">
<Cargos>
<Cargo ID="1530">
<ShippingPoints>
<ShippingPoint ID="1596" ArrivesOn="2011-04-04T12:00:00"/>
<ShippingPoint ID="1597" ArrivesOn="2011-04-09T18:00:00"/>
</ShippingPoints>
</Cargo>
<Cargo ID="1529">
<ShippingPoints>
<ShippingPoint ID="1595" ArrivesOn="2011-04-04T13:00:00"/>
<ShippingPoint ID="1594" ArrivesOn="2011-04-14T12:00:00"/>
</ShippingPoints>
</Cargo>
</Cargos>
</Transport>
</Transports>

关于xml - 递归排序 XML - 仅对内部节点进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5351367/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com