Элементы фильтра XSL на основе значения узла - PullRequest
1 голос
/ 18 октября 2019

Я не могу понять, как я могу успешно реализовать этот базовый xsl. Я хочу отфильтровать все элементы SalesLines в моем xml-файле со значением SalesOrderStatus <> SalesOrder (Не сохранять Sent / CancelledBySales).

Исходный 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, извините ..

1 Ответ

1 голос
/ 19 октября 2019

Вот простое, короткое решение 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>
...