我有一个 XML 文件,我想从一个位置复制一个文本节点并将其粘贴到不同位置的文本节点。
下面是我要操作的 XML 文件:
<?xml version="1.0" encoding="UTF-8"?>
<Main>
<installObj uniqueObjects="2822">
<object class="Installer" ">
<visualChildren>
<object class="InstallSet">
<installchildrenMain>
<object class="InstallBundle1">
<property name="ruleExpression">
<string><!***[CDATA[I Need a VALUE!]]***>
</string>
</property>
<property name="bundleName">
<string><![CDATA[Australia]]></string>
</property>
<rules logicalOperation="AND">
<object class="CompareVariable">
**
<property name="ruleId">
<string><!***[CDATA[CIAV819]]***>
</string>
</property>
**
</object>
</rules>
<installChildren>
<object class="InstallComponent1">
<property name="ruleExpression">
<string><!***[CDATA[CIAV962***]]>
</string>
</property>
<property name="componentName">
<string><![CDATA[AUST]]></string>
</property>
<rules logicalOperation="AND">
<object class="CompareVariable">
<property name="ruleId">
<string><!***[CDATA[CIAV962]***]>
</string>
</property>
</object>
</rules>
<installChildren>
<object class="InstallDirCont">
<property name="ruleExpression">
<string><![***CDATA[CIAV100 || CIAV503]***]>
</string>
</property>
<rules logicalOperation="OR">
<object class="CompareVariable1">
<property name="ruleId">
<string><***![CDATA[CIAV100]***]></string>
</property>
</object>
<object class="CompareVariable2">
<property name="ruleId">
<string><***![CDATA[CIAV503]***]></string>
</property>
</object>
</rules>
</object>
</installChildren>
</object>
#end of installComponent1
<object class="InstallComponent2">
======
Same as above (Install Component1)
=====
</object>
#end of installComponent2
</installchildren>
</object>
#installBundle1
<object class="InstallBundle2">
====
Same structure as InstallBundle1
====
</object>
#installBundle2
</installchildrenMain>
</object>
# End of InstallSet
</visualChildren>
# end of visualchildren
</object>
# end of Installer
</installObj>
<restorationObjects count="0" />
</Main>
现在我想在这里做的是:
从对象类 InstallBundle1 - 对象类 CompareVariable 复制“CIAV819”文本节点值并将其粘贴到对象类 InstallBundle1 的文本节点,就在 InstallChildrenMain 之前
从对象类 InstallComponent1 - 对象类 CompareVariable 复制“CIAV962”文本节点值并将其粘贴到对象类 InstallComponent1 的文本节点,就在 InstallChildren 之前
从对象类 InstallDirCont - 对象类 CompareVariable1 复制“CIAV100”文本节点值并添加操作数“||” 并从对象类 InstallDirCont - 对象类 CompareVariable2 复制“CIAV503”文本节点值,并将其粘贴到对象类 InstallDirCont 的文本节点,就在 InstallChildren 之前。
我知道这看起来有点整洁,但我需要这个修复。我对 xml 和 PHP 完全陌生,非常感谢您的帮助。
下面还有我在这里用来操作这个 xml 文件的 PHP 代码:
<?php
$xmldoc = new DOMDocument();
$xmldoc ->load('HR92_latest.iap_xml');
#$x = $xmldoc->getElementsByTagName('/InstallAnywhere_Deployment_Project/installationObjects[1]/object[1]/visualChildren[1]/object[1]/installChildren[1]/object[1]/rules[1]/object[1]/property[8]');
$x = $xmldoc->getElementsByTagName('//property[@name]');
var_dump ($x);
foreach ($x as $string) {
echo $string->nodeValue, PHP_EOL;
}
?>
但是这段代码正在返回 o/p:
object(DOMNodeList)#2 (1) { ["length"]=> int(0) }
基本上我在这里尝试首先找到xpath,然后是文本节点的值,但它失败了。任何帮助将不胜感激。谢谢!
2013 年 10 月 17 日 - 更新问题
@hielsnoppe:我发现使用 DOMDocument 的限制。因此,我想到了使用您的 xslt 代码。它几乎可以工作,但是在命令提示符下运行时,它会显示如下所示的输出:
<object class="InstallComponent1" objectID="03fe9">
<property><string>CIAV4</string></property>
<property name="componentName">
<string>BA, NT db2</string>
</property>
<rules logicalOperation="AND">
<object class="CompareVariable" objectID="0304">
<property name="ruleId">
<string>CIAV4</string>
</property>
</object>
</rules>
<installChildren>
<object class="InstallDirCont" objectID="03005">
<property><string>CIAV50 || CIAV60</string></property>
<rules logicalOperation="OR">
<object class="CompareVariable">
<property name="ruleId">
<string>CIAV50</string>
</property>
</object>
<object class="CompareVariable">
<property name="ruleId">
<string>CIAV60</string>
</property>
</object>
</rules>
</object>
</installChildren>
我的意思是输出显示为:
<object class="InstallComponent1" objectID="03fe9">
<property><string>CIAV962</string></property>
代替
<object class="InstallComponent1" objectID="03fe9">
<property name="ruleExpression"><string><![CDATA[CIAV962]]</string></property>
当我打开 xml 文件时,它仍然显示规则表达式字段为空白!!
请帮助我非常接近解决方案:)谢谢!
2013 年 10 月 18 日 - 更新
我是否必须添加 nodeType == XML_CDATA_SECTION_NODE?在规则表达式的模板匹配中?