如何拆分 XML

2024-04-24

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

我不太确定需要什么技术来完成以下操作。

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

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

X路径? 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>

如果您可以使用 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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何拆分 XML 的相关文章

  • 如何找到给定字符串的最长重复子串

    我是java新手 我被分配寻找字符串的最长子字符串 我在网上研究 似乎解决这个问题的好方法是实现后缀树 请告诉我如何做到这一点或者您是否有任何其他解决方案 请记住 这应该是在 Java 知识水平较低的情况下完成的 提前致谢 附 测试仪字符串
  • 使用非 ASCII(自然语言)XML 标签是否合适?

    使用以非 ASCII 自然语言编写的 XML 标签 元素名称 是否合适 XML 规范允许这样做 请参阅Names http www w3 org TR 2006 REC xml11 20060816 NT Name and 例外情况 htt
  • 在 HTTPResponse Android 中跟踪重定向

    我需要遵循 HTTPost 给我的重定向 当我发出 HTTP post 并尝试读取响应时 我得到重定向页面 html 我怎样才能解决这个问题 代码 public void parseDoc final HttpParams params n
  • 制作一个交互式Windows服务

    我希望我的 Java 应用程序成为交互式 Windows 服务 用户登录时具有 GUI 的 Windows 服务 我搜索了这个 我发现这样做的方法是有两个程序 第一个是服务 第二个是 GUI 程序并使它们进行通信 服务将从 GUI 程序获取
  • Final字段的线程安全

    假设我有一个 JavaBeanUser这是从另一个线程更新的 如下所示 public class A private final User user public A User user this user user public void
  • 无法展开 RemoteViews - 错误通知

    最近 我收到越来越多的用户收到 RemoteServiceException 错误的报告 我每次给出的堆栈跟踪如下 android app RemoteServiceException Bad notification posted fro
  • 加速代码 - 3D 数组

    我正在尝试提高我编写的一些代码的速度 我想知道从 3d 整数数组访问数据的效率如何 我有一个数组 int cube new int 10 10 10 我用价值观填充其中 然后我访问这些值数千次 我想知道 由于理论上所有 3d 数组都存储在内
  • Liferay ClassNotFoundException:DLFileEntryImpl

    在我的 6 1 0 Portal 实例上 带有使用 ServiceBuilder 和 DL Api 的 6 1 0 SDK Portlet 这一行 DynamicQuery query DynamicQueryFactoryUtil for
  • 不使用 local-name() 或 name() 函数的 XPath

    我必须解析oprResult code使用 XPath 从下面的 XML 中获取 XPath 表达式 local name oprResult code 正在按预期工作 但是 我无法使用name or local name功能为 在我的解析
  • 我可以使用 HSQLDB 进行 junit 测试克隆 mySQL 数据库吗

    我正在开发一个 spring webflow 项目 我想我可以使用 HSQLDB 而不是 mysql 进行 junit 测试吗 如何将我的 mysql 数据库克隆到 HSQLDB 如果您使用 spring 3 1 或更高版本 您可以使用 s
  • 路径中 File.separator 和斜杠之间的区别

    使用有什么区别File separator和一个正常的 在 Java 路径字符串中 与双反斜杠相反 平台独立性似乎不是原因 因为两个版本都可以在 Windows 和 Unix 下运行 public class SlashTest Test
  • Spring @RequestMapping 带有可选参数

    我的控制器在请求映射中存在可选参数的问题 请查看下面的控制器 GetMapping produces MediaType APPLICATION JSON VALUE public ResponseEntity
  • Java TestNG 与跨多个测试的数据驱动测试

    我正在电子商务平台中测试一系列商店 每个商店都有一系列属性 我正在考虑对其进行自动化测试 是否有可能有一个数据提供者在整个测试套件中提供数据 而不仅仅是 TestNG 中的测试 我尝试不使用 testNG xml 文件作为机制 因为这些属性
  • 在两个活动之间传输数据[重复]

    这个问题在这里已经有答案了 我正在尝试在两个不同的活动之间发送和接收数据 我在这个网站上看到了一些其他问题 但没有任何问题涉及保留头等舱的状态 例如 如果我想从 A 类发送一个整数 X 到 B 类 然后对整数 X 进行一些操作 然后将其发送
  • Eclipse Java 远程调试器通过 VPN 速度极慢

    我有时被迫离开办公室工作 这意味着我需要通过 VPN 进入我的实验室 我注意到在这种情况下使用 Eclipse 进行远程调试速度非常慢 速度慢到调试器需要 5 7 分钟才能连接到远程 jvm 连接后 每次单步执行断点 行可能需要 20 30
  • 在mockito中使用when进行模拟ContextLoader.getCurrentWebApplicationContext()调用。我该怎么做?

    我试图在使用 mockito 时模拟 ContextLoader getCurrentWebApplicationContext 调用 但它无法模拟 here is my source code Mock org springframewo
  • Java列表的线程安全

    我有一个列表 它将在线程安全上下文或非线程安全上下文中使用 究竟会是哪一个 无法提前确定 在这种特殊情况下 每当列表进入非线程安全上下文时 我都会使用它来包装它 Collections synchronizedList 但如果不进入非线程安
  • 使用 x509 证书签署 json 文档或字符串

    如何使用 x509 证书签署 json 文档或字符串 public static void fund string filePath C Users VIKAS Desktop Data xml Read the file XmlDocum
  • 按日期对 RecyclerView 进行排序

    我正在尝试按日期对 RecyclerView 进行排序 但我尝试了太多的事情 我不知道现在该尝试什么 问题就出在这条线上适配器 notifyDataSetChanged 因为如果我不放 不会显示错误 但也不会更新 recyclerview
  • 如何实现仅当可用内存较低时才将数据交换到磁盘的写缓存

    我想将应用程序生成的数据缓存在内存中 但如果内存变得稀缺 我想将数据交换到磁盘 理想情况下 我希望虚拟机通知它需要内存并将我的数据写入磁盘并以这种方式释放一些内存 但我没有看到任何方法以通知我的方式将自己挂接到虚拟机中before an O

