你能用xpath3将内容复制到文件名吗?

时间:2016-03-03 04:59:05

标签: xpath mule filenames

我正在尝试将xml标记作为文件名的一部分,但我没有获得任何值。难道我做错了什么?我在更改变压器时收到错误或空值

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns:mulexml="http://www.mulesoft.org/schema/mule/xml" xmlns:email="http://www.mulesoft.org/schema/mule/email" xmlns:pop3="http://www.mulesoft.org/schema/mule/pop3" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
    xmlns:spring="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/pop3 http://www.mulesoft.org/schema/mule/pop3/current/mule-pop3.xsd
http://www.mulesoft.org/schema/mule/email http://www.mulesoft.org/schema/mule/email/current/mule-email.xsd
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd
http://www.mulesoft.org/schema/mule/xml http://www.mulesoft.org/schema/mule/xml/current/mule-xml.xsd">
    <flow name="email_notificationFlow">
        <file:inbound-endpoint path="C:\Users\pd00h\Desktop\Knauf\testxML" responseTimeout="10000" doc:name="File"/>
        <file:file-to-byte-array-transformer doc:name="File to Byte Array"/>
        <set-variable variableName="order" value="#[xpath3('//ns0:Payload/WhsDockets/WhsDocket/Reference')]" doc:name="Variable"/>
        <logger message="#[flowVars.order]" level="INFO" doc:name="Logger"/>
        <file:outbound-endpoint path="C:\Users\pd00h\Desktop\Knauf" outputPattern="Knauf-#[message.id].xml" responseTimeout="10000" doc:name="File"/>
    </flow>
</mule>

示例输入文件

<?xml version="1.0" encoding="utf-8"?><XmlInterchange xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Version="1" xmlns="http://www.edi.com.au/EnterpriseService/">
<InterchangeInfo>
    <Date>2016-02-19T09:31:34.969+09:00</Date>
    <XmlType>LightWeight</XmlType>
    <Source>
        <EnterpriseCode>AWH</EnterpriseCode>
        <CompanyCode>ADL</CompanyCode>
        <OriginServer>ADL</OriginServer>
    </Source>
    <Target />
    <EDIOrganisation EDICode="AWHLOG_AU" OwnerCode="AWHLOG_AU">
        <OrganisationDetails>
            <Name>AWH LOGISTICS</Name>
            <Location Country="Australia" City="Adelaide">AUADL</Location>
            <Addresses>
                <Address AddressType="MAIN">
                    <AddressLine1>Gillman Wool Complex</AddressLine1>
                    <AddressLine2>GRAND TRUNKWAY,</AddressLine2>
                    <AddressCode>Gillman Wool Complex</AddressCode>
                    <CityOrSuburb>GILLMAN</CityOrSuburb>
                    <StateOrProvince>SA</StateOrProvince>
                    <PostCode>5013</PostCode>
                    <TelephoneNumbers>
                        <TelephoneNumber NumberType="Business">+618 8240 8400</TelephoneNumber>
                        <TelephoneNumber NumberType="Fax">+618 8240 0080</TelephoneNumber>
                    </TelephoneNumbers>
                    <Email>containers@awh.com.au</Email>
                    <Language>ENG</Language>
                    <Location>AUADL</Location>
                    <Sequence>1</Sequence>
                    <AddressCapabilities>
                        <AddressCapability AddressType="MAIN" />
                        <AddressCapability IsMainAddress="true" AddressType="OFC" />
                        <AddressCapability IsMainAddress="true" AddressType="PAD" />
                    </AddressCapabilities>
                </Address>
            </Addresses>
        </OrganisationDetails>
    </EDIOrganisation>
