3

我在这里的第一篇文章。我已经搜索但没有找到我要找的东西。

我不确定我需要什么技术来执行以下操作。

我使用 Mule 3.3 CE,我需要拆分 XML 文件。我需要在每个拆分的 XML 中保留“rootElement”及其属性。所有 XML 文件都将放在同一个 JMS 队列中。

我知道如何拆分三个产品节点,但如何在每个 XML 文件中保留“rootElement”?

XPath?XSLT?DOM 并删除和添加节点?我更喜欢 XPath,但它有能力做到这一点吗?

<?xml version="1.0" encoding="ISO-8859-1"?>
<rootElement xmlns="http://Ecommerce.com/schemas/loyalist/3"
           preOrderTo="2012-12-31T23:59:59"
           currency="GBP"
           timeStamp="2012-08-15T23:59:59">
  <Product
             itemID="09999-3-"
             name="Plate"
             description="Plate of blue man"
             tax="0.00"
             eanCode="1234567890123"
             eanType="EAN 13"/>
  <priceBracket quantity="1"
            price="10.98"
            grossPrice="13.00"/>
  <Product
             itemID="12345-3-"
             name="Plate"
             description="Plate of black man"
             tax="0.00"
             eanCode="1234569870123"
             eanType="EAN 13"/>
  <priceBracket quantity="1"
            price="15.98"
            grossPrice="18.00"/>
  <Product
             itemID="98765-3-"
             name="Plate"
             description="Plate of yellow man"
             tax="0.00"
             eanCode="7894567890123"
             eanType="EAN 13"/>
  <priceBracket quantity="1"
            price="20.98"
            grossPrice="24.00"/>
</rootElement>

我在 Mule 3.3 CE 中需要的是以下拆分:

1.

<?xml version="1.0" encoding="ISO-8859-1"?>
<rootElement xmlns="http://Ecommerce.com/schemas/loyalist/3"
           preOrderTo="2012-12-31T23:59:59"
           currency="GBP"
           timeStamp="2012-08-15T23:59:59">
<Product
             itemID="09999-3-"
             name="Plate"
             description="Plate of blue man"
             tax="0.00"
             eanCode="1234567890123"
             eanType="EAN 13"/>
<priceBracket quantity="1"
            price="10.98"
            grossPrice="13.00"/>
</rootElement>

2.

<?xml version="1.0" encoding="ISO-8859-1"?>
<rootElement xmlns="http://Ecommerce.com/schemas/loyalist/3"
           preOrderTo="2012-12-31T23:59:59"
           currency="GBP"
           timeStamp="2012-08-15T23:59:59">
  <Product
             itemID="12345-3-"
             name="Plate"
             description="Plate of black man"
             tax="0.00"
             eanCode="1234569870123"
             eanType="EAN 13"/>
  <priceBracket quantity="1"
            price="15.98"
            grossPrice="18.00"/>
</rootElement>

3.

<?xml version="1.0" encoding="ISO-8859-1"?>
<rootElement xmlns="http://Ecommerce.com/schemas/loyalist/3"
           preOrderTo="2012-12-31T23:59:59"
           currency="GBP"
           timeStamp="2012-08-15T23:59:59">
  <Product
             itemID="98765-3-"
             name="Plate"
             description="Plate of yellow man"
             tax="0.00"
             eanCode="7894567890123"
             eanType="EAN 13"/>
  <priceBracket quantity="1"
            price="20.98"
            grossPrice="24.00"/>
</rootElement>
4

4 回答 4

0

用 XPath 拆分然后用 XSLT 转换器用 rootElement 包装每个结果怎么样?

于 2012-12-08T23:51:20.767 回答
0

如果您可以使用 XSLT 2.0,这里有一种方法...

XML 输入

<rootElement xmlns="http://Ecommerce.com/schemas/loyalist/3"
    preOrderTo="2012-12-31T23:59:59"
    currency="GBP"
    timeStamp="2012-08-15T23:59:59">
    <Product
        itemID="09999-3-"
        name="Plate"
        description="Plate of blue man"
        tax="0.00"
        eanCode="1234567890123"
        eanType="EAN 13"/>
    <priceBracket quantity="1"
        price="10.98"
        grossPrice="13.00"/>
    <Product
        itemID="12345-3-"
        name="Plate"
        description="Plate of black man"
        tax="0.00"
        eanCode="1234569870123"
        eanType="EAN 13"/>
    <priceBracket quantity="1"
        price="15.98"
        grossPrice="18.00"/>
    <Product
        itemID="98765-3-"
        name="Plate"
        description="Plate of yellow man"
        tax="0.00"
        eanCode="7894567890123"
        eanType="EAN 13"/>
    <priceBracket quantity="1"
        price="20.98"
        grossPrice="24.00"/>
