需要使用XSLT Transformation来解决XML问题

时间:2010-11-10 03:01:15

标签: xml xslt transformation

我有一个源XML文件,我需要将其展平为更结构化的XML,以便稍后我可以将其作为数据集进行处理。
我已经通过apply-templates和for-each命令尝试了很多方法,但似乎总是不尽如人意。

我需要澄清元素之间的关系。从最深的元素,即BookingDetail元素开始,如果它的元素等于BookingHeader元素的元素,它应该只是BookingHeader元素的子元素。然后向上链,如果它们的两个相等,则BookingHeader元素将仅作为ManifestBooking元素的子元素出现。最后,如果ManifestBooking元素的两个相等,它们应该只作为Manifest元素的子元素出现。

非常感谢任何帮助。

源文件:


<?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>

进入这个:


<?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>

1 个答案:

答案 0 :(得分:1)

此样式表:

<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>

输出:

<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>

注意:您想要的输出有一个名称空间声明,但元素在null名称空间URI下(如果您需要,请澄清)。使用大型文档的键可以更有效地完成查找。如果有不止一个,也可以用于清单(不仅仅是书籍)。

编辑:剥离命名空间声明。

编辑2 :添加了清单绑定。