合并多个 XML 文件

发布于 2024-11-05 17:32:38 字数 374 浏览 0 评论 0原文

我可以使用什么语言来组合多个 XML 文件。多个为 10 多个文件。

PHP、java 还是什么?

我尝试使用 XSLT,但我不知道是否需要像 Saxon 这样的“处理器”。

这些文档令人困惑,因为我不知道从哪里开始。

总而言之,我需要有人为我指明正确的方向。

请有人帮忙。我已经尝试解决这个问题好几天了

<xml version="1.0">
<products>
<price>Price List Here</price>
<model>Model Number Here</model>
</product>

What language can I use to combine multiple XML files. Multiple as 10+ files.

PHP, java, or what?

I tried to use XSLT but I do not know if I need a 'processor' such as Saxon.

The docs were to confusing as I did not know where to start.

All in all, I need someone to point me in the right direction.

Someone please help. I've been trying to figure this out for days

<xml version="1.0">
<products>
<price>Price List Here</price>
<model>Model Number Here</model>
</product>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鲜血染红嫁衣 2024-11-12 17:32:38

这可以在纯 XSLT 中轻松完成

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:param name="pdoc1Url" select="'doc1.xml'"/>
 <xsl:param name="pdoc2Url" select="'doc2.xml'"/>

 <xsl:template match="/">
  <documents>
    <xsl:copy-of select="document($pdoc1Url)"/>
    <xsl:copy-of select="document($pdoc2Url)"/>
  </documents>
 </xsl:template>
</xsl:stylesheet>

上面的代码处理两个 XML 文档,但可以扩展以处理任何预先已知数量的 XML 文档。

说明

  1. 将 XML 文档的 URL 作为全局/外部参数传递给转换。

  2. 使用标准 XSLT 函数document()

This can be done easily in pure XSLT:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:param name="pdoc1Url" select="'doc1.xml'"/>
 <xsl:param name="pdoc2Url" select="'doc2.xml'"/>

 <xsl:template match="/">
  <documents>
    <xsl:copy-of select="document($pdoc1Url)"/>
    <xsl:copy-of select="document($pdoc2Url)"/>
  </documents>
 </xsl:template>
</xsl:stylesheet>

The code above deals with two XML documents, but can be extended to deal with any, known in advance number of XML documents.

Explanation:

  1. Passing the URLs for the XML documents as global/external parameters to the transformation.

  2. Use of the standard XSLT function document().

季末如歌 2024-11-12 17:32:38

您可以使用任何允许您直接操作 xml 的语言。我建议寻找 DOM 而不是 SAX 的东西。如果您使用 SAX,您基本上必须自己遍历 xml - 根据我的经验,这是一个皮塔饼。 DOM 允许您以更加 OOP 的方式对 xml 进行操作。

立即浮现在脑海中的可能是 xml“文档”的包装器 xml。

所以类似:

<documents>
   <document>
      <!-- Your xml here -->
   </document>
   <document>
      <!-- Your xml here -->
   </document>
   <document>
      <!-- Your xml here -->
   </document>
</documents>

伪代码将是:
创建文档根目录。
添加一个名为“文档”的元素,并将其用作根。
迭代每个 xml 文件。
为每个文件创建一个名为 document 的新元素。将该元素添加到父元素。从文件加载 xml。将该节点导入到外部文档中。将导入的节点追加到文档元素子集合中。

编辑
正如这里所承诺的,这是经过测试的更新代码,我知道它可以工作:

<?php

    // Replace the strings below with the actual filenames, add or decrease as fit
    $filenames = array(0 => "test.xml", 1 => "test2.xml", 2 => "test3.xml" );

    $docList = new DOMDocument();

    $root = $docList->createElement('documents');
    $docList->appendChild($root);

    foreach($filenames as $filename) {

        $doc = new DOMDocument();
        $doc->load($filename);

        $xmlString = $doc->saveXML($doc->documentElement);

        $xpath = new DOMXPath($doc);
        $query = "//product";  // this is the name of the ROOT element

        $nodelist = $xpath->evaluate($query, $doc->documentElement);

        if( $nodelist->length > 0 ) {

            $node = $docList->importNode($nodelist->item(0), true);

            $xmldownload = $docList->createElement('document');
            $xmldownload->setAttribute("filename", $filename);
            $xmldownload->appendChild($node);

            $root->appendChild($xmldownload);
        }

    }

    echo $docList->saveXML();
?>

You can use any language that allows you to manipulate xml directly. I suggest finding something with DOM rather than SAX. If you use SAX you have to basically traverse the xml yourself - a pita in my experience. DOM allows you to act on the xml in a more OOP manner.

Something that springs immediately to mind would be a wrapper xml around your xml "documents".

So something like:

<documents>
   <document>
      <!-- Your xml here -->
   </document>
   <document>
      <!-- Your xml here -->
   </document>
   <document>
      <!-- Your xml here -->
   </document>
</documents>

The pseudo code would be:
Create a document root.
Add an element called documents, use that as the root.
Iterate each of your xml files.
For each file create a new element called document. Add that element to the parent. Load the xml from the file. Import that node into the outer document. Append the imported node into the document elements child collection.

EDIT
As promised here is the updated code that was tested and I know works:

<?php

    // Replace the strings below with the actual filenames, add or decrease as fit
    $filenames = array(0 => "test.xml", 1 => "test2.xml", 2 => "test3.xml" );

    $docList = new DOMDocument();

    $root = $docList->createElement('documents');
    $docList->appendChild($root);

    foreach($filenames as $filename) {

        $doc = new DOMDocument();
        $doc->load($filename);

        $xmlString = $doc->saveXML($doc->documentElement);

        $xpath = new DOMXPath($doc);
        $query = "//product";  // this is the name of the ROOT element

        $nodelist = $xpath->evaluate($query, $doc->documentElement);

        if( $nodelist->length > 0 ) {

            $node = $docList->importNode($nodelist->item(0), true);

            $xmldownload = $docList->createElement('document');
            $xmldownload->setAttribute("filename", $filename);
            $xmldownload->appendChild($node);

            $root->appendChild($xmldownload);
        }

    }

    echo $docList->saveXML();
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文