你如何在mule中拆分XML?分路器?

时间:2016-04-18 07:21:29

标签: xslt mule splitter

我对拆分XML有点新意,你能帮助我从一个输入创建多个XML吗?我需要使用分离器吗? XSLT?另外,我可以在xml中插入消息ID吗?

输入

DELIMITER $$
CREATE PROCEDURE test_a_loop()
BEGIN
    DECLARE x  INT;
    DECLARE str  VARCHAR(255);   
    SET x = 0;
    SET str =  '';
    WHILE x < 5 DO
    SET x = x + 1;
    SET str =  CONCAT('num-', x);
    INSERT INTO a_loop (id,num) VALUES (x, str);
    END WHILE;
END $$
DELIMITER ;

输出

<?xml version="1.0"?>
<StockMovementDataRequest xmlns:p1="urn:ams.com.au:dynamo:3pl:am:SAP_AM_I_005:StockMovement" xmlns:a="http://www.edi.com.au/EnterpriseService">
    <Header>
        <From>Warehouse</From>
        <To>Location</To>
        <Unique_ID>idm1468201212</Unique_ID>
        <DateTimeStamp>2016-04-13T11:55:30.263+10:00</DateTimeStamp>
    </Header>
    <StockMovementData>
        <Serialised_Material>YES</Serialised_Material>
        <DateTime>2016-04-13T11:55:30.263+10:00</DateTime>
        <From_Location>30-80</From_Location>
        <To_Location>client</To_Location>
        <Material>7CAGL3G00</Material>
        <Serial>700030011</Serial>
        <Quantity>100</Quantity>
    </StockMovementData>
    <StockMovementData>
        <Serialised_Material>YES</Serialised_Material>
        <DateTime>2016-04-13T11:55:30.263+10:00</DateTime>
        <From_Location>30-80</From_Location>
        <To_Location>client</To_Location>
        <Material>7CAGL3G00</Material>
        <Serial>700029911</Serial>
        <Quantity>100</Quantity>
    </StockMovementData>
</StockMovementDataRequest>

<?xml version="1.0"?>
<StockMovementDataRequest xmlns:p1="urn:ams.com.au:dynamo:3pl:am:SAP_AM_I_005:StockMovement"
    xmlns:a="http://www.edi.com.au/EnterpriseService/">
    <Header>
        <From>warehouse</From>
        <To>client</To>
        <Unique_ID>idm1467386212</Unique_ID>
        <DateTimeStamp>2016-04-13T11:55:30.263+10:00</DateTimeStamp>
    </Header>
    <StockMovementData>
        <Serialised_Material>YES</Serialised_Material>
        <DateTime>2016-04-13T11:55:30.263+10:00</DateTime>
        <From_Location>30-80</From_Location>
        <To_Location>client</To_Location>
        <Material>7CAGL3G00</Material>
        <Serial>700030011</Serial>
        <Quantity>100</Quantity>
    </StockMovementData>
</StockMovementDataRequest>

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

如果您的处理器支持XSLT-2.0,您可以尝试以下代码:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">

    <xsl:strip-space elements="*"/>
    <xsl:output indent="yes"/>

    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="/">
        <xsl:for-each select="StockMovementDataRequest/StockMovementData">
            <xsl:result-document href="{concat('output', position(), '.xml')}">
                <StockMovementDataRequest xmlns:p1="urn:ams.com.au:dynamo:3pl:am:SAP_AM_I_005:StockMovement" xmlns:a="http://www.edi.com.au/EnterpriseService">
                    <xsl:apply-templates select="preceding-sibling::Header"/>
                    <xsl:apply-templates select="."/>
                </StockMovementDataRequest>
            </xsl:result-document>
        </xsl:for-each>
    </xsl:template>

</xsl:stylesheet>

根据您的输入输出output1.xml和output2.xml。

答案 1 :(得分:0)

使用Mule Splitter分割xml

    <splitter expression="#[xpath3('//StockMovementDataRequest/StockMovementData',payload,'NODESET')]" doc:name="Splitter"/>        
    <mulexml:dom-to-xml-transformer doc:name="DOM to XML"/>

要在拆分器后聚合有效负载,请使用aggregator

<collection-aggregator doc:name="Collection Aggregator"/>

请在此处查看更多文档 https://docs.mulesoft.com/mule-user-guide/v/3.7/splitter-flow-control-reference