Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何复制一个元素(文本字符串)的内容并附加到另一个元素的内容?(XML)_Xml_Batch File_Xpath_Find_Copy - Fatal编程技术网

如何复制一个元素(文本字符串)的内容并附加到另一个元素的内容?(XML)

如何复制一个元素(文本字符串)的内容并附加到另一个元素的内容?(XML),xml,batch-file,xpath,find,copy,Xml,Batch File,Xpath,Find,Copy,我的任务是修改数千个问题文件。每个文件有一个问题 我需要修改一个像这样的文件 <xml> <question>What is the fourth planet from the Sun?</question> <answer choice="1">Mercury</answer> <answer choice="2">Venus</answer> <answer choice="3">Mars&

我的任务是修改数千个问题文件。每个文件有一个问题

我需要修改一个像这样的文件

<xml>

<question>What is the fourth planet from the Sun?</question>

<answer choice="1">Mercury</answer>
<answer choice="2">Venus</answer>
<answer choice="3">Mars</answer>

<feedback choice="1">Incorrect.</feedback>
<feedback choice="2">Incorrect.</feedback>
<feedback choice="3">Correct.</feedback>

<extrafeedback>Mars is the fourth planet from the Sun and is the second smallest planet in the solar system. Named after the Roman god of war, Mars is also often described as the "Red Planet" due to its reddish appearance. </extrafeedback>

</xml>

离太阳第四颗行星是什么?
水星
维纳斯
火星
不对。
不对。
对的
火星是离太阳第四远的行星,也是太阳系中第二小的行星。火星以罗马战神的名字命名,因其略带红色的外观,也常被称为“红色星球”。
…这件事。 在结束“应答”标签之前添加了“额外反馈”


离太阳第四颗行星是什么?
水星
维纳斯
火星
不对。火星是离太阳第四远的行星,也是太阳系中第二小的行星。火星以罗马战神的名字命名,因其略带红色的外观,也常被称为“红色星球”。
不对。火星是离太阳第四远的行星,也是太阳系中第二小的行星。火星以罗马战神的名字命名,因其略带红色的外观,也常被称为“红色星球”。
对的火星是离太阳第四远的行星,也是太阳系中第二小的行星。火星以罗马战神的名字命名,因其略带红色的外观,也常被称为“红色星球”。
火星是离太阳第四远的行星,也是太阳系中第二小的行星。火星以罗马战神的名字命名,因其略带红色的外观,也常被称为“红色星球”。

我有记事本++和Oxygen XML编辑器,但我愿意学习一些新东西。

通过对每个文档应用XSLT,可以很容易地实现这一点。使用一个专门模板修改
反馈/text()



使用oXygen,您可以配置一组文件。

使用PowerShell可以非常简单地完成此操作。如果愿意,只需将此脚本复制到剪贴板,打开PowerShell控制台,
cd
到包含XML文件副本的目录,然后粘贴即可

#//对于每个XML文件。。。
gci*.xml |%{
写入主机-nonewline“修补$($\名称)
#//读取其内容并转换为XML对象
[xml]$xml=gc$_
$extra=$xml.selectSingleNode(“//extrafeedback/text()”).data.trim()
#//对于尚未包含$extra的每个反馈元素的文本节点。。。
$xml.selectNodes(“//feedback/text()”)|?{$|.data-NotMatch$extra}|%{
$\数据+=“$extra”
}
#//将修改后的XML保存到原始文件
$xml.save($\ux)
将主机写为“完成”。-f绿色
}
PowerShell的XML解析器将删除XML文件中的所有孤立空白。也许将几个XML文件复制到一个测试目录中,然后首先进行测试运行。在将此脚本提交到数千个XML文件之前,请检查结果

<xml>

<question>What is the fourth planet from the Sun?</question>

<answer choice="1">Mercury</answer>
<answer choice="2">Venus</answer>
<answer choice="3">Mars</answer>

<feedback choice="1">Incorrect. Mars is the fourth planet from the Sun and is the second smallest planet in the solar system. Named after the Roman god of war, Mars is also often described as the "Red Planet" due to its reddish appearance.</feedback>
<feedback choice="2">Incorrect. Mars is the fourth planet from the Sun and is the second smallest planet in the solar system. Named after the Roman god of war, Mars is also often described as the "Red Planet" due to its reddish appearance.</feedback>
<feedback choice="3">Correct. Mars is the fourth planet from the Sun and is the second smallest planet in the solar system. Named after the Roman god of war, Mars is also often described as the "Red Planet" due to its reddish appearance.</feedback>

<extrafeedback>Mars is the fourth planet from the Sun and is the second smallest planet in the solar system. Named after the Roman god of war, Mars is also often described as the "Red Planet" due to its reddish appearance.</extrafeedback>

</xml>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
  <xsl:output indent="yes"/>

  <xsl:template match="@*|node()">
      <xsl:copy>
          <xsl:apply-templates select="@*|node()"/>
      </xsl:copy>
  </xsl:template>

  <xsl:template match="feedback/text()">
    <xsl:value-of select="., /xml/extrafeedback" separator=" "/>
  </xsl:template>

</xsl:stylesheet>