开发者

Need to un-flatten XML using XLST Transformation

I have a source XML file which I need to un-flatten into a more structured XML so that I can then process later on as a dataset.

I've tried numerous methods via apply-templates and for-each commands, but always seem to fall short.

I need to clarify the relationship between the elements. Starting from deepest element, the BookingDetail element, it should only be a child of the BookingHeader element if it's element equals that of the BookingHeader element. Then going up the chain, the BookingHeader element will only appear as a child to the ManifestBooking element if their two equal. And finally the ManifestBooking element should only appear as a child to the Manifest element if their two equal.

Any help would be much appreciated.

Source file:


<?xml version="1.0" encoding="UTF-8"?>
<ShipmentInformation xmlns="http://www.tranxml.org/TranXML/Version4.0" Transaction="Actual">
    <GeneralShipmentInformation>
        <TransactionSetPurposeCode>00</TransactionSetPurposeCode>
        <TransportationMethodTypeCode>L</TransportationMethodTypeCode>
        <ShipmentMethodOfPayment>DE</ShipmentMethodOfPayment>
        <ShipmentIdentificationNumber>918072</ShipmentIdentificationNumber>
        <StandardCarrierAlphaCode>NUNCRR</StandardCarrierAlphaCode>
    </GeneralShipmentInformation>
    <Priority>0</Priority>
    <DateTimeReference SegmentId="StartTime">
        <Date>2010-11-05</Date>
        <Time>00:00:00</Time>
    </DateTimeReference>
    <Origin>
        <GeographicLocation>
            <LocationIdentifier>NUN</LocationIdentifier>
        </GeographicLocation>
    </Origin>
    <Destination>
        <GeographicLocation>
            <LocationIdentifier>SSS BARGAIN BUILDERS SUPP P/L</LocationIdentifier>
        </GeographicLocation>
    </Destination>
    <EquipmentDetailsLoop>
        <EquipmentDetails>
            <EquipmentStructure>
                <Initial/>
                <EquipmentNumber>Z48GW</EquipmentNumber>
                <DescriptionCode>TV</DescriptionCode>
            </EquipmentStructure>
        </EquipmentDetails>
    </EquipmentDetailsLoop>
    <EquipmentDetailsLoop>
        <EquipmentDetails>
            <EquipmentStructure>
                <Initial/>
                <EquipmentNumber>B50KX</EquipmentNumber>
                <DescriptionCode>TL</DescriptionCode>
            </EquipmentStructure>
        </EquipmentDetails>
    </EquipmentDetailsLoop>
    <EquipmentDetailsLoop>
        <EquipmentDetails>
            <EquipmentStructure>
                <Initial/>
                <EquipmentNumber>B60KX</EquipmentNumber>
                <DescriptionCode>TL</DescriptionCode>
            </EquipmentStructure>
        </EquipmentDetails>
    </EquipmentDetailsLoop>
    <EquipmentDetailsLoop>
        <EquipmentDetails>
            <EquipmentStructure>
                <Initial/>
                <EquipmentNumber>B70KX</EquipmentNumber>
                <DescriptionCode>TL</DescriptionCode>
            </EquipmentStructure>
        </EquipmentDetails>
    </EquipmentDetailsLoop>
    <EquipmentDetailsLoop>
        <EquipmentDetails>
            <EquipmentStructure>
                <Initial/>
                <EquipmentNumber>B80KX</EquipmentNumber>
                <DescriptionCode>TL</DescriptionCode>
            </EquipmentStructure>
        </EquipmentDetails>
    </EquipmentDetailsLoop>
    <ManifestSourceBD xmlns:version4="http://www.tranxml.org/TranXML/Version4.0">
        <DSBookingDetail>
            <SeqNo>1</SeqNo>
            <LineType>Product</LineType>
            <BookingNo>1</BookingNo>
            <OrderNo>CAM/074082/040</OrderNo>
            <SSCC>193168810020083905</SSCC>
            <Description>RIDGE CAP ROLL TOP.42 X 390MM DEEP OCEAN</Description>
            <UnitCode1>KG</UnitCode1>
            <Quantity1>25</Quantity1>
        </DSBookingDetail>
        <DSBookingDetail>
            <SeqNo>2</SeqNo>
            <LineType>Product</LineType>
            <BookingNo>1</BookingNo>
            <OrderNo>CAM/074082/040</OrderNo>
            <SSCC>193168810020083912</SSCC>
            <Description>RIDGE CAP ROLL TOP.42 X 390MM DEEP OCEAN</Description>
            <UnitCode1>KG</UnitCode1>
            <Quantity1>25</Quantity1>
        </DSBookingDetail>
        <DSBookingDetail>
            <SeqNo>3</SeqNo>
            <LineType>Product</LineType>
            <BookingNo>1</BookingNo>
            <OrderNo>CAM/074082/040</OrderNo>
            <SSCC>193168810020083929</SSCC>
            <Description>RIDGE CAP ROLL TOP.42 X 390MM DEEP OCEAN</Description>
            <UnitCode1>KG</UnitCode1>
            <Quantity1>17</Quantity1>
        </DSBookingDetail>
        <DSBookingDetail>
            <SeqNo>4</SeqNo>
            <LineType>Product</LineType>
            <BookingNo>1</BookingNo>
            <OrderNo>CAM/074082/010</OrderNo>
            <SSCC>193168810020115484</SSCC>
            <Description>CUSTOM ORB.42 DEEP OCEAN</Description>
            <UnitCode1>KG</UnitCode1>
            <Quantity1>484</Quantity1>
        </DSBookingDetail>
        <DSBookingDetail>
            <SeqNo>5</SeqNo>
            <LineType>Product</LineType>
            <BookingNo>3</BookingNo>
            <OrderNo>CAM/074082/010</OrderNo>
            <SSCC>193168810020115491</SSCC>
            <Description>CUSTOM ORB.42 DEEP OCEAN</Description>
            <UnitCode1>KG</UnitCode1>
            <Quantity1>487</Quantity1>
        </DSBookingDetail>
        <DSBookingDetail>
            <SeqNo>6</SeqNo>
            <LineType>Product</LineType>
            <BookingNo>1</BookingNo>
            <OrderNo>CAM/074082/010</OrderNo>
            <SSCC>193168810020115507</SSCC>
            <Description>CUSTOM ORB.42 DEEP OCEAN</Description>
            <UnitCode1>KG</UnitCode1>
            <Quantity1>274</Quantity1>
        </DSBookingDetail>
        <DSBookingDetail>
            <SeqNo>7</SeqNo>
            <LineType>Product</LineType>
            <BookingNo>1</BookingNo>
            <OrderNo>CAM/074082/010</OrderNo>
            <SSCC>193168810380969598</SSCC>
            <Description>CUSTOM ORB.42 DEEP OCEAN</Description>
            <UnitCode1>KG</UnitCode1>
            <Quantity1>149</Quantity1>
        </DSBookingDetail>
        <DSBookingDetail>
            <SeqNo>8</SeqNo>
            <LineType>Product</LineType>
            <BookingNo>1</BookingNo>
            <OrderNo>CAM/074082/020</OrderNo>
            <SSCC>193168810380969604</SSCC>
            <Description>CUSTOM ORB.42 SANDBANK</Description>
            <UnitCode1>KG</UnitCode1>
            <Quantity1>312</Quantity1>
        </DSBookingDetail>
        <DSBookingDetail>
            <SeqNo>9</SeqNo>
            <LineType>Product</LineType>
            <BookingNo>1</BookingNo>
            <OrderNo>CAM/074082/030</OrderNo>
            <SSCC>193168810380969611</SSCC>
            <Description>VALLEY GUTTER.42 390MM DEEP OCEAN</Description>
            <UnitCode1>KG</UnitCode1>
            <Quantity1>11</Quantity1>
        </DSBookingDetail>
        <DSBookingDetail>
            <SeqNo>10</SeqNo>
            <LineType>Product</LineType>
            <BookingNo>1</BookingNo>
            <OrderNo>CAM/074082/040</OrderNo>
            <SSCC>193168810380969628</SSCC>
            <Description>RIDGE CAP ROLL TOP.42 X 390MM DEEP OCEAN</Description>
            <UnitCode1>KG</UnitCode1>
            <Quantity1>17</Quantity1>
        </DSBookingDetail>
        <DSBookingDetail>
            <SeqNo>11</SeqNo>
            <LineType>Product</LineType>
            <BookingNo>2</BookingNo>
            <OrderNo>NUN/017553/010</OrderNo>
            <SSCC>193168810020081963</SSCC>
            <Description>CUSTOM ORB.42 PAPERBARK</Description>
            <UnitCode1>KG</UnitCode1>
            <Quantity1>279</Quantity1>
        </DSBookingDetail>
        <DSBookingDetail>
            <SeqNo>12</SeqNo>
            <LineType>Product</LineType>
            <BookingNo>2</BookingNo>
            <OrderNo>NUN/017553/020</OrderNo>
            <SSCC>193168810020081970</SSCC>
            <Description>QUAD GUTTER HI 115 PAPERBARK (PLAIN)</Description>
            <UnitCode1>KG</UnitCode1>
            <Quantity1>6</Quantity1>
        </DSBookingDetail>
        <DSBookingDetail>
            <SeqNo>13</SeqNo>
            <LineType>Product</LineType>
            <BookingNo>2</BookingNo>
            <OrderNo>NUN/017553/070</OrderNo>
            <SSCC>193168810020082021</SSCC>
            <Description>UC UNIVERSAL CAP PAPERBARK</Description>
            <UnitCode1>KG</UnitCode1>
            <Quantity1>13</Quantity1>
        </DSBookingDetail>
        <DSBookingDetail>
            <SeqNo>14</SeqNo>
            <LineType>Product</LineType>
            <BookingNo>2</BookingNo>
            <OrderNo>CAM/074112/010</OrderNo>
            <SSCC>193168810380973892</SSCC>
            <Description>QUAD GUTTER HI 115 CLASSIC CREAM (PLAIN)</Description>
            <UnitCode1>KG</UnitCode1>
            <Quantity1>6</Quantity1>
        </DSBookingDetail>
        <DSBookingDetail>
            <SeqNo>15</SeqNo>
            <LineType>Product</LineType>
            <BookingNo>3</BookingNo>
            <OrderNo>CAM/074112/050</OrderNo>
            <SSCC>193168810380973939</SSCC>
            <Description开发者_高级运维>CUSTOM ORB.42 WOODLAND GREY</Description>
            <UnitCode1>KG</UnitCode1>
            <Quantity1>149</Quantity1>
        </DSBookingDetail>
        <DSBookingDetail>
            <SeqNo>16</SeqNo>
            <LineType>Product</LineType>
            <BookingNo>2</BookingNo>
            <OrderNo>CAM/074112/040</OrderNo>
            <SSCC>193168810020118645</SSCC>
            <Description>QUAD GUTTER HI 115 EXTERNAL BRACKET CLASSIC CREAM</Description>
            <UnitCode1>KG</UnitCode1>
            <Quantity1>1</Quantity1>
        </DSBookingDetail>
        <DSBookingDetail>
            <SeqNo>17</SeqNo>
            <LineType>Product</LineType>
            <BookingNo>2</BookingNo>
            <OrderNo>NUN/017553/060</OrderNo>
            <SSCC>193168810020118669</SSCC>
            <Description>POP ROUND 90MM ZINCALUME NOZZLE/DROP</Description>
            <UnitCode1>KG</UnitCode1>
            <Quantity1>1</Quantity1>
        </DSBookingDetail>
    </ManifestSourceBD>
    <ManifestSourceBH xmlns:version4="http://www.tranxml.org/TranXML/Version4.0">
        <DSBookingHeader/>
        <DSBookingHeader>
            <BookingNo>2</BookingNo>
            <SenderName>BlueScope Lysaght</SenderName>
            <ReceiverName>REECE PTY LTD - CAMPBELLFIELD</ReceiverName>
            <ReceiverAddress1>35   STATION              AV vicroads 639 S6</ReceiverAddress1>
            <ReceiverAddress3>HEATHCOTE JUNCTION</ReceiverAddress3>
            <ReceiverPostCode>3758</ReceiverPostCode>
            <DespatchDate>2010-11-05</DespatchDate>
            <DeliveryDate>2010-11-05</DeliveryDate>
            <SenderReference>918072</SenderReference>
            <DeliveryStartTime>00:00:00</DeliveryStartTime>
        </DSBookingHeader>
        <DSBookingHeader>
            <BookingNo>1</BookingNo>
            <SenderName>BlueScope Lysaght</SenderName>
            <ReceiverName>SSS BARGAIN BUILDERS SUPP P/L</ReceiverName>
            <ReceiverAddress1>83   HIGH                 ST TO 85</ReceiverAddress1>
            <ReceiverAddress3>BROADFORD</ReceiverAddress3>
            <ReceiverPostCode>3658</ReceiverPostCode>
            <DespatchDate>2010-11-05</DespatchDate>
            <DeliveryDate>2010-11-05</DeliveryDate>
            <SenderReference>918072</SenderReference>
            <DeliveryStartTime>00:00:00</DeliveryStartTime>
        </DSBookingHeader>
        <DSBookingHeader>
            <BookingNo>3</BookingNo>
            <SenderName>BlueScope Lysaght</SenderName>
            <ReceiverName>SSS BARGAIN BUILDERS SUPP P/L</ReceiverName>
            <ReceiverAddress1>83   HIGH                 ST TO 85</ReceiverAddress1>
            <ReceiverAddress3>BROADFORD</ReceiverAddress3>
            <ReceiverPostCode>3658</ReceiverPostCode>
            <DespatchDate>2010-11-05</DespatchDate>
            <DeliveryDate>2010-11-05</DeliveryDate>
            <SenderReference>918072</SenderReference>
            <DeliveryStartTime>00:00:00</DeliveryStartTime>
        </DSBookingHeader>
    </ManifestSourceBH>
    <ManifestSourceM xmlns:version4="http://www.tranxml.org/TranXML/Version4.0">
        <DSManifest>
            <ManifestNo>918072</ManifestNo>
            <ManifestDate>2010-11-05</ManifestDate>
            <PrimeMover>Z48GW</PrimeMover>
            <Fleet1>B50KX</Fleet1>
            <Fleet2>B60KX</Fleet2>
            <Fleet3>B70KX</Fleet3>
            <Fleet4>B80KX</Fleet4>
            <Fleet5>B90KX</Fleet5>
            <Description>""</Description>
            <Description2>""</Description2>
        </DSManifest>
    </ManifestSourceM>
    <ManifestSourceMB xmlns:version4="http://www.tranxml.org/TranXML/Version4.0">
        <DSManifestBooking/>
        <DSManifestBooking>
            <ManifestNo>918072</ManifestNo>
            <BookingNo>3</BookingNo>
            <SeqNo>1</SeqNo>
            <SubBookingNo>1</SubBookingNo>
            <Confirmed>"No"</Confirmed>
            <Charge>"No"</Charge>
            <SentToEDI>"No"</SentToEDI>
        </DSManifestBooking>
        <DSManifestBooking>
            <ManifestNo>918072</ManifestNo>
            <BookingNo>2</BookingNo>
            <SeqNo>2</SeqNo>
            <SubBookingNo>1</SubBookingNo>
            <Confirmed>"No"</Confirmed>
            <Charge>"No"</Charge>
            <SentToEDI>"No"</SentToEDI>
        </DSManifestBooking>
        <DSManifestBooking>
            <ManifestNo>918072</ManifestNo>
            <BookingNo>1</BookingNo>
            <SeqNo>1</SeqNo>
            <SubBookingNo>1</SubBookingNo>
            <Confirmed>"No"</Confirmed>
            <Charge>"No"</Charge>
            <SentToEDI>"No"</SentToEDI>
        </DSManifestBooking>
    </ManifestSourceMB>
</ShipmentInformation>

Into this:


<?xml version="1.0" encoding="UTF-8"?>
<ManifestSource xmlns:version4="http://www.tranxml.org/TranXML/Version4.0">
    <DSManifest>
        <ManifestNo>918072</ManifestNo>
        <ManifestDate>2010-11-05</ManifestDate>
        <PrimeMover>Z48GW</PrimeMover>
        <Description>""</Description>
        <Description2>""</Description2>
        <Fleet1>B50KX</Fleet1>
        <Fleet2>B60KX</Fleet2>
        <Fleet3>B70KX</Fleet3>
        <Fleet4>B80KX</Fleet4>
        <Fleet5>B90KX</Fleet5>
        <DSManifestBooking>
           <ManifestNo>918072</ManifestNo>
           <BookingNo>1</BookingNo>
           <SeqNo>1</SeqNo>
           <Confirmed>"No"</Confirmed>
           <Charge>"No"</Charge>
           <SentToEDI>"No"</SentToEDI>
              <DSBookingHeader>
                 <BookingNo>1</BookingNo>
                 <SenderName>BlueScope Lysaght</SenderName>
                 <ReceiverName>REECE PTY LTD - CAMPBELLFIELD</ReceiverName>
                 <ReceiverAddress1>35 STATION AV vicroads 639 S6</ReceiverAddress1>
                 <ReceiverAddress3>HEATHCOTE JUNCTION</ReceiverAddress3>
                 <ReceiverPostCode>3758</ReceiverPostCode>
                 <DespatchDate>2010-11-05</DespatchDate>
                 <DeliveryDate>2010-11-05</DeliveryDate>
                 <SenderReference>918072</SenderReference>
                 <DeliveryStartTime>00:00:00</DeliveryStartTime>
                 <DSBookingDetail>
                    <BookingNo>1</BookingNo>
                    <SeqNo>1</SeqNo>
                    <Description>RIDGE CAP ROLL TOP.42 X 390MM DEEP OCEAN</Description>
                    <Quantity1>25</Quantity1>
                    </DSBookingDetail>
             </DSBookingHeader>
          </DSManifestBooking>
          <DSManifestBooking>
             <ManifestNo>918072</ManifestNo>
             <BookingNo>2</BookingNo>
             <SeqNo>2</SeqNo>
             <Confirmed>"No"</Confirmed>
             <Charge>"No"</Charge>
             <SentToEDI>"No"</SentToEDI>
             <DSBookingHeader>
                <BookingNo>2</BookingNo>
                <SenderName>BlueScope Lysaght</SenderName>
                <ReceiverName>SSS BARGAIN BUILDERS SUPP P/L</ReceiverName>
                <ReceiverAddress1>83 HIGH ST TO 85</ReceiverAddress1>
                <ReceiverAddress3>BROADFORD</ReceiverAddress3>
                <ReceiverPostCode>3658</ReceiverPostCode>
                <DespatchDate>2010-11-05</DespatchDate>
                <DeliveryDate>2010-11-05</DeliveryDate>
                <SenderReference>918072</SenderReference>
                <DeliveryStartTime>00:00:00</DeliveryStartTime>
                <DSBookingDetail>
                   <BookingNo>2</BookingNo>
                   <SeqNo>11</SeqNo>
                   <Description>CUSTOM ORB.42 PAPERBARK</Description>
                   <Quantity1>279</Quantity1>
                </DSBookingDetail>
             </DSBookingHeader>
        </DSManifestBooking>
    </DSManifest>