</InterchangeInfo>
<Payload>
    <WhsDockets>
        <WhsDocket>
        <Identifier>
            <Client EDICode="KNAINSBNE" OwnerCode="KNAINSBNE">
                <OrganisationDetails>
                    <Name>Knauf Insulation Pty Ltd</Name>
                    <Location Country="Australia" City="Brisbane">AUBNE</Location>
                    <Addresses>
                        <Address AddressType="MAIN">
                            <AddressLine1>2/44 BorthwickAve</AddressLine1>
                            <AddressCode>2/44 BorthwickAve</AddressCode>
                            <CityOrSuburb>MURARRIE</CityOrSuburb>
                            <StateOrProvince>QLD</StateOrProvince>
                            <PostCode>4172</PostCode>
                            <TelephoneNumbers>
                                <TelephoneNumber NumberType="Business">+61 (7) 3393-7300</TelephoneNumber>
                                <TelephoneNumber NumberType="Mobile">+61 (438) 488-000</TelephoneNumber>
                                <TelephoneNumber NumberType="Fax">+61 (7) 3343-1898</TelephoneNumber>
                            </TelephoneNumbers>
                            <Email>orders.au@knaufinsulation.com</Email>
                            <Language>ENG</Language>
                            <Location>AUBNE</Location>
                            <Sequence>1</Sequence>
                            <AddressCapabilities>
                                <AddressCapability AddressType="MAIN" />
                                <AddressCapability IsMainAddress="true" AddressType="OFC" />
                            </AddressCapabilities>
                    </Address>
                </Addresses>
            </OrganisationDetails>
        </Client>
        <Reference>2363867</Reference>
        <DocketType>WOH</DocketType>
        <ActionType>CON</ActionType>
    </Identifier>
    <DocketDetail>
        <WarehouseCode>ROC</WarehouseCode>
        <CustomerReference>3330826</CustomerReference>
        <Units>80</Units>
        <Packages>0</Packages>
        <Pallets>0</Pallets>
        <Weight DimensionType="KG">673</Weight>
        <Cubic DimensionType="M3">7.624</Cubic>
        <TransportInsurance>0.0000</TransportInsurance>
        <ShipperCODAmount>0.0000</ShipperCODAmount>
        <CustomerOrderDetail>
            <OrderType>ORD</OrderType>
            <DateRequired>2015-08-25T00:00:00</DateRequired>
            <Consignee AddressType="CEA">
                <AddressLine1>71-83 Kenny Street</AddressLine1>
                <CityOrSuburb>PORTSMITH</CityOrSuburb>
                <StateOrProvince>QLD</StateOrProvince>
                <PostCode>4870</PostCode>
                <CompanyName>Bunnings Cairns Central Warehouse</CompanyName>
                <CountryCode>AU</CountryCode>
                <ContactName>The Import Manager</ContactName>
            </Consignee>
        </CustomerOrderDetail>
        <CustomAttributes />
    </DocketDetail>
    <DocketLines>
        <DocketLine>
            <Product>E2271</Product>
            <Description>R 3 0 145mm x 580mm x 1160mm</Description>
            <QuantityFromClientOrder>4</QuantityFromClientOrder>
            <QuantityActuallyOrdered>4</QuantityActuallyOrdered>
            <ProductUQ>MST</ProductUQ>
            <LineAttributes />
            <LineNumber>1</LineNumber>
            <Confirmation>
                <Lines>
                    <Line>
                        <Quantity>16</Quantity>
                        <QuantityUQ>PAC</QuantityUQ>
                    </Line>
                </Lines>
                <Quantity>16</Quantity>
            </Confirmation>
        </DocketLine>
        <DocketLine>
            <Product>E4386</Product>
            <Description>R 2 0 90mm x 580mm x 19000mm</Description>
            <QuantityFromClientOrder>4</QuantityFromClientOrder>
            <QuantityActuallyOrdered>4</QuantityActuallyOrdered>
            <ProductUQ>MST</ProductUQ>
            <LineAttributes />
            <LineNumber>2</LineNumber>
            <Confirmation>
                <Lines>
                    <Line>
                        <Quantity>32</Quantity>
                        <QuantityUQ>PAC</QuantityUQ>
                    </Line>
                </Lines>
                <Quantity>32</Quantity>
            </Confirmation>
        </DocketLine>
        <DocketLine>
            <Product>450521</Product>
            <Description>XPS300 30 x 600 x 1200 B1 / On / SE</Description>
            <QuantityFromClientOrder>20</QuantityFromClientOrder>
            <QuantityActuallyOrdered>20</QuantityActuallyOrdered>
            <ProductUQ>PC</ProductUQ>
            <LineAttributes />
            <LineNumber>3</LineNumber>
            <Confirmation>
                <Lines>
                    <Line>
                        <Quantity>20</Quantity>
                        <QuantityUQ>PC</QuantityUQ>
                    </Line>
                </Lines>
                <Quantity>20</Quantity>
            </Confirmation>
        </DocketLine>
        <DocketLine>
            <Product>450523</Product>
            <Description>XPS300 50 x 600 x 1200 B1 / On / SE</Description>
            <QuantityFromClientOrder>12</QuantityFromClientOrder>
            <QuantityActuallyOrdered>12</QuantityActuallyOrdered>
            <ProductUQ>PC</ProductUQ>
            <LineAttributes />
            <LineNumber>4</LineNumber>
            <Confirmation>
                <Lines>
                    <Line>
                        <Quantity>12</Quantity>
                        <QuantityUQ>PC</QuantityUQ>
                    </Line>
                </Lines>
                <Quantity>12</Quantity>
            </Confirmation>
        </DocketLine>
    </DocketLines>
   </WhsDocket>
