如何复制&;在XML中粘贴文本节点
我有一个XML文件,我想从一个位置复制一个文本节点,然后将它粘贴到另一个位置的文本节点 下面是我要处理的XML文件:如何复制&;在XML中粘贴文本节点,xml,Xml,我有一个XML文件,我想从一个位置复制一个文本节点,然后将它粘贴到另一个位置的文本节点 下面是我要处理的XML文件: <?xml version="1.0" encoding="UTF-8"?> <Main> <installObj uniqueObjects="2822"> <object class="Installer" "> <visualChildren>
<?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>
在下面的例子中,我假设输入文件看起来是这样的,但是没有注释。在所有解决方案中,您可能都必须根据需要调整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>
使用我建议将以下保存为pick\u a\u有意义的\u 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>
复制粘贴是在文本编辑器中执行的操作。因为你可能不会问:你想复制一个节点并将其插入其他地方,还是想重新定位一个节点?@Mike'Pomax'Kamermans:嗨,迈克,谢谢你的快速回复。我不想通过xml编辑器手动执行此操作。我正试图使这个过程自动化。因此,我想复制一个节点并将其插入其他位置。请查看我的更新答案并告诉我,复制粘贴位置是否正确。从你的描述中我不完全清楚。
<object class="InstallComponent1" objectID="03fe9">
<property><string>CIAV962</string></property>
<object class="InstallComponent1" objectID="03fe9">
<property name="ruleExpression"><string><![CDATA[CIAV962]]</string></property>
<?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
$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
}
<?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
$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