【问题标题】:Append xml tree to another xml tree in PHP using DOMDocuments使用 DOMDocuments 将 xml 树附加到 PHP 中的另一个 xml 树
【发布时间】:2015-11-12 20:38:38
【问题描述】:

我希望将 xml 树附加到另一个。

例如,我想要以下xml:

<a>
  <b>
    <c/>
  </b>
</a>

要在其中包含以下 xml:

<n:d xmlns:xsl="namespace">
  <n:e>
    <n:f/>
  </n:e>
</n:d>

让它看起来像这样:

<a>
  <b>
    <c/>
    <n:d xmlns:n="namespace">
      <n:e>
        <n:f/>
      </n:e>
    </n:d>
  </b>
</a>

我尝试但未能执行此操作的代码如下:

$doc1 = new DOMDocument();
$doc2 = new DOMDocument();

$doc1->loadXML($xml1);
$doc2->loadXML($xml2);

$node_To_Insert = $doc2->getElementsByTagName('d')->item(0);
$node_To_Be_Inserted_To = $doc1->getElementsByTagName('b')->item(0);

$node_To_Be_Inserted_To->appendChild($doc1->importNode($node_To_Insert));

echo '<pre>'.htmlspecialchars(print_r($doc1->saveXML(),true)).'</pre>';

我从 echo 得到的当前结果:

<a>
  <b>
    <c/>
    <n:d xmlns:n="namespace" />
  </b>
</a>

我没有一些并非不可能阅读的想法,或者不是看似愚蠢的迂回曲折。

任何帮助将不胜感激。提前谢谢你。

【问题讨论】:

    标签: php xml domdocument


    【解决方案1】:

    您的解决方案非常接近。您只需执行deep copy with importNode 即可获得您想要的结果。

    $node_To_Be_Inserted_To->appendChild($doc1->importNode($node_To_Insert, true));
    

    【讨论】:

      【解决方案2】:

      另外,处理 XML 转换(例如 merging documents)的本地方式是使用 XSLT,这种专用语言主要针对这种需要重新构建、重新设置样式、重新格式化 XML 文档以供各种最终用途使用需要。

      就像另一种用于数据库的专用语言 SQL 一样,XSLT 对 XML 文件也很方便。 PHP 配备了 XSLT 处理器(可能需要启用扩展:php_xsl.so)。

      XSLT (另存为 .xsl 或 .xslt 文件)

      <xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
      <xsl:output version="1.0" encoding="UTF-8" indent="yes" />
      <xsl:strip-space elements="*" />
      
        <!-- Identity Transform -->
        <xsl:template match="@*|node()">
           <xsl:copy>
             <xsl:apply-templates select="@*|node()"/>
           </xsl:copy>
        </xsl:template>
      
        <xsl:template match="b">
          <b>
            <xsl:copy-of select="c" />
            <xsl:copy-of select="document('doc2.xml')"/>
          </b>
        </xsl:template> 
      </xsl:transform>
      

      PHP (仅加载第一个文档,因为上面的 XSLT 在特定节点加载第二个文档)

      $doc1 = new DOMDocument();    
      $doc1->load('doc1.xml');
      
      $xsl = new DOMDocument;
      $xsl->load('XSLTScript.xsl');
      
      // Configure the transformer
      $proc = new XSLTProcessor;
      $proc->importStyleSheet($xsl); 
      
      // Transform XML source
      $newXml = $proc->transformToXML($doc1);
      
      // Save output to file
      $xmlfile = 'Output.xml';
      file_put_contents($xmlfile, $newXml);
      

      输出

      <?xml version="1.0" encoding="UTF-8"?>
      <a>
        <b>
          <c/>
          <n:d xmlns:n="namespace">
            <n:e>
              <n:f/>
            </n:e>
          </n:d>
        </b>
      </a>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-12-24
        • 1970-01-01
        • 1970-01-01
        • 2019-11-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多