我需要以一种方式对一个示例XML进行排序,首先对所有ShippingPoints进行排序,然后根据他们的第一个ShippingPoint进行Cargos排序,最后根据第一个Cargo中的第一个ShippingPoint进行运输。所以基本上我试图根据它们应该开始的日期对所有运输进行分类。
现在,我找到了一个使用XSL递归的解决方案,除了只按预期对Cargos和ShippingPoints进行排序 - 最外层的传输节点不是。我想知道我在这里做错了什么。 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>
答案 0 :(得分:3)
除非我不明白这一点,否则你正在寻找一个至少@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>
答案 1 :(得分:0)
如果您想逐步转换,那么您应该使用模式和变量来存储临时结果,如以下XSLT 2.0示例所示:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" indent="yes" encoding="UTF-8" />
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:variable name="t1">
<xsl:apply-templates mode="step1"/>
</xsl:variable>
<xsl:variable name="t2">
<xsl:apply-templates select="$t1/node()" mode="step2"/>
</xsl:variable>
<xsl:apply-templates select="$t2/node()"/>
</xsl:template>
<xsl:template match="@*|node()" mode="#all">
<xsl:copy>
<xsl:apply-templates select="@*, node()" mode="#current"/>
</xsl:copy>
</xsl:template>
<xsl:template match="ShippingPoints" mode="step1">
<xsl:copy>
<xsl:apply-templates select="ShippingPoint">
<xsl:sort select="@ArrivesOn" />
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="Cargos" mode="step2">
<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>
我认为使用XSLT 2.0处理器可以满足您的需求。