</ManifestSource>


This stylesheet:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:version4="http://www.tranxml.org/TranXML/Version4.0">
    <xsl:template match="*" name="copy">
        <xsl:element name="{local-name()}">
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>
    <xsl:template match="version4:ShipmentInformation/*
                            [not(self::version4:ManifestSourceM)]"/>
    <xsl:template match="version4:DSManifest/*[last()]">
        <xsl:call-template name="copy"/>
        <xsl:apply-templates select="/*/version4:ManifestSourceMB/*
                                        [version4:ManifestNo =
                                         current()/../version4:ManifestNo]"/>
    </xsl:template>
    <xsl:template match="version4:DSManifestBooking/*[last()]">
        <xsl:call-template name="copy"/>
        <xsl:apply-templates select="/*/version4:ManifestSourceBH/*
                                        [version4:BookingNo =
                                         current()/../version4:BookingNo]"/>
    </xsl:template>
    <xsl:template match="version4:DSBookingHeader/*[last()]">
        <xsl:call-template name="copy"/>
        <xsl:apply-templates select="/*/version4:ManifestSourceBD/*
                                        [version4:BookingNo =
                                         current()/../version4:BookingNo]"/>
    </xsl:template>
    <xsl:template match="version4:ShipmentInformation">
        <xsl:apply-templates/>
    </xsl:template>
