2016-07-28 19 views
-1

例如,我有很多的文件具有類似的模板:有一個簡單的方法在XML中的一個特定節點替換文本

<book book_id="foo"> 
    <title>Examples in different languages</title> 
    <book_section>Tutorials</book_section> 
     <body> 
     <p>Some text</p> 
     <section> 
      <sectionheader>if</sectionheader> 
      <sectionbody> 
       <p>Depending on language you use, the sytax will be different:</p> 
       <scriptsample> 
        <language_a> 
         <block> 
          <p> 
           &nbsp;&nbsp;...<br/> 
           &nbsp;&nbsp;<i>Do a comparation</i><br/> 
           &nbsp;&nbsp;<b>if</b> (a == b)<br/><br/> 
           &nbsp;&nbsp;{<br/> 
           &nbsp;&nbsp;&nbsp;&nbsp;do what you want<br/> 
           &nbsp;&nbsp;}<br/> 
          </p> 
         </block> 
        </language_a> 
        <language_b> 
         <block> 
          <p> 
           &nbsp;&nbsp;...<br/> 
           &nbsp;&nbsp;<i>Do a comparation</i><br/> 
           &nbsp;&nbsp;<b>if</b> (a == b)<br/><br/> 
           &nbsp;&nbsp;{<br/> 
           &nbsp;&nbsp;&nbsp;&nbsp;do what you want<br/> 
           &nbsp;&nbsp;}<br/> 
          </p> 
         </block> 
        </language_b> 
       </scriptsample> 
      </sectionbody> 
     </section> 
     <p>Some other text</p> 
     <section> 
      <sectionheader>while</sectionheader> 
      <sectionbody> 
       <p>Depending on language you use, the sytax will be different:</p> 
       <scriptsample> 
        <language_a> 
          <p> 
           &nbsp;&nbsp;...<br/> 
           &nbsp;&nbsp;<i>Do a comparation</i><br/> 
           &nbsp;&nbsp;<b>while</b> (a == b)<br/><br/> 
           &nbsp;&nbsp;{<br/> 
           &nbsp;&nbsp;&nbsp;&nbsp;do what you want<br/> 
           &nbsp;&nbsp;}<br/> 
          </p> 
        </language_a> 
        <language_b> 
          <p> 
           &nbsp;&nbsp;...<br/> 
           &nbsp;&nbsp;<i>Do a comparation</i><br/> 
           &nbsp;&nbsp;<b>while</b> (a == b)<br/><br/> 
           &nbsp;&nbsp;{<br/> 
           &nbsp;&nbsp;&nbsp;&nbsp;do what you want<br/> 
           &nbsp;&nbsp;}<br/> 
          </p> 
        </language_b> 
       </scriptsample> 
      </sectionbody> 
     </section> 
    <body> 
</book> 

現在,我想與compare(a, b)替換的文本(a == b)僅在 language_b元素中。

我設法通過在Altova XMLSpy中使用XPath來查找所有文件,但是我無法更改文件。

我試圖爲此創建一個正則表達式,但是我失敗了。

順便說,另一個障礙是ab可以是方法調用(obj.method(args))。所以,它真的很難在這裏使用正則表達式,但它是另一個任務)

我也試圖做一個XQuery應用更新元素的特徵(也Altova的XMLSpy的):

for $x in //language_b/*[contains(., 'a == b')] return 
replace value of node $x with (
replace($x, 'a == b', 'compare(a, b)') 
) 

但失去孩子「language_b」中的元素。 (如i,nbsp等)。 此外,Altova在整個文件中添加了默認屬性,這讓我因爲無用的元素而感到難過。

我不相信在我之前沒有這樣的任務。

有什麼建議嗎?

PS。現在我試圖通過在C#中使用DOM來創建一些東西(但由於缺乏知識需要很長時間)

+0

確定你想要使用的編程環境,然後給它一個搜索。它之前已經被問及過很多次。 – Gordon

+1

我會發布一些代碼片段來做到這一點在xpath和vtd-xml ..但你必須提供一個更具體的例子是什麼替代標準是... –

回答

0

嘗試使用dom或stax來做到這一點。

相關問題