簡體   English   中英

如何在XML文檔中的特定元素上添加屬性?

[英]How to add an attribute to specific elements in an XML document?

我有這個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>元素(有數百個元素)?

您所謂的“屬性”確實是另一個包含屬性的元素

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM