2017-04-20 55 views
-1

以下是我的輸入記錄。如果有多個停靠點,每個LoadNumber可以重複多次。我想轉換如下:嘗試在檢測到重複鍵時合併XML

<Load> 
    <LoadData> 
     <ShipmentNumber>24609038</ShipmentNumber> 
     <TrackingType>FK </TrackingType> 
     <LoadNumber>90621234</LoadNumber> 
     <StopSequence>1</StopSequence> 
     <CarrierNumber>22104</CarrierNumber> 
     <SCAC>WOLH</SCAC> 
     <ShippingName>Somewhere DC        </ShippingName> 
     <ShippingAddress>115 S MAIN ST      </ShippingAddress> 
     <ShippingCity>CLEVELAND     </ShippingCity> 
     <ShippingState>OH </ShippingState> 
     <ShippingZip>54915  </ShippingZip> 
     <ShippingCountry>US </ShippingCountry> 
     <PromisedDate>116321</PromisedDate> 
     <PromisedTime>153000</PromisedTime> 
     <DestinationName>COMPANY1   </DestinationName> 
     <DestinationAddress>4000 3RD AVE N     </DestinationAddress> 
     <DestinationCity>MINNEAPOLIS   </DestinationCity> 
     <DestinationState>MN</DestinationState> 
     <DestinationZip>55443  </DestinationZip> 
     <DestinationCountry>US</DestinationCountry> 
     <ShippingDate>116320</ShippingDate> 
     <ShippingTime>230000</ShippingTime> 
    </LoadData> 
     <LoadData> 
     <ShipmentNumber>24615865</ShipmentNumber> 
     <TrackingType>FK </TrackingType> 
     <LoadNumber>90621234</LoadNumber> 
     <StopSequence>2</StopSequence> 
     <CarrierNumber>22104</CarrierNumber> 
     <SCAC>WOLH</SCAC> 
     <ShippingName>Somewhere DC        </ShippingName> 
     <ShippingAddress>115 S MAIN ST      </ShippingAddress> 
     <ShippingCity>CLEVELAND     </ShippingCity> 
     <ShippingState>OH </ShippingState> 
     <ShippingZip>54912  </ShippingZip> 
     <ShippingCountry>US </ShippingCountry> 
     <PromisedDate>116321</PromisedDate> 
     <PromisedTime>133000</PromisedTime> 
     <DestinationName>COMPANY 2    </DestinationName> 
     <DestinationAddress>3 KENT ST      </DestinationAddress> 
     <DestinationCity>SHOREVIEW    </DestinationCity> 
     <DestinationState>MN</DestinationState> 
     <DestinationZip>55126  </DestinationZip> 
     <DestinationCountry>US</DestinationCountry> 
     <ShippingDate>116320</ShippingDate> 
     <ShippingTime>230000</ShippingTime> 
    </LoadData> 
    <LoadData> 
     <ShipmentNumber>24469114</ShipmentNumber> 
     <TrackingType>FK </TrackingType> 
     <LoadNumber>11325877</LoadNumber> 
     <StopSequence>1</StopSequence> 
     <CarrierNumber>22104</CarrierNumber> 
     <SCAC>WOLH</SCAC> 
     <ShippingName>Somewhere DC        </ShippingName> 
     <ShippingAddress>115 S MAIN ST      </ShippingAddress> 
     <ShippingCity>CLEVELAND     </ShippingCity> 
     <ShippingState>OH </ShippingState> 
     <ShippingZip>54915  </ShippingZip> 
     <ShippingCountry>US </ShippingCountry> 
     <PromisedDate>116318</PromisedDate> 
     <PromisedTime>235900</PromisedTime> 
     <DestinationName>Test DC       </DestinationName> 
     <DestinationAddress>450 DECLARATION DR     </DestinationAddress> 
     <DestinationCity>MCDONOUGH    </DestinationCity> 
     <DestinationState>GA</DestinationState> 
     <DestinationZip>30253  </DestinationZip> 
     <DestinationCountry>US</DestinationCountry> 
     <ShippingDate>116316</ShippingDate> 
     <ShippingTime>130000</ShippingTime> 
    </LoadData> 
</Load> 

進入這個:

<Loads> 
    <Load> 
     <Action>add</Action> 
     <BillOfLading<90621234</BillOfLading> 
     <Carrier> 
     <SCAC>WOLH</SCAC> 
     </Carrier> 
     <PickUpStops> 
     <PickUpStop> 
      <ActionPickup>add</ActionPickup> 
      <NamePickup>Somewhere DC</NamePickup> 
      <StopIdPickup>0</StopIdPickup> 
      <StopSequencePickup>0</StopSequencePickup> 
      <StreetAddressPickup>115 S MAIN ST</StreetAddressPickup> 
      <CityPickup>CLEVELAND</CityPickup> 
      <StatePickup>OH</StatePickup> 
      <PostalPickup>54915</PostalPickup> 
      <CountryPickup>US</CountryPickup> 
      <EarliestPlannedPickupTime>2016-11-15T23:00:00Z</EarliestPlannedPickupTime> 
      <LatestPlannedPickupTime>2016-11-15T23:00:00Z</LatestPlannedPickupTime> 
     </PickupStop> 
     </PickUpStops> 
     <DeliveryStops> 
     <DeliveryStop> 
      <ActionDelivery>add</ActionDelivery> 
      <NameDelivery>COMPANY1</NameDelivery> 
      <StopIdDelivery>1</StopIdDelivery> 
      <StopSequenceDelivery>1</StopSequenceDelivery> 
      <StreetAddressDelivery>4000 #RD AVE N</StreetAddressDelivery> 
      <CityDelivery>MINNEAPOLIS</CityDelivery> 
      <StateDelivery<MN</StateDelivery> 
      <PostalDelivery>55443</PostalDelivery> 
      <CountryDelivery>US</CountryDelivery> 
      <EarliestAppointmentTime>2016-11-16T15:30:00Z</EarliestAppointmentTime> 
      <LatestAppointmentTime>2016-11-16T15:30:00Z</LatestAppointmentTime> 
     </DeliveryStop> 
     <DeliveryStop> 
      <ActionDelivery>add</ActionDelivery> 
      <NameDelivery>COMPANY 2</NameDelivery> 
      <StopIdDelivery>2</StopIdDelivery> 
      <StopSequenceDelivery>2</StopSequenceDelivery> 
      <StreetAddressDelivery>3 KENT ST</StreetAddressDelivery> 
      <CityDelivery>SHOREVIEW</CityDelivery> 
      <StateDelivery<MN</StateDelivery> 
      <PostalDelivery>55126</PostalDelivery> 
      <CountryDelivery>US</CountryDelivery> 
      <EarliestAppointmentTime>2016-11-16T13:30:00Z</EarliestAppointmentTime> 
      <LatestAppointmentTime>2016-11-16T13:30:00Z</LatestAppointmentTime> 
     </DeliveryStop> 
     </DeliveryStops> 
    </Load> 
    <Load> 
     <Action>add</Action> 
     <BillOfLading<11325877</BillOfLading> 
     <Carrier> 
     <SCAC>WOLH</SCAC> 
     </Carrier> 
     <PickUpStops> 
     <PickUpStop> 
      <ActionPickup>add</ActionPickup> 
      <NamePickup>Somewhere DC</NamePickup> 
      <StopIdPickup>0</StopIdPickup> 
      <StopSequencePickup>0</StopSequencePickup> 
      <StreetAddressPickup>115 S MAIN ST</StreetAddressPickup> 
      <CityPickup>CLEVELAND</CityPickup> 
      <StatePickup>OH</StatePickup> 
      <PostalPickup>54915</PostalPickup> 
      <CountryPickup>US</CountryPickup> 
      <EarliestPlannedPickupTime>2016-11-11T13:00:00Z</EarliestPlannedPickupTime> 
      <LatestPlannedPickupTime>2016-11-11T13:00:00Z</LatestPlannedPickupTime> 
     </PickupStop> 
     </PickUpStops> 
     <DeliveryStops> 
     <DeliveryStop> 
      <ActionDelivery>add</ActionDelivery> 
      <NameDelivery>Test DC</NameDelivery> 
      <StopIdDelivery>1</StopIdDelivery> 
      <StopSequenceDelivery>1</StopSequenceDelivery> 
      <StreetAddressDelivery>450 DECLARATION DR</StreetAddressDelivery> 
      <CityDelivery>MCDONOUGH</CityDelivery> 
      <StateDelivery<GA</StateDelivery> 
      <PostalDelivery>30253</PostalDelivery> 
      <CountryDelivery>US</CountryDelivery> 
      <EarliestAppointmentTime>2016-11-13T23:59:00Z</EarliestAppointmentTime> 
      <LatestAppointmentTime>2016-11-13T23:59:00Z</LatestAppointmentTime> 
    </DeliveryStop> 
    </DeliveryStops> 
    </Load> 
