如何将数组中的一个元素与另一个数组中的所有其他元素进行比较?并为第一个数组中的 n 个元素重复它?使用 XSLT

How to compare one element in an array with all the other elements in another array? And repeating it for n elements in the first array? Using XSLT

在下面的 XML 中,我需要将 OrderShipmentItemsLineItemNbr 值(示例:000010)与所有 OrderItems/ScheduleLineItemsLineItemNbr 进行比较(000010, 000020, 000030, 000040) 在第一次迭代中。

对所有 OrderShipmentItem LineItemNbr (000010,000020) 重复此比较。根据给出的 xml 应该是 2 次。

如果它们相等,则将 DelivereQty 值映射到订单元素的 Quantity 属性,将 customerNbr 映射到订单元素的 customercontact 属性。

<Orders>
  <OrderEvent>
    <SAPOrderLineNbrs>
      <LineItemNbr>000010</LineItemNbr>
      <LineItemNbr>000020</LineItemNbr>
    </SAPOrderLineNbrs>
  </OrderEvent>
  <OrderHeader>
    <OrderItems>
      <LineItemNbr>000010</LineItemNbr>
      <ScheduleLineItems>
        <LineItemNbr>000010</LineItemNbr>
        <CustomerNbr>22222222</CustomerNbr>
      </ScheduleLineItems>
    </OrderItems>
    <OrderItems>
      <LineItemNbr>000020</LineItemNbr>
      <ScheduleLineItems>
        <LineItemNbr>000020</LineItemNbr>
        <CustomerNbr>11111111</CustomerNbr>
      </ScheduleLineItems>
    </OrderItems>
    <OrderItems>
      <LineItemNbr>000030</LineItemNbr>
      <ScheduleLineItems>
        <LineItemNbr>000030</LineItemNbr>
        <CustomerNbr>33333333</CustomerNbr>
      </ScheduleLineItems>
    </OrderItems>
    <OrderItems>
      <LineItemNbr>000040</LineItemNbr>
      <ScheduleLineItems>
        <LineItemNbr>000040</LineItemNbr>
        <CustomerNbr />
      </ScheduleLineItems>
    </OrderItems>
    <OrderShipment>
      <OrderShipmentItems>
        <LineItemNbr>000010</LineItemNbr>
        <OrderShipmentScheduleItems>
          <DeliveredQty>1.000</DeliveredQty>
        </OrderShipmentScheduleItems>
      </OrderShipmentItems>
      <OrderShipmentItems>
        <DeliveredQty>2.000</DeliveredQty>
        <LineItemNbr>000020</LineItemNbr>
        <OrderShipmentScheduleItems>
          <DeliveredQty>2.000</DeliveredQty>
        </OrderShipmentScheduleItems>
      </OrderShipmentItems>
    </OrderShipment>
  </OrderHeader>
</Orders>

我的输出应该包含与 LineItemNbr 相同数量的订单(在上面它是 2)并且我们需要检查 OrderShipmentItems 的 LineItemNbr 值(示例:000010)和所有 OrderItems/ScheduleLineItems 的 LineItemNbr(000010, 000020, 000030, 000040) 每个 lineItemNbr.

如果它们相等,那么我们需要将 DeliveredQty 值映射为 Quantity,将 CustomerNbr 映射为 CustomerContact。

输出应该是:

 <Orders> 
    <Order Quantity="1.000" CustomerContact="222222222"/>
    <Order Quantity="2.000" CustomerContact="111111111"/>
  <Orders>

请帮帮我

我试过的是下面的 xslt,但它只比较第一个列表元素值。

    <xsl:for-each select="/*/OrderHeader/OrderShipment/OrderShipmentItems">
<order>
      <xsl:for-each select="/*/OrderHeader/OrderItems/ScheduleLineItems">
        <xsl:if test="/*/OrderHeader/OrderShipment/OrderShipmentItems/LineItemNbr = /*/OrderHeader/OrderItems/ScheduleLineItems/LineItemNbr">
          <xsl:attribute name="Quantity">
            <xsl:value-of select="/*/OrderHeader/OrderShipment/OrderShipmentItems/OrderShipmentScheduleItems/DeliveredQty" />
          </xsl:attribute>
          <xsl:attribute name="CustomerContact">
            <xsl:value-of select="/*/OrderHeader/OrderItems/ScheduleLineItems/CustomerNbr" />
          </xsl:attribute>
        </xsl:if>
      </xsl:for-each>
</order>
    </xsl:for-each>

如果您显示预期的输出,您的问题会更清楚。看看这个简化的例子是否有帮助:

XSLT 1.0/2.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:key name="shipment" match="OrderShipmentItems" use="LineItemNbr" />

<xsl:template match="/Orders">
    <result>
        <xsl:for-each select="OrderHeader/OrderItems/ScheduleLineItems">
            <item>
                <xsl:copy-of select="LineItemNbr | CustomerNbr"/>
                <xsl:copy-of select="key('shipment', LineItemNbr)/OrderShipmentScheduleItems/DeliveredQty"/>
            </item> 
        </xsl:for-each>     
    </result>   
</xsl:template>

</xsl:stylesheet>

应用于您输入的 XML 示例,这将产生:

结果

<?xml version="1.0" encoding="UTF-8"?>
<result>
  <item>
    <LineItemNbr>000010</LineItemNbr>
    <CustomerNbr>22222222</CustomerNbr>
    <DeliveredQty>1.000</DeliveredQty>
  </item>
  <item>
    <LineItemNbr>000020</LineItemNbr>
    <CustomerNbr>11111111</CustomerNbr>
    <DeliveredQty>2.000</DeliveredQty>
  </item>
  <item>
    <LineItemNbr>000030</LineItemNbr>
    <CustomerNbr>33333333</CustomerNbr>
  </item>
  <item>
    <LineItemNbr>000040</LineItemNbr>
    <CustomerNbr/>
  </item>
</result>

-- 添加--

在相反的方向应用相同的原理是相当微不足道的:

XSLT 1.0/2.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:key name="order-item" match="OrderItems" use="LineItemNbr" />

<xsl:template match="/Orders">
    <Orders>
        <xsl:for-each select="OrderHeader/OrderShipment/OrderShipmentItems">
            <Order Quantity="{OrderShipmentScheduleItems/DeliveredQty}" CustomerContact="{key('order-item', LineItemNbr)/ScheduleLineItems/CustomerNbr}"/>
        </xsl:for-each>     
    </Orders>   
</xsl:template>

</xsl:stylesheet>

结果

<?xml version="1.0" encoding="UTF-8"?>
<Orders>
  <Order Quantity="1.000" CustomerContact="22222222"/>
  <Order Quantity="2.000" CustomerContact="11111111"/>
</Orders>