</rootElement>

XSLT 2.0

<xsl:stylesheet version="2.0" 
    xpath-default-namespace="http://Ecommerce.com/schemas/loyalist/3" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

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

    <xsl:template match="/*">
        <xsl:apply-templates select="*"/>
    </xsl:template>

    <xsl:template match="Product">
        <xsl:result-document href="{@itemID}.xml">
            <xsl:element name="{/*/name()}" 
                namespace="http://Ecommerce.com/schemas/loyalist/3">
                <xsl:copy-of select="/*/@*|.|following-sibling::priceBracket[1]"/>
            </xsl:element>
        </xsl:result-document>
    </xsl:template>

</xsl:stylesheet>

生成的 XML 文件(名称基于产品的 itemID,但可以轻松更改)...

09999-3-.xml

<rootElement xmlns="http://Ecommerce.com/schemas/loyalist/3"
             preOrderTo="2012-12-31T23:59:59"
             currency="GBP"
             timeStamp="2012-08-15T23:59:59">
   <Product itemID="09999-3-"
            name="Plate"
            description="Plate of blue man"
            tax="0.00"
            eanCode="1234567890123"
            eanType="EAN 13"/>
   <priceBracket quantity="1" price="10.98" grossPrice="13.00"/>
</rootElement>

12345-3-.xml

<rootElement xmlns="http://Ecommerce.com/schemas/loyalist/3"
             preOrderTo="2012-12-31T23:59:59"
             currency="GBP"
             timeStamp="2012-08-15T23:59:59">
   <Product itemID="12345-3-"
            name="Plate"
            description="Plate of black man"
            tax="0.00"
            eanCode="1234569870123"
            eanType="EAN 13"/>
   <priceBracket quantity="1" price="15.98" grossPrice="18.00"/>
</rootElement>

98765-3-.xml

<rootElement xmlns="http://Ecommerce.com/schemas/loyalist/3"
             preOrderTo="2012-12-31T23:59:59"
             currency="GBP"
             timeStamp="2012-08-15T23:59:59">
   <Product itemID="98765-3-"
            name="Plate"
            description="Plate of yellow man"
            tax="0.00"
            eanCode="7894567890123"
            eanType="EAN 13"/>
   <priceBracket quantity="1" price="20.98" grossPrice="24.00"/>
</rootElement>
于 2012-12-09T00:10:12.493 回答
0

使用拆分器组件并在 set payload 中添加根元素

<root>#[payload]</root>
于 2016-05-05T15:21:38.817 回答
-1

下面是使用vtd-xml编码拆分文档的代码,大约 20 行代码......

import com.ximpleware.*;
import java.io.*;
public class simpleSplit {
    public static void main(String[] s) throws VTDException, IOException{
        VTDGen vg = new VTDGen();
        if (!vg.parseFile("input.xml", true)) //namespace awareness disabled
            return;
        VTDNav vn = vg.getNav();
        AutoPilot ap = new AutoPilot(vn);
        XMLModifier xm = new XMLModifier(vn);
        ap.selectXPath("/rootElement/product");
        int i=0,j=1;
        byte[] ba1 = ("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>"+
                      "<rootElement xmlns=\"http://Ecommerce.com/schemas/loyalist/3\""+
                      "preOrderTo=\"2012-12-31T23:59:59\""+
                      "currency=\"GBP\""+
                      "timeStamp=\"2012-08-15T23:59:59\">\n").getBytes();
        byte[] ba =   "\n".getBytes();
        byte[] ba2 = "\n</rootElement>".getBytes();
        while((i=ap.evalXPath())!=-1){
            FileOutputStream fios = new FileOutputStream("file"+j+".xml");
            fios.write(ba1);//write starting tag
            vn.dumpFragment(fios);// write the product fragment
            fios.write(ba);
            if (vn.toElement(VTDNav.NEXT_SIBLING,"priceBracket")){
                // write the priceBracket fragment
                vn.dumpFragment(fios);
                vn.toElement(VTDNav.PREV_SIBLING);
            }
            fios.write(ba2);// write ending tag
            j++;
        }
    }
}
于 2016-05-04T21:34:15.197 回答