如果foll标记中出现负值,我想将其替换为0.00
输入:<TaxAmount>-1.00</TaxAmount>
输出:<TaxAmount>0.00</TaxAmount>
代码段
<xsl:template match="/tXML/Message/Order/OrderLines/OrderLine/TaxDetails/TaxDetail/TaxAmount">
<TaxAmount>
<xsl:value-of select="translate(., '-0123456789','0.00')"/>
</TaxAmount>
</xsl:template>
更新:
使用的XSLT:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="TaxAmount[. < '0']">
<TaxAmount>0.00</TaxAmount>
</xsl:template>
<xsl:template match="/tXML/Message/Order/OrderLines/OrderLine/TaxDetails/TaxDetail/TaxAmount[number(.) < 0]">
<TaxAmount>0.00</TaxAmount>
</xsl:template>
<xsl:template match="/tXML/Message/Order/OrderLines/OrderLine/TaxDetails/TaxDetail/TaxAmount">
<TaxAmount>
<xsl:choose>
<xsl:when test="number(.) < 0">0.00</xsl:when>
<xsl:otherwise>
<xsl:value-of select="."/>
</xsl:otherwise>
</xsl:choose>
</TaxAmount>
</xsl:template>
</xsl:stylesheet>
输入XML:
<tXML>
<Header>
<Source>X</Source>
<Action_Type>Update</Action_Type>
<Reference_ID>9999999</Reference_ID>
<Message_Type>CustomerOrder</Message_Type>
<Company_ID>1</Company_ID>
</Header>
<Message>
<Order>
<OrderNumber>9999999</OrderNumber>
<OrderCaptureDate>06/26/2015 14:59</OrderCaptureDate>
<ExternalOrderNumber>9999999</ExternalOrderNumber>
<OrderType>YYYY</OrderType>
<OrderCurrency>X</OrderCurrency>
<EntryType>NONE</EntryType>
<Confirmed>TRUE</Confirmed>
<PaymentDetails>
<PaymentDetail>
<ExternalPaymentDetailId>X</ExternalPaymentDetailId>
<PaymentMethod>X</PaymentMethod>
<ReqAuthorizationAmount>41.65</ReqAuthorizationAmount>
<AccountNumber>1111111111</AccountNumber>
<ChargeSequence>1</ChargeSequence>
<BillToDetail>
<BillToFirstName>XX</BillToFirstName>
<BillToLastName>XX</BillToLastName>
<BillToAddressLine1>XX</BillToAddressLine1>
<BillToAddressLine2/>
<BillToCity>XX</BillToCity>
<BillToState>x</BillToState>
<BillToPostalCode>x</BillToPostalCode>
<BillToCountry>x</BillToCountry>
<BillToPhone>x</BillToPhone>
<BillToEmail>X</BillToEmail>
</BillToDetail>
</PaymentDetail>
</PaymentDetails>
<ReferenceFields>
<ReferenceField1>x</ReferenceField1>
<ReferenceField10>x</ReferenceField10>
<ReferenceField2/>
<ReferenceField3>4</ReferenceField3>
<ReferenceField4/>
<ReferenceField8>STND</ReferenceField8>
</ReferenceFields>
<OrderLines>
<OrderLine>
<LineNumber>1</LineNumber>
<ExternalLineID>x</ExternalLineID>
<ItemID>x</ItemID>
<Canceled>x</Canceled>
<AllocationInfo>
<RequiredCapacityPerUnit>1</RequiredCapacityPerUnit>
</AllocationInfo>
<PriceInfo>
<Price>41.65</Price>
</PriceInfo>
<Quantity>
<OrderedQty>1</OrderedQty>
<OrderedQtyUOM>ea</OrderedQtyUOM>
<CancelledQty>0</CancelledQty>
</Quantity>
<ShippingInfo>
<RequestedDeliveryBy>11/20/2015 23:59</RequestedDeliveryBy>
<ShipVia>X</ShipVia>
<ShippingAddress>
</ShippingAddress>
</ShippingInfo>
<ChargeDetails>
<ChargeDetail>
</ChargeDetail>
</ChargeDetails>
<TaxDetails>
<TaxDetail>
<TaxAmount>-0.07</TaxAmount>
</TaxDetail>
<TaxDetail>
<TaxAmount>0.01</TaxAmount>
<TaxDTTM>06/26/2015 14:59</TaxDTTM>
</TaxDetail>
</TaxDetails>
</OrderLine>
</OrderLines>
</Order>
</Message>
</tXML>
感谢你的帮助。提前感谢!!
假设您也有身份转换模板,您可以使用:
<xsl:template match="TaxAmount[. < 0]">
<TaxAmount>0.00</TaxAmount>
</xsl:template>
已添加:
以下样式表:
XSLT 1.0
<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:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="TaxAmount[. < 0]">
<TaxAmount>0.00</TaxAmount>
</xsl:template>
</xsl:stylesheet>
当应用于您的示例输入时,将返回:
结果
<?xml version="1.0" encoding="UTF-8"?>
<tXML>
<Header>
<Source>X</Source>
<Action_Type>Update</Action_Type>
<Reference_ID>9999999</Reference_ID>
<Message_Type>CustomerOrder</Message_Type>
<Company_ID>1</Company_ID>
</Header>
<Message>
<Order>
<OrderNumber>9999999</OrderNumber>
<OrderCaptureDate>06/26/2015 14:59</OrderCaptureDate>
<ExternalOrderNumber>9999999</ExternalOrderNumber>
<OrderType>YYYY</OrderType>
<OrderCurrency>X</OrderCurrency>
<EntryType>NONE</EntryType>
<Confirmed>TRUE</Confirmed>
<PaymentDetails>
<PaymentDetail>
<ExternalPaymentDetailId>X</ExternalPaymentDetailId>
<PaymentMethod>X</PaymentMethod>
<ReqAuthorizationAmount>41.65</ReqAuthorizationAmount>
<AccountNumber>1111111111</AccountNumber>
<ChargeSequence>1</ChargeSequence>
<BillToDetail>
<BillToFirstName>XX</BillToFirstName>
<BillToLastName>XX</BillToLastName>
<BillToAddressLine1>XX</BillToAddressLine1>
<BillToAddressLine2/>
<BillToCity>XX</BillToCity>
<BillToState>x</BillToState>
<BillToPostalCode>x</BillToPostalCode>
<BillToCountry>x</BillToCountry>
<BillToPhone>x</BillToPhone>
<BillToEmail>X</BillToEmail>
</BillToDetail>
</PaymentDetail>
</PaymentDetails>
<ReferenceFields>
<ReferenceField1>x</ReferenceField1>
<ReferenceField10>x</ReferenceField10>
<ReferenceField2/>
<ReferenceField3>4</ReferenceField3>
<ReferenceField4/>
<ReferenceField8>STND</ReferenceField8>
</ReferenceFields>
<OrderLines>
<OrderLine>
<LineNumber>1</LineNumber>
<ExternalLineID>x</ExternalLineID>
<ItemID>x</ItemID>
<Canceled>x</Canceled>
<AllocationInfo>
<RequiredCapacityPerUnit>1</RequiredCapacityPerUnit>
</AllocationInfo>
<PriceInfo>
<Price>41.65</Price>
</PriceInfo>
<Quantity>
<OrderedQty>1</OrderedQty>
<OrderedQtyUOM>ea</OrderedQtyUOM>
<CancelledQty>0</CancelledQty>
</Quantity>
<ShippingInfo>
<RequestedDeliveryBy>11/20/2015 23:59</RequestedDeliveryBy>
<ShipVia>X</ShipVia>
<ShippingAddress/>
</ShippingInfo>
<ChargeDetails>
<ChargeDetail/>
</ChargeDetails>
<TaxDetails>
<TaxDetail>
<TaxAmount>0.00</TaxAmount>
</TaxDetail>
<TaxDetail>
<TaxAmount>0.01</TaxAmount>
<TaxDTTM>06/26/2015 14:59</TaxDTTM>
</TaxDetail>
</TaxDetails>
</OrderLine>
</OrderLines>
</Order>
</Message>
</tXML>
这个较短的转换:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="TaxAmount/text()[0 > .]">0.00</xsl:template>
</xsl:stylesheet>
应用于所提供的XML文档时:
<tXML>
<Header>
<Source>X</Source>
<Action_Type>Update</Action_Type>
<Reference_ID>9999999</Reference_ID>
<Message_Type>CustomerOrder</Message_Type>
<Company_ID>1</Company_ID>
</Header>
<Message>
<Order>
<OrderNumber>9999999</OrderNumber>
<OrderCaptureDate>06/26/2015 14:59</OrderCaptureDate>
<ExternalOrderNumber>9999999</ExternalOrderNumber>
<OrderType>YYYY</OrderType>
<OrderCurrency>X</OrderCurrency>
<EntryType>NONE</EntryType>
<Confirmed>TRUE</Confirmed>
<PaymentDetails>
<PaymentDetail>
<ExternalPaymentDetailId>X</ExternalPaymentDetailId>
<PaymentMethod>X</PaymentMethod>
<ReqAuthorizationAmount>41.65</ReqAuthorizationAmount>
<AccountNumber>1111111111</AccountNumber>
<ChargeSequence>1</ChargeSequence>
<BillToDetail>
<BillToFirstName>XX</BillToFirstName>
<BillToLastName>XX</BillToLastName>
<BillToAddressLine1>XX</BillToAddressLine1>
<BillToAddressLine2/>
<BillToCity>XX</BillToCity>
<BillToState>x</BillToState>
<BillToPostalCode>x</BillToPostalCode>
<BillToCountry>x</BillToCountry>
<BillToPhone>x</BillToPhone>
<BillToEmail>X</BillToEmail>
</BillToDetail>
</PaymentDetail>
</PaymentDetails>
<ReferenceFields>
<ReferenceField1>x</ReferenceField1>
<ReferenceField10>x</ReferenceField10>
<ReferenceField2/>
<ReferenceField3>4</ReferenceField3>
<ReferenceField4/>
<ReferenceField8>STND</ReferenceField8>
</ReferenceFields>
<OrderLines>
<OrderLine>
<LineNumber>1</LineNumber>
<ExternalLineID>x</ExternalLineID>
<ItemID>x</ItemID>
<Canceled>x</Canceled>
<AllocationInfo>
<RequiredCapacityPerUnit>1</RequiredCapacityPerUnit>
</AllocationInfo>
<PriceInfo>
<Price>41.65</Price>
</PriceInfo>
<Quantity>
<OrderedQty>1</OrderedQty>
<OrderedQtyUOM>ea</OrderedQtyUOM>
<CancelledQty>0</CancelledQty>
</Quantity>
<ShippingInfo>
<RequestedDeliveryBy>11/20/2015 23:59</RequestedDeliveryBy>
<ShipVia>X</ShipVia>
<ShippingAddress>
</ShippingAddress>
</ShippingInfo>
<ChargeDetails>
<ChargeDetail>
</ChargeDetail>
</ChargeDetails>
<TaxDetails>
<TaxDetail>
<TaxAmount>-0.07</TaxAmount>
</TaxDetail>
<TaxDetail>
<TaxAmount>0.01</TaxAmount>
<TaxDTTM>06/26/2015 14:59</TaxDTTM>
</TaxDetail>
</TaxDetails>
</OrderLine>
</OrderLines>
</Order>
</Message>
</tXML>
生成所需的正确结果:
<tXML>
<Header>
<Source>X</Source>
<Action_Type>Update</Action_Type>
<Reference_ID>9999999</Reference_ID>
<Message_Type>CustomerOrder</Message_Type>
<Company_ID>1</Company_ID>
</Header>
<Message>
<Order>
<OrderNumber>9999999</OrderNumber>
<OrderCaptureDate>06/26/2015 14:59</OrderCaptureDate>
<ExternalOrderNumber>9999999</ExternalOrderNumber>
<OrderType>YYYY</OrderType>
<OrderCurrency>X</OrderCurrency>
<EntryType>NONE</EntryType>
<Confirmed>TRUE</Confirmed>
<PaymentDetails>
<PaymentDetail>
<ExternalPaymentDetailId>X</ExternalPaymentDetailId>
<PaymentMethod>X</PaymentMethod>
<ReqAuthorizationAmount>41.65</ReqAuthorizationAmount>
<AccountNumber>1111111111</AccountNumber>
<ChargeSequence>1</ChargeSequence>
<BillToDetail>
<BillToFirstName>XX</BillToFirstName>
<BillToLastName>XX</BillToLastName>
<BillToAddressLine1>XX</BillToAddressLine1>
<BillToAddressLine2/>
<BillToCity>XX</BillToCity>
<BillToState>x</BillToState>
<BillToPostalCode>x</BillToPostalCode>
<BillToCountry>x</BillToCountry>
<BillToPhone>x</BillToPhone>
<BillToEmail>X</BillToEmail>
</BillToDetail>
</PaymentDetail>
</PaymentDetails>
<ReferenceFields>
<ReferenceField1>x</ReferenceField1>
<ReferenceField10>x</ReferenceField10>
<ReferenceField2/>
<ReferenceField3>4</ReferenceField3>
<ReferenceField4/>
<ReferenceField8>STND</ReferenceField8>
</ReferenceFields>
<OrderLines>
<OrderLine>
<LineNumber>1</LineNumber>
<ExternalLineID>x</ExternalLineID>
<ItemID>x</ItemID>
<Canceled>x</Canceled>
<AllocationInfo>
<RequiredCapacityPerUnit>1</RequiredCapacityPerUnit>
</AllocationInfo>
<PriceInfo>
<Price>41.65</Price>
</PriceInfo>
<Quantity>
<OrderedQty>1</OrderedQty>
<OrderedQtyUOM>ea</OrderedQtyUOM>
<CancelledQty>0</CancelledQty>
</Quantity>
<ShippingInfo>
<RequestedDeliveryBy>11/20/2015 23:59</RequestedDeliveryBy>
<ShipVia>X</ShipVia>
<ShippingAddress>
</ShippingAddress>
</ShippingInfo>
<ChargeDetails>
<ChargeDetail>
</ChargeDetail>
</ChargeDetails>
<TaxDetails>
<TaxDetail>
<TaxAmount>0.00</TaxAmount>
</TaxDetail>
<TaxDetail>
<TaxAmount>0.01</TaxAmount>
<TaxDTTM>06/26/2015 14:59</TaxDTTM>
</TaxDetail>
</TaxDetails>
</OrderLine>
</OrderLines>
</Order>
</Message>
</tXML>
解释:
此解决方案使用最通用、功能最强大的XSLT设计模式来覆盖标识规则。
覆盖模板匹配作为TaxAmount
元素的子元素并且其字符串值(文本节点的)在转换为数字时小于0
的任何文本节点。它只是用一个新节点替换匹配的节点,该节点的字符串值为字符串0.00
。