随机推荐

  • 格式化整数时 printf 中的精度字段

    当我执行这两行时 printf 5d n 3 use of precision filed printf 05d n 3 use of 0 flag to prepend with 0 我得到以下输出 00003 00003 结果相同 所以
  • Google 字体无法在移动设备中加载

    我读过类似的帖子 但这个问题有点不同 我有 rest of the code 在 css 样式文件中我有 body font family Source Sans Pro sans serif rest of the code 它在浏览器中
  • 将新对象附加到 JSON 文件中的数组

    如何将附加对象添加到现有 JSON 文件 即对象数组 中 这是我的 JS 代码 const fs require fs let Human Name John age 20 Human JSON stringify Human null 2
  • 自动完成搜索字符串的多个部分,然后返回最可能的部分

    有点像这个问题 https stackoverflow com questions 824144 how do i use jquery autocomplete for multiple words 我有很多文本片段 每天都会使用很多很多
  • 使用 nokogiri 干式搜索网站的每个页面

    我想搜索网站的每个页面 我的想法是找到页面上保留在域内的所有链接 访问它们 然后重复 我也必须采取措施 避免重复努力 所以开始很容易 page http example com nf Nokogiri HTML open page link
  • Azure Functions 中 PowerShell 脚本的选项在哪里

    我想使用 PowerShell 创建 Azure Function 当我谈到 Azure 希望我选择要创建的函数类型时 唯一可用的语言是 C F 和 JavaScript 我错过了什么吗 如何使用 PowerShell 创建 Azure 函
  • 尝试使用 Comparator 按名称排序、忽略大小写以及先处理空值

    我在使用 Java 8 Comparator 类对项目列表进行排序时遇到问题 我当前的工作比较器如下 comparator Comparator comparing Person getName Comparator nullsFirst
  • Android 中从时间戳获取日期名称

    我有一个类 当它初始化时 它会使用公共 getter 在私有字段中记录初始化时间 public class TestClass private long mTimestamp public TestClass mTimestamp Syst
  • 每个 ajax 请求都会调用 preRenderView

    我正在使用 jquery waypoints 和 jsf 实现无限滚动link http kahimyang info kauswagan code blogs 1405 building a page with infinite scro
  • CSS自定义组合框问题

    我需要一个自定义组合框 所以 我实施了ul 问题是我无法通过单击在顶部打开组合框列表button 展示的同时ul 它移动button到网页底部 Code ul width 100px background color rgb 224 224
  • 在 Emacs 中定义新的工具提示

    我想向 emacs 添加自定义工具提示 更具体地说 每当我将鼠标悬停在符号 函数 变量 名称上时 用我的鼠标我想看到带有符号定义的工具提示 我知道我可以使用 cscope 这样的工具找到此类信息 但我不知道如何找到 将 cscope 的输出
  • 运行烘焙命令时出现 SQLSTATE HY000 2002

    我在运行烘焙命令时遇到问题 我认为它与 mysql 有关 但我在 Stackoverflow 上没有找到此错误的任何解决方案 这是我的app php Datasources gt default gt className gt Cake D
  • Kafka的消息键有什么特别的地方吗?

    我没有看到任何提及消息键 org apache kafka clients producer ProducerRecord key 除了它们可以用于主题分区 我可以自由地将我喜欢的任何数据放入密钥中 还是有一些我应该遵守的特殊语义 该密钥似
  • 分组时间序列(面板)数据的交叉验证

    我使用面板数据 随着时间的推移 我观察许多单位 例如人 对于每个单元 我都有相同固定时间间隔的记录 当将数据分为训练集和测试集时 我们需要确保这两个集是不相交的并且顺序的 即训练集中的最新记录应该在测试集中最早的记录之前 参见例如此博客文章
  • 如何使用可用内存有效地比较 1,000 张图像

    这是一个棘手的问题 我的磁盘中存储了大约 1 000 张图像 我想通过成对比较来找到彼此相似的图像 所以我必须做周围1 000 999 2 https stackoverflow com questions 46958633 generat
  • 如何用 Java 以编程方式下载网页

    我希望能够获取网页的 html 并将其保存到String 这样我就可以对其进行一些处理 另外 我如何处理各种类型的压缩 我将如何使用 Java 来做到这一点 我会使用像样的 HTML 解析器Jsoup http jsoup org 那么就很
  • 如何通过部分名称查找文件夹和文件c#

    在我的硬盘驱动器的特定文件夹中 我存储了许多其他子文件夹和文件 现在我想按部分名称列出这些文件夹和文件名 for example c webapi xx folder c mvctutorial xx folder done webapi
  • Gradlew bundleRelease 不会在 React-Native 中生成发布 apk

    我尝试获取应用程序的apk 我以前也做过 效果很好 但是 我今天尝试使用其他应用程序的获取 apk 但它没有给我发布 apk 为什么 我跟着这些步骤 https facebook github io react native docs si
  • 如何更改 ComboFieldEditor 的内容?

    我想更改其中一个的值ComboFieldEditor取决于另一个ComboFieldEditor在 Eclipse 插件中 例如 如果用户更改package 需要在第二个中填充不同的类ComboFieldEditor The ComboFi
  • 如何拆分 XML

    我的第一篇文章在这里 我已经搜索过 但没有找到我要找的东西 我不太确定需要什么技术来完成以下操作 我使用 Mule 3 3 CE 我需要拆分 XML 文件 我需要在每个分割的 XML 中保留 rootElement 及其属性 所有 XML