</WhsDockets>
</Payload>

你能用它来复杂的XML结构吗?

1 个答案:

答案 0 :(得分:1)

  1. 添加命名空间管理器
  2. 替换文件:file-to-byte-array-transformer with byte-array-to-string-transformer
  3. 修复xpath用法
  4. 更新代码:

    <mule xmlns:file="http://www.mulesoft.org/schema/mule/file"
        xmlns:mulexml="http://www.mulesoft.org/schema/mule/xml" xmlns:email="http://www.mulesoft.org/schema/mule/email"
        xmlns:pop3="http://www.mulesoft.org/schema/mule/pop3" xmlns="http://www.mulesoft.org/schema/mule/core"
        xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
        xmlns:spring="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
        http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
        http://www.mulesoft.org/schema/mule/pop3 http://www.mulesoft.org/schema/mule/pop3/current/mule-pop3.xsd
        http://www.mulesoft.org/schema/mule/email http://www.mulesoft.org/schema/mule/email/current/mule-email.xsd
        http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd
        http://www.mulesoft.org/schema/mule/xml http://www.mulesoft.org/schema/mule/xml/current/mule-xml.xsd">
        <mulexml:namespace-manager
            includeConfigNamespaces="true">
            <mulexml:namespace prefix="ns0"
                uri="http://www.example.com/something.xsd" />
        </mulexml:namespace-manager>
        <flow name="email_notificationFlow">
            <file:inbound-endpoint path="C:\Users\pd00h\Desktop\Knauf\testxML"
                responseTimeout="10000" doc:name="File" />
            <byte-array-to-string-transformer
                doc:name="Byte Array to String" />
            <set-variable variableName="order"
                value="#[xpath3('//ns0:WhsDockets/ns0:WhsDocket/ns0:Reference')]"
                doc:name="Variable" />
            <logger message="#[flowVars.order]" level="INFO" doc:name="Logger" />
            <file:outbound-endpoint path="C:\Users\pd00h\Desktop\Knauf"
                outputPattern="Knauf-#[message.id].xml" responseTimeout="10000"
                doc:name="File" />
        </flow>
    </mule>
    

    示例xml文件:

    <?xml version='1.0' encoding='UTF-8'?>
    <ns0:WhsDockets xmlns:ns0="http://www.example.com/something.xsd">
        <ns0:WhsDocket>
            <ns0:Reference>GotIt!</ns0:Reference>
        </ns0:WhsDocket>
    </ns0:WhsDockets>
    

    干杯