1

我有一个 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>

现在我想在这里做的是:

  1. 从对象类 InstallBundle1 - 对象类 CompareVariable 复制“CIAV819”文本节点值并将其粘贴到对象类 InstallBundle1 的文本节点,就在 InstallChildrenMain 之前

  2. 从对象类 InstallComponent1 - 对象类 CompareVariable 复制“CIAV962”文本节点值并将其粘贴到对象类 InstallComponent1 的文本节点,就在 InstallChildren 之前

  3. 从对象类 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?在规则表达式的模板匹配中?

4

1 回答 1

2

在下文中,我假设输入文件看起来像这样,但没有注释。在所有解决方案中,您可能必须根据需要调整 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() &lt; 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
于 2013-10-10T08:46:47.983 回答