首页 > 解决方案 > 使用 XSLT 解析 XML 时如何处理多个小数?

问题描述

我在零售方面工作,我们有MaxLineStatus表示客户所下订单的当前状态。作为此过程的一部分,我有一个 XML 需要传递给 XSLT 进行转换。我的 XSLT 检查了 MaxLineStatus < 3700.7777,并且 MaxLineStatus 的值在输入 xml 中作为 3200.200.100 传递。在理想情况下,3200.200.100小于3700.7777,作为回报,它应该满足条件并填充 orderLine 节点。

每当我在 xml 中传递 3200.200 代替 3200.200.100 时,我都会得到所需的输出。我什至尝试将 xslt 值存储在某个变量中,然后比较该值,但它仍然无法正常工作。

我正在比较以下代码中的值:

<xsl:attribute name="OrderLineCount"><xsl:value-of select="count(./OrderLine[(@MaxLineStatus &lt; 3700.7777) and (@MaxLineStatus!='3200.150') and (@MaxLineStatus!='1400')])" /></xsl:attribute>

感谢您的帮助,很抱歉以这种方式发布 xml 和 xslt。我遇到了一些缩进相同的问题。请原谅。

XML:

<OrderRelease APIName="sendReleaseModificationUpdateToILS" CarrierServiceCode="" DeliveryMethod="DEL" DocumentType="0003" EnterpriseCode="RETAIL" MFLocationId="70" NotificationType=" " OrderDate="20190410T06:49:07" OrderHeaderKey="2019041006490764562203" OrderLineCount="1" PriorityCode="" PriorityNumber="0" ProNumber="" ReleaseNo="1" RewardZoneIndicator="N" SCAC="" SalesOrderNo="1119100622586" ShipNode="BBY_9034" ShippingType="" WorkOrderKey="2019041006493464562225"><Shipnode Localecode="en_US_CST" NodeType="DDC" ReceiptProcessingTime="60.00" ShipNode="BBY_9034"><Extn FulfilmentDateBufferTime="0" LocationID="9034" MFLocationId="70" MobileSAS="N" /></Shipnode><Order CarrierServiceCode="" CustomerEMailID="RACHAKONDA.SATYASRINIVAS@BESTBUY.COM" DocumentType="0001" DraftOrderFlag="N" EnterpriseCode="RETAIL" MaxOrderStatus="3200.200.100" MinOrderStatus="1100.100" NotificationType=" " OrderDate="20190410T06:49:07" OrderHeaderKey="2019041006490764562203" OrderNo="1119100622586" OrderPurpose="" PriorityCode="" PriorityNumber="0" ReturnOrderHeaderKeyForExchange=" " SCAC="" SellerOrganizationCode="BBY_281" ShipNode=" " Status="Partially Awaiting Pickup Date" isHistory="N" /><OrderLine CarrierServiceCode="" ConditionVariable1="" ConditionVariable2="" DeliveryMethod="DEL" DependentOnLineKey=" " DerivedFromOrderHeaderKey="2018012921180556404483" DerivedFromOrderLineKey="2018012921180556404485" FulfillmentType="DELIVERY" HasDerivedChild="" LineType="RETURN" MaxLineStatus="3200.200.100" MaxLineStatusDesc="Awaiting Pickup Date" MinLineStatus="3200.200.100" MinLineStatusDesc="Awaiting Pickup Date" OpenQty="1" OrderLineKey="2019041006490764562204" OrderedQty="1" PrimeLineNo="1" ProcureFromNode="" PromisedApptEndDate="20190413T19:00:00" PromisedApptStartDate="20190413T07:00:00" SCAC="" ShipNode="BBY_9034" ShipToKey=" " Status="Awaiting Pickup Date" SubLineNo="1"><Extn ExpectedShipDate="20190410T00:00:00" StreetDate="19000101T00:00:00"><EOMSOrderLineList><EOMSOrderLine ExpectedApptEndDate="20190413T19:00:00" ExpectedApptStartDate="20190413T07:00:00" HingeSettings="" LineShipNodeWMS="WMS" OrderLineKey="2019041006490764562204" SellingStoreId="281" /></EOMSOrderLineList></Extn><Item CustomerItem="3519796" CustomerItemDesc="NINTENDO DS UNIVERSAL GAME SELECTOR CASE" ItemID="1640461" ManufacturerItem="52LB5D" ProductClass="" ProductLine="9.99.224" UnitOfMeasure="EACH" /><Notes NumberOfNotes="0" /><ParentOrderLineRelationships /></OrderLine></OrderRelease>

XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" /><xsl:template match="/OrderRelease"><xsl:copy><xsl:copy-of select="@*" /><xsl:attribute name="MFLocationId"><xsl:value-of select="./Shipnode/Extn/@MFLocationId" /></xsl:attribute><Extn><xsl:attribute name="ReleaseSeqNo"><xsl:value-of select="./Extn/@ReleaseSeqNo" /></xsl:attribute></Extn><xsl:attribute name="OrderLineCount"><xsl:value-of select="count(./OrderLine[(@MaxLineStatus &lt; 3700.7777) and (@MaxLineStatus!='3200.150') and (@MaxLineStatus!='1400')])" /></xsl:attribute><xsl:for-each select="./OrderLine[(@MaxLineStatus &lt; 3700.7777) and (@MaxLineStatus!='3200.150') and (@MaxLineStatus!='1400')]"><OrderLine><xsl:copy-of select="./@*" /><xsl:copy-of select="./OrderLineInvAttRequest" /><Item><xsl:copy-of select="./Item/@*" /><Extn><xsl:attribute name="BrandItemID"><xsl:value-of select="./Item/@CustomerItem" /></xsl:attribute></Extn></Item><Notes><xsl:copy-of select="./Notes/*" /></Notes><ParentOrderLineRelationships><xsl:copy-of select="./ParentOrderLineRelationships/*" /></ParentOrderLineRelationships><Extn><xsl:copy-of select="./Extn/*" /></Extn></OrderLine></xsl:for-each><Notes><xsl:for-each select="./Notes/Note"><xsl:copy-of select="current()" /></xsl:for-each></Notes><Order><xsl:attribute name="OrderPurpose"><xsl:value-of select="./Order/@OrderPurpose" /></xsl:attribute><xsl:attribute name="ReturnOrderHeaderKeyForExchange"><xsl:value-of select="./Order/@ReturnOrderHeaderKeyForExchange" /></xsl:attribute><xsl:attribute name="CustomerEMailID"><xsl:value-of select="./Order/@CustomerEMailID" /></xsl:attribute><xsl:attribute name="SellerOrganizationCode"><xsl:value-of select="./Order/@SellerOrganizationCode" /></xsl:attribute><Extn><xsl:attribute name="BillingPhoneNo"><xsl:value-of select="./Order/Extn/@BillingPhoneNo" /></xsl:attribute></Extn></Order><xsl:copy-of select="./PersonInfoShipTo" /></xsl:copy></xsl:template>

实际结果:

<OrderRelease APIName="sendReleaseModificationUpdateToILS" CarrierServiceCode="" DeliveryMethod="DEL" DocumentType="0003" EnterpriseCode="RETAIL" MFLocationId="70" NotificationType=" " OrderDate="20190410T06:49:07" OrderHeaderKey="2019041006490764562203" OrderLineCount="1" PriorityCode="" PriorityNumber="0" ProNumber="" ReleaseNo="1" RewardZoneIndicator="N" SCAC="" SalesOrderNo="1119100622586" ShipNode="BBY_9034" ShippingType="" WorkOrderKey="2019041006493464562225"><Extn ReleaseSeqNo="" /><Notes></Notes><Order OrderPurpose="" ReturnOrderHeaderKeyForExchange=" " CustomerEMailID="RACHAKONDA.SATYASRINIVAS@BESTBUY.COM" SellerOrganizationCode="BBY_281"><Extn BillingPhoneNo="" /></Order></OrderRelease>

预期结果:

<OrderRelease APIName="sendReleaseModificationUpdateToILS" CarrierServiceCode="" DeliveryMethod="DEL" DocumentType="0003" EnterpriseCode="RETAIL" MFLocationId="70" NotificationType=" " OrderDate="20190410T06:49:07" OrderHeaderKey="2019041006490764562203" OrderLineCount="1" PriorityCode="" PriorityNumber="0" ProNumber="" ReleaseNo="1" RewardZoneIndicator="N" SCAC="" SalesOrderNo="1119100622586" ShipNode="BBY_9034" ShippingType="" WorkOrderKey="2019041006493464562225"><Extn ReleaseSeqNo="" /><OrderLine CarrierServiceCode="" ConditionVariable1="" ConditionVariable2="" DeliveryMethod="DEL" DependentOnLineKey=" " DerivedFromOrderHeaderKey="2018012921180556404483" DerivedFromOrderLineKey="2018012921180556404485" FulfillmentType="DELIVERY" HasDerivedChild="" LineType="RETURN" MaxLineStatus="3200.200.100" MaxLineStatusDesc="Awaiting Pickup Date" MinLineStatus="3200.200.100" MinLineStatusDesc="Awaiting Pickup Date" OpenQty="1" OrderLineKey="2019041006490764562204" OrderedQty="1" PrimeLineNo="1" ProcureFromNode="" PromisedApptEndDate="20190413T19:00:00" PromisedApptStartDate="20190413T07:00:00" SCAC="" ShipNode="BBY_9034" ShipToKey=" " Status="Awaiting Pickup Date" SubLineNo="1"><Item CustomerItem="3519796" CustomerItemDesc="NINTENDO DS UNIVERSAL GAME SELECTOR CASE" ItemID="1640461" ManufacturerItem="52LB5D" ProductClass="" ProductLine="9.99.224" UnitOfMeasure="EACH"><Extn BrandItemID="3519796" /></Item><Notes></Notes><ParentOrderLineRelationships></ParentOrderLineRelationships><Extn><EOMSOrderLineList><EOMSOrderLine ExpectedApptEndDate="20190413T19:00:00" ExpectedApptStartDate="20190413T07:00:00" HingeSettings="" LineShipNodeWMS="WMS" OrderLineKey="2019041006490764562204" SellingStoreId="281" /></EOMSOrderLineList></Extn></OrderLine><Notes></Notes><Order OrderPurpose="" ReturnOrderHeaderKeyForExchange=" " CustomerEMailID="RACHAKONDA.SATYASRINIVAS@BESTBUY.COM" SellerOrganizationCode="BBY_281"><Extn BillingPhoneNo="" /></Order></OrderRelease>

标签: javaxmlxsltxml-parsingxslt-1.0

解决方案


<在 XSLT 1.0 中,比较字符串和>关系的唯一方法是排序;<and运算符>始终转换为数字(如果值不能转换为数字,则将其视为 NaN)。

理想情况下,前进到更新的 XSLT 版本(1.0 已经很老了)。

如果你不能做到这一点,那么这将是一个复杂的解决方法。三种可能的方法:

(a) 找到某种方法将这些“点分十进制”值转换为正确排序的数字。(例如,将每个组件填充到标准位数,然后去除点)。

(b) 对值进行排序,分配序号,通过查看序号来检验一个值是否满足相关条件。

(c) 以 Java 等外部语言调用扩展函数进行比较。


推荐阅读