我有一个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>
现在我想做的是:
从Object类复制“CIAV819”文本节点值InstallBundle1 - 对象类CompareVariable并将其粘贴到对象类InstallBundle1的文本节点上,就在InstallChildrenMain之前
从Object类复制“CIAV962”文本节点值InstallComponent1 - 对象类CompareVariable并在InstallChildren
之前将其粘贴到对象类InstallComponent1的文本节点从Object类InstallDirCont复制“CIAV100”文本节点值 - 对象类CompareVariable1并添加操作数“||”并从Object类InstallDirCont复制“CIAV503”文本节点值 - 对象类CompareVariable2并在InstallChildren之前将其粘贴到对象类InstallDirCont的文本节点。
我知道这看起来有点整洁,但我需要这个修复。我是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){ [ “长度”] =&GT; 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?在模板匹配规则表达式?
答案 0 :(得分:1)
在下面我假设输入文件看起来像这样但没有评论。在所有解决方案中,您可能必须根据需要调整XPath选择器。
<?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 /><!-- PASTE1 -->
</property>
<property name="bundleName">
<string><![CDATA[Australia]]></string>
</property>
<rules logicalOperation="AND">
<object class="CompareVariable">
<property name="ruleId">
<string><![CDATA[CIAV819]]></string><!-- COPY1 -->
</property>
</object>
</rules>
<installChildren>
<object class="InstallComponent1">
<property name="ruleExpression">
<string /><!-- PASTE2 -->
</property>
<property name="componentName">
<string><![CDATA[AUST]]></string>
</property>
<rules logicalOperation="AND">
<object class="CompareVariable">
<property name="ruleId">
<string><![CDATA[CIAV962]]></string><!-- COPY2 -->
</property>
</object>
</rules>
<installChildren>
<object class="InstallDirCont">
<property name="ruleExpression">
<string /><!-- PASTE3 -->
</property>
<rules logicalOperation="OR">
<object class="CompareVariable1">
<property name="ruleId">
<string><![CDATA[CIAV100]]></string><!-- COPY3.1 -->
</property>
</object>
<object class="CompareVariable2">
<property name="ruleId">
<string><![CDATA[CIAV503]]></string><!-- COPY3.2 -->
</property>
</object>
</rules>
</object>
</installChildren>
</object><!-- end of installComponent1 -->
<object class="InstallComponent2" >
<!--
Same as above (Install Component1)
-->
</object><!-- end of installComponent2 -->
</installChildren>
</object><!-- end of installBundle1 -->
<object class="InstallBundle2" >
<!--
Same structure as InstallBundle1
-->
</object><!-- end of installBundle2 -->
</installchildrenMain>
</object><!-- end of InstallSet -->
</visualChildren><!-- end of visualChildren -->
</object><!-- end of Installer -->
</installObj>
<restorationObjects count="0"/>
</Main>
如果逻辑必须在PHP中完成,请尝试使用DOMXPath
而不是getElementsByTagName()
来遍历相应的PASTE位置,并从相应的COPY位置提取数据。我不会详细说明这一点,因为我更喜欢下面的XSLT解决方案。
<?php
$xmldoc = new DOMDocument();
$xmldoc->load('HR92_latest.iap_xml');
$xpath = new DOMXPath($xmldoc);
$paste1 = $xpath->query('//object[starts-with(@class, 'InstallBundle')]/property[@name='ruleExpression']');
$paste2 = $xpath->query('//installChildren/object[starts-with(@class, 'InstallComponent')]/property[@name='ruleExpression']');
$paste3 = $xpath->query('//installChildren/object/installChildren/object[@class='InstallDirCont']/property[@name='ruleExpression']');
foreach ($paste1 as $p) {
// find corresponding COPY location and pull in the data
}
foreach ($paste2 as $p) {
// find corresponding COPY location and pull in the data
}
foreach ($paste3 as $p) {
// find corresponding COPY locations and pull in the data
}
使用XSLT我建议将保存为pick_a_meaningful_name.xslt
的以下转换应用于上述示例形式的XML文档,以生成所需结果。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output cdata-section-elements="string" />
<xsl:template match="@*|node()">
<xsl:copy><xsl:apply-templates select="@*|node()" /></xsl:copy>
</xsl:template>
<xsl:template match="//object[starts-with(@class, 'InstallBundle')]/property[@name='ruleExpression']">
<xsl:copy>
<xsl:copy-of select="../rules/object/property/string" />
</xsl:copy>
</xsl:template>
<xsl:template match="//installChildren/object[starts-with(@class, 'InstallComponent')]/property[@name='ruleExpression']">
<xsl:copy>
<xsl:copy-of select="../rules/object/property/string" />
</xsl:copy>
</xsl:template>
<xsl:template match="//installChildren/object/installChildren/object[@class='InstallDirCont']/property[@name='ruleExpression']">
<xsl:copy>
<string>
<xsl:for-each select="../rules[@logicalOperation='OR']/object">
<xsl:value-of select="./property/string" />
<xsl:if test="position() < count(../object)"><xsl:value-of select="' || '" /></xsl:if>
</xsl:for-each>
</string>
</xsl:copy>
</xsl:template>
</xsl:transform>
执行此操作的PHP代码的最小示例可能使用XSLTProcessor
,如下所示:
<?php
$xmldoc = new DOMDocument();
$xmldoc->load('HR92_latest.iap_xml');
$xsldoc = new DOMDocument();
$xsldoc->load('pick_a_meaningful_name.xslt');
$xslt = new XSLTProcessor();
$xslt->importStylesheet($xsldoc);
$result = $xslt->transformToDoc($xmldoc);
$result->save('HR92_latest.iap_xml'); // writes result to original file
echo $result->saveXML(); // echoes result