</xsl:stylesheet>

Output:

<ManifestSourceM>
    <DSManifest>
        <ManifestNo>918072</ManifestNo>
        <ManifestDate>2010-11-05</ManifestDate>
        <PrimeMover>Z48GW</PrimeMover>
        <Fleet1>B50KX</Fleet1>
        <Fleet2>B60KX</Fleet2>
        <Fleet3>B70KX</Fleet3>
        <Fleet4>B80KX</Fleet4>
        <Description>""</Description>
        <Description2>""</Description2>
        <DSManifestBooking>
            <ManifestNo>918072</ManifestNo>
            <BookingNo>1</BookingNo>
            <SeqNo>1</SeqNo>
            <Confirmed>"No"</Confirmed>
            <Charge>"No"</Charge>
            <SentToEDI>"No"</SentToEDI>
            <DSBookingHeader>
                <BookingNo>1</BookingNo>
                <SenderName>BlueScope Lysaght</SenderName>
                <ReceiverName>REECE PTY LTD - CAMPBELLFIELD</ReceiverName>
                <ReceiverAddress1>35 STATION AV vicroads 639 S6</ReceiverAddress1>
                <ReceiverAddress3>HEATHCOTE JUNCTION</ReceiverAddress3>
                <ReceiverPostCode>3758</ReceiverPostCode>
                <DespatchDate>2010-11-05</DespatchDate>
                <DeliveryDate>2010-11-05</DeliveryDate>
                <SenderReference>918072</SenderReference>
                <DeliveryStartTime>00:00:00</DeliveryStartTime>
                <DSBookingDetail>
                    <BookingNo>1</BookingNo>
                    <SeqNo>1</SeqNo>
                    <Description>RIDGE CAP ROLL TOP.42 X 390MM DEEP OCEAN</Description>
                    <Quantity1>25</Quantity1>
                </DSBookingDetail>
            </DSBookingHeader>
        </DSManifestBooking>
        <DSManifestBooking>
            <ManifestNo>918072</ManifestNo>
            <BookingNo>2</BookingNo>
            <SeqNo>2</SeqNo>
            <Confirmed>"No"</Confirmed>
            <Charge>"No"</Charge>
            <SentToEDI>"No"</SentToEDI>
            <DSBookingHeader>
                <BookingNo>2</BookingNo>
                <SenderName>BlueScope Lysaght</SenderName>
                <ReceiverName>SSS BARGAIN BUILDERS SUPP P/L</ReceiverName>
                <ReceiverAddress1>83 HIGH ST TO 85</ReceiverAddress1>
                <ReceiverAddress3>BROADFORD</ReceiverAddress3>
                <ReceiverPostCode>3658</ReceiverPostCode>
                <DespatchDate>2010-11-05</DespatchDate>
                <DeliveryDate>2010-11-05</DeliveryDate>
                <SenderReference>918072</SenderReference>
                <DeliveryStartTime>00:00:00</DeliveryStartTime>
                <DSBookingDetail>
                    <BookingNo>2</BookingNo>
                    <SeqNo>11</SeqNo>
                    <Description>CUSTOM ORB.42 PAPERBARK</Description>
                    <Quantity1>279</Quantity1>
                </DSBookingDetail>
            </DSBookingHeader>
        </DSManifestBooking>
    </DSManifest>
</ManifestSourceM>

Note: Your desired output has a namespace declaration but elements are under null namespace URI (If you want this, please clarify). The look up can be done more efficiently with keys for large documents. It could also be done for manifest (not just for books) in case there will be more than one.

Edit: Strip namespace declarations.

Edit 2: Added manifest binding.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