如何将属性添加到XML文档中的特定元素?

时间:2016-02-24 13:35:26

标签: xml

我有这个XML文档:

<?xml version="1.0" encoding="utf-8"?>
<tmx version="1.4">
  <header creationtool="xxx" creationtoolversion="1.0" o-tmf="Format" datatype="xml" segtype="sentence" adminlang="en-US" srclang="en-US" creationdate="20160224T124311Z" creationid="xxx">
    <prop type="x-Quality:Integer"></prop>
    <prop type="x-Comment:SingleString"></prop>
    <prop type="x-Recognizers">RecognizeAll</prop>
    <prop type="x-TMName">templates</prop>
    <prop type="x-TokenizerFlags">DefaultFlags</prop>
    <prop type="x-WordCountFlags">BreakOnTag</prop>
  </header>
  <body>
    <tu creationdate="20160224T130930Z" creationid="alignment" changedate="20160224T131623Z" changeid="alignment" lastusagedate="20160224T130930Z">
      <prop type="x-Origin">Alignment</prop>
      <prop type="x-ConfirmationLevel">Draft</prop>
      <prop type="x-Comment:SingleString">Testing</prop>
      <prop type="x-Quality:Integer">50</prop>
      <tuv xml:lang="en-US">
        <seg>Version 10, 02/2016</seg>
      </tuv>
      <tuv xml:lang="lt-LT">
        <seg>10 versija, 2016/02</seg>
      </tuv>
    </tu>
    <tu creationdate="20160224T130930Z" creationid="alignment" changedate="20160224T130930Z" changeid="alignment" lastusagedate="20160224T130930Z">
      <prop type="x-Origin">Alignment</prop>
      <prop type="x-ConfirmationLevel">Draft</prop>
      <prop type="x-Quality:Integer">88</prop>
      <tuv xml:lang="en-US">
        <seg>ANNEX I</seg>
      </tuv>
      <tuv xml:lang="lt-LT">
        <seg>I PRIEDAS</seg>
      </tuv>
    </tu>
    <tu creationdate="20160224T130930Z" creationid="alignment" changedate="20160224T130930Z" changeid="alignment" lastusagedate="20160224T130930Z">
      <prop type="x-Origin">Alignment</prop>
      <prop type="x-ConfirmationLevel">Draft</prop>
      <prop type="x-Quality:Integer">93</prop>
      <tuv xml:lang="en-US">
        <seg>SUMMARY OF PRODUCT CHARACTERISTICS</seg>
      </tuv>
      <tuv xml:lang="lt-LT">
        <seg>PREPARATO CHARAKTERISTIKŲ SANTRAUKA</seg>
      </tuv>
    </tu>
    <tu creationdate="20160224T130930Z" creationid="alignment" changedate="20160224T130930Z" changeid="alignment" lastusagedate="20160224T130930Z">
      <prop type="x-Origin">Alignment</prop>
      <prop type="x-ConfirmationLevel">Draft</prop>
      <prop type="x-Quality:Integer">91</prop>
      <tuv xml:lang="en-US">
        <seg><bpt i="1" type="pt40" x="1" />&lt;<ept i="1" /><ph x="2" type="ph41" /><bpt i="3" type="pt42" x="3" />This medicinal product is subject to additional monitoring.<ept i="3" /></seg>
      </tuv>
      <tuv xml:lang="lt-LT">
        <seg><bpt i="1" type="pt40" x="1" />&lt;<ept i="1" /><ph x="2" type="ph41" /><bpt i="3" type="pt42" x="3" />Vykdoma papildoma šio vaistinio preparato stebėsena.<ept i="3" /></seg>
      </tuv>
    </tu>
    <tu creationdate="20160224T130930Z" creationid="alignment" changedate="20160224T130930Z" changeid="alignment" lastusagedate="20160224T130930Z">
      <prop type="x-Origin">Alignment</prop>
      <prop type="x-ConfirmationLevel">Draft</prop>
      <prop type="x-Quality:Integer">90</prop>
      <tuv xml:lang="en-US">
        <seg>This will allow quick identification of new safety information.</seg>
      </tuv>
      <tuv xml:lang="lt-LT">
        <seg>Tai padės greitai nustatyti naują saugumo informaciją.</seg>
      </tuv>
    </tu>
  </body>
</tmx>

我想将下面写的这个属性添加到所有“body”元素“tu”:

<prop type="x-Comment:SingleString">Testing</prop>

(我已经将它添加到第一个元素中作为示例。)我可以使用什么工具以及如何将此属性添加或复制到所有<tu>元素(其中有数百个)?

1 个答案:

答案 0 :(得分:1)

你叫什么&#34;属性&#34;确实是包含属性的另一个元素

<prop type="x-Comment:SingleString">Testing</prop>

您可以使用的工具是XSLT处理器。他们将输入XML转换为输出XML。

在此XSLT中使用XSLT处理器可提供所需的输出:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

  <xsl:template match="@* | node()" priority="-1"> <!-- copy everything unless another template with higher priority says otherwise -->
    <xsl:copy>
      <xsl:apply-templates select="@* | node()" />
    </xsl:copy>
  </xsl:template>

  <xsl:template match="tu">
    <xsl:copy>
      <xsl:apply-templates select="@*" />          <!-- copies all attributes of 'tu' -->
      <prop type="x-Comment:SingleString">Testing</prop>
      <xsl:apply-templates select="node()" />      <!-- copies all sub-nodes of 'tu' -->
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>