我似乎无法弄清楚如何成功实现这个基本的xsl。我想过滤掉 xml 中的所有 SalesLines 元素,其 SalesOrderStatus 值<> SalesOrder (不要保留 Sent/CancelBySales(。
源 XML:
<?xml version="1.0" encoding="UTF-8"?><Envelope xmlns="http://schemas.microsoft.com/dynamics/2008/01/documents/Message">
<Header>
<MessageId>{9CEDBBC7-5AF3-4A8B-AC68-49F1E0D8402A}</MessageId>
<SourceEndpoint>hwf</SourceEndpoint>
<DestinationEndpoint>HWF_ORDRSP</DestinationEndpoint>
<Action>http://schemas.microsoft.com/dynamics/2008/01/services/SalesSalesOrderService/read</Action>
</Header>
<Body>
<MessageParts>
<SalesOrder xmlns="http://schemas.microsoft.com/dynamics/2008/01/documents/SalesOrder">
<SalesTable class="entity">
<CustAccount>00003</CustAccount>
<DeliveryDate>2019-10-21</DeliveryDate>
<POMOrderId>0002539819</POMOrderId>
<PurchOrderFormNum>4500143886</PurchOrderFormNum>
<SalesId>QN00000949</SalesId>
<SalesOrderStatus>SalesOrder</SalesOrderStatus>
<SalesLine class="entity">
<ExtLineNum>10</ExtLineNum>
<ItemId>08201</ItemId>
<SalesId>QN00000949</SalesId>
<SalesOrderStatus>SalesOrder</SalesOrderStatus>
<SalesQty>1320.00</SalesQty>
<SalesUnit>st</SalesUnit>
<ShippingDateConfirmed>2019-10-21</ShippingDateConfirmed>
</SalesLine>
<SalesLine class="entity">
<ExtLineNum>20</ExtLineNum>
<ItemId>08236</ItemId>
<SalesId>QN00000949</SalesId>
<SalesOrderStatus>Sent</SalesOrderStatus>
<SalesQty>1296.00</SalesQty>
<SalesUnit>st</SalesUnit>
<ShippingDateConfirmed>2019-10-21</ShippingDateConfirmed>
</SalesLine>
<SalesLine class="entity">
<ExtLineNum>30</ExtLineNum>
<ItemId>08238</ItemId>
<SalesId>QN00000949</SalesId>
<SalesOrderStatus>SalesOrder</SalesOrderStatus>
<SalesQty>1296.00</SalesQty>
<SalesUnit>st</SalesUnit>
<ShippingDateConfirmed>2019-10-21</ShippingDateConfirmed>
</SalesLine>
<SalesLine class="entity">
<ExtLineNum>40</ExtLineNum>
<ItemId>08254</ItemId>
<SalesId>QN00000949</SalesId>
<SalesOrderStatus>CancelledBySales</SalesOrderStatus>
<SalesQty>1320.00</SalesQty>
<SalesUnit>st</SalesUnit>
<ShippingDateConfirmed>2019-10-21</ShippingDateConfirmed>
</SalesLine>
</SalesTable>
</SalesOrder>
</MessageParts>
</Body>
</Envelope>
预期成果:
<?xml version="1.0" encoding="UTF-8"?>
<Envelope xmlns="http://schemas.microsoft.com/dynamics/2008/01/documents/Message">
<Header>
<MessageId>{9CEDBBC7-5AF3-4A8B-AC68-49F1E0D8402A}</MessageId>
<SourceEndpoint>hwf</SourceEndpoint>
<DestinationEndpoint>HWF_ORDRSP</DestinationEndpoint>
<Action>http://schemas.microsoft.com/dynamics/2008/01/services/SalesSalesOrderService/read</Action>
</Header>
<Body>
<MessageParts xmlns="http://schemas.microsoft.com/dynamics/2008/01/documents/Message">
<SalesOrder xmlns="http://schemas.microsoft.com/dynamics/2008/01/documents/SalesOrder">
<SalesTable class="entity">
<CustAccount>00003</CustAccount>
<DeliveryDate>2019-10-21</DeliveryDate>
<POMOrderId>0002539819</POMOrderId>
<PurchOrderFormNum>4500143886</PurchOrderFormNum>
<SalesId>QN00000949</SalesId>
<SalesOrderStatus>SalesOrder</SalesOrderStatus>
<SalesLine class="entity">
<ExtLineNum>10</ExtLineNum>
<ItemId>08201</ItemId>
<SalesId>QN00000949</SalesId>
<SalesOrderStatus>SalesOrder</SalesOrderStatus>
<SalesQty>1320.00</SalesQty>
<SalesUnit>st</SalesUnit>
<ShippingDateConfirmed>2019-10-21</ShippingDateConfirmed>
</SalesLine>
<SalesLine class="entity">
<ExtLineNum>30</ExtLineNum>
<ItemId>08238</ItemId>
<SalesId>QN00000949</SalesId>
<SalesOrderStatus>SalesOrder</SalesOrderStatus>
<SalesQty>1296.00</SalesQty>
<SalesUnit>st</SalesUnit>
<ShippingDateConfirmed>2019-10-21</ShippingDateConfirmed>
</SalesLine>
</SalesTable>
</SalesOrder>
</MessageParts>
</Body>
</Envelope>
到目前为止,我已经创建了XSL:
<!-- Michael van den Dool 18-10-2019 17:46 filter saleslines -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://schemas.microsoft.com/dynamics/2008/01/documents/SalesOrder" exclude-result-prefixes="xs" version="2.0">
<!-- Copy everything -->
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*"/>
</xsl:copy>
</xsl:template>
<!-- Match elements which need to be filtered and do not copy them to the output -->
<xsl:template match="SalesTable/SalesLine[not(SalesTable/SalesLine/SalesOrderStatus/text() = 'SalesOrder')]"/>
</xsl:stylesheet>
运行 xsl 时,不会过滤掉任何内容。我认为它在命名空间/层次结构的某个地方。但我仍然是XSL的业余爱好者,对不起..
下面是一个简单、简短的 XSLT 1.0 解决方案。
您的主要问题是未能在匹配模式中指定元素的命名空间 - 它们不在"无命名空间"中
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:x="http://schemas.microsoft.com/dynamics/2008/01/documents/SalesOrder">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="x:SalesLine[not(x:SalesOrderStatus = 'SalesOrder')]"/>
</xsl:stylesheet>
在提供的 XML 文档上应用此转换时:
<Envelope xmlns="http://schemas.microsoft.com/dynamics/2008/01/documents/Message">
<Header>
<MessageId>{9CEDBBC7-5AF3-4A8B-AC68-49F1E0D8402A}</MessageId>
<SourceEndpoint>hwf</SourceEndpoint>
<DestinationEndpoint>HWF_ORDRSP</DestinationEndpoint>
<Action>http://schemas.microsoft.com/dynamics/2008/01/services/SalesSalesOrderService/read</Action>
</Header>
<Body>
<MessageParts>
<SalesOrder xmlns="http://schemas.microsoft.com/dynamics/2008/01/documents/SalesOrder">
<SalesTable class="entity">
<CustAccount>00003</CustAccount>
<DeliveryDate>2019-10-21</DeliveryDate>
<POMOrderId>0002539819</POMOrderId>
<PurchOrderFormNum>4500143886</PurchOrderFormNum>
<SalesId>QN00000949</SalesId>
<SalesOrderStatus>SalesOrder</SalesOrderStatus>
<SalesLine class="entity">
<ExtLineNum>10</ExtLineNum>
<ItemId>08201</ItemId>
<SalesId>QN00000949</SalesId>
<SalesOrderStatus>SalesOrder</SalesOrderStatus>
<SalesQty>1320.00</SalesQty>
<SalesUnit>st</SalesUnit>
<ShippingDateConfirmed>2019-10-21</ShippingDateConfirmed>
</SalesLine>
<SalesLine class="entity">
<ExtLineNum>20</ExtLineNum>
<ItemId>08236</ItemId>
<SalesId>QN00000949</SalesId>
<SalesOrderStatus>Sent</SalesOrderStatus>
<SalesQty>1296.00</SalesQty>
<SalesUnit>st</SalesUnit>
<ShippingDateConfirmed>2019-10-21</ShippingDateConfirmed>
</SalesLine>
<SalesLine class="entity">
<ExtLineNum>30</ExtLineNum>
<ItemId>08238</ItemId>
<SalesId>QN00000949</SalesId>
<SalesOrderStatus>SalesOrder</SalesOrderStatus>
<SalesQty>1296.00</SalesQty>
<SalesUnit>st</SalesUnit>
<ShippingDateConfirmed>2019-10-21</ShippingDateConfirmed>
</SalesLine>
<SalesLine class="entity">
<ExtLineNum>40</ExtLineNum>
<ItemId>08254</ItemId>
<SalesId>QN00000949</SalesId>
<SalesOrderStatus>CancelledBySales</SalesOrderStatus>
<SalesQty>1320.00</SalesQty>
<SalesUnit>st</SalesUnit>
<ShippingDateConfirmed>2019-10-21</ShippingDateConfirmed>
</SalesLine>
</SalesTable>
</SalesOrder>
</MessageParts>
</Body>
</Envelope>
生成所需的正确结果:
<Envelope xmlns="http://schemas.microsoft.com/dynamics/2008/01/documents/Message">
<Header>
<MessageId>{9CEDBBC7-5AF3-4A8B-AC68-49F1E0D8402A}</MessageId>
<SourceEndpoint>hwf</SourceEndpoint>
<DestinationEndpoint>HWF_ORDRSP</DestinationEndpoint>
<Action>http://schemas.microsoft.com/dynamics/2008/01/services/SalesSalesOrderService/read</Action>
</Header>
<Body>
<MessageParts>
<SalesOrder xmlns="http://schemas.microsoft.com/dynamics/2008/01/documents/SalesOrder">
<SalesTable class="entity">
<CustAccount>00003</CustAccount>
<DeliveryDate>2019-10-21</DeliveryDate>
<POMOrderId>0002539819</POMOrderId>
<PurchOrderFormNum>4500143886</PurchOrderFormNum>
<SalesId>QN00000949</SalesId>
<SalesOrderStatus>SalesOrder</SalesOrderStatus>
<SalesLine class="entity">
<ExtLineNum>10</ExtLineNum>
<ItemId>08201</ItemId>
<SalesId>QN00000949</SalesId>
<SalesOrderStatus>SalesOrder</SalesOrderStatus>
<SalesQty>1320.00</SalesQty>
<SalesUnit>st</SalesUnit>
<ShippingDateConfirmed>2019-10-21</ShippingDateConfirmed>
</SalesLine>
<SalesLine class="entity">
<ExtLineNum>30</ExtLineNum>
<ItemId>08238</ItemId>
<SalesId>QN00000949</SalesId>
<SalesOrderStatus>SalesOrder</SalesOrderStatus>
<SalesQty>1296.00</SalesQty>
<SalesUnit>st</SalesUnit>
<ShippingDateConfirmed>2019-10-21</ShippingDateConfirmed>
</SalesLine>
</SalesTable>
</SalesOrder>
</MessageParts>
</Body>
</Envelope>