如何复制一个元素的内容(文本字符串)并附加到另一个元素的内容?(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.</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>

。进入这个。在结束的"答案"标签之前添加了"额外反馈"。

<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>

我有Notepad++和Oxygen XML编辑器,但我愿意学习新东西。

您可以通过将 XSLT 应用于每个文档来轻松实现此目的。使用修改后的身份转换,为feedback/text()提供专用模板:

<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>

使用 oXygen,您可以配置 XSLT 转换方案以将批量转换应用于一组文件。

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

# // For each XML file...
gci *.xml | %{
    write-host -nonewline "Patching $($_.Name)... "
    # // Read its contents and cast as an XML object
    [xml]$xml = gc $_
    $extra = $xml.selectSingleNode("//extrafeedback/text()").data.trim()
    # // For each feedback element's text node not already containing $extra...
    $xml.selectNodes("//feedback/text()") | ?{ $_.data -NotMatch $extra } | %{
        $_.data += " $extra"
    }
    # // Save modified XML to original file
    $xml.save($_)
    write-host "done." -f green
}

PowerShell的XML解析器将删除XML文件中的所有孤立空格。 也许将几个 XML 文件复制到测试目录中,然后先对其进行测试运行。 在将此脚本提交到数千个 XML 文件之前,请检查结果。

相关内容

最新更新