</Loads>` 

我還必須找到一種方法,在我輸入Julian日期轉換爲我所需的輸出格式爲好。下面的XSLT和我一樣。我試圖將LoadNumber用作關鍵字,但我不確定如何使用此關鍵字將兩個輸入記錄合併成一個具有兩個停止點的單個載入,因此xslt不完整。任何人都可以給我一些建議嗎?謝謝。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
     <xsl:output omit-xml-declaration="yes" indent="yes"/> 

     <xsl:key name="load" match="LoadData" use="LoadNumber"/> 

     <xsl:template match="/root"> 
     <xsl:copy> 
      <xsl:apply-templates select="LoadData[generate-id(.) = 
         generate-id(key('load', LoadNumber))]"/> 
     </xsl:copy> 
     </xsl:template> 

     <xsl:template match="LoadData"> 
     <Loads> 
     <Action> 
      <xsl:text>add</xsl:text> 
      <BillOfLading> 
       <xsl:value-of select = "LoadNumber"/> 
      </BillOfLading> 
      <Carrier> 
       <SCAC> 
       <xsl:value-of select = "SCAC"/> 
       </SCAC> 
      <PickUpStops> 
       <PickUpStop> 
        <ActionPickup> 
        <xsl:text>add</xsl:text> 
        <ActionPickup> 
        <NamePickup> 
        <xsl:value-of select = "ShippingName"/> 
        </NamePickup> 
        <StopIdPickup> 
        <xsl:text>0</xsl:text> 
        </StopIdPickup> 
        <StopSequencePickup> 
        <xsl:text>0</xsl:text> 
        </StopSequencePickup> 
        <StreetAddressPickup> 
        <xsl:value-of select = "ShippingAddress"/> 
        </StreetAddressPickup> 
        <CityPickup> 
        <xsl:value-of select = "ShippingCity"/> 
        </CityPickup> 
        <StatePickup> 
        <xsl:value-of select = "ShippingState"/> 
        </StatePickup> 
        <PostalPickup> 
        <xsl:value-of select = "ShippingZip"/> 
        </PostalPickup> 
        <CountryPickup> 
         <xsl:value-of select = "ShippingCountry"/> 
        </CountryPickup> 
        <EarliestPlannedPickupTime> 
        <xsl:value-of select = "concat(ShippingDate,'T',ShippingTime)"/> 
        </EarliestPlannedPickupTime> 
        <LatestPlannedPickupTime> 
        <xsl:value-of select = "concat(ShippingDate,'T',ShippingTime)"/> 
        </LatestPlannedPickupTime> 
       </PickupStop> 
      </PickupStops> 
      <DeliveryStop> 
       <ActionDelivery> 
        <xsl:text>add</xsl:text> 
       </ActionDelivery> 
       <NameDelivery> 
        <xsl:value-of select = "ShippingName"/> 
       </NameDelivery> 
       <StopIdDelivery> 
        <xsl:value-of select="key('load',LoadNumber)//Stop 
       </StopIdDelivery> 
     </Loads> 
     </xsl:template> 

    </xsl:stylesheet> 
+0

「*我還必須找到一種方法將我的輸入中的朱利安日期轉換爲我想要的輸出格式。*「另外提出一個問題。順便說一句,那些日期看起來像格里高利。 –

回答

0

我不能確定如何使用此鍵 一起合併兩個輸入記錄到一個單一的負載兩個站

你需要爲了得到重新使用相同的密鑰記錄在當前組中。下面是一個例子,降低了清晰度的最小必要證明的原則:

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="*"/> 

<xsl:key name="load" match="LoadData" use="LoadNumber"/> 

<xsl:template match="/Load"> 
    <Loads> 
     <xsl:apply-templates select="LoadData[generate-id() = generate-id(key('load', LoadNumber)[1])]" mode="pickup"/> 
    </Loads> 
</xsl:template> 

<xsl:template match="LoadData" mode="pickup"> 
    <Load> 
     <!-- more here --> 
     <PickupStops> 
      <PickupStop> 
       <!-- more here --> 
       <CityPickup> 
        <xsl:value-of select="ShippingCity"/> 
       </CityPickup> 
       <!-- more here --> 
      </PickupStop> 
     </PickupStops> 
     <DeliveryStops> 
      <xsl:apply-templates select="key('load', LoadNumber)"/> 
     </DeliveryStops> 
    </Load> 
</xsl:template> 

<xsl:template match="LoadData"> 
    <DeliveryStop> 
     <!-- more here --> 
     <CityPickup> 
     <xsl:value-of select="DestinationCity"/> 
     </CityPickup> 
     <!-- more here --> 
    </DeliveryStop> 
</xsl:template> 

</xsl:stylesheet> 

應用到您的示例輸入,結果將是:

<?xml version="1.0" encoding="UTF-8"?> 
<Loads> 
    <Load> 
    <PickupStops> 
     <PickupStop> 
     <CityPickup>CLEVELAND     </CityPickup> 
     </PickupStop> 
    </PickupStops> 
    <DeliveryStops> 
     <DeliveryStop> 
     <CityPickup>MINNEAPOLIS   </CityPickup> 
     </DeliveryStop> 
     <DeliveryStop> 
     <CityPickup>SHOREVIEW    </CityPickup> 
     </DeliveryStop> 
    </DeliveryStops> 
    </Load> 
    <Load> 
    <PickupStops> 
     <PickupStop> 
     <CityPickup>CLEVELAND     </CityPickup> 
     </PickupStop> 
    </PickupStops> 
    <DeliveryStops> 
     <DeliveryStop> 
     <CityPickup>MCDONOUGH    </CityPickup> 
     </DeliveryStop> 
    </DeliveryStops> 
    </Load> 
</Loads>