2013-06-04 33 views
3

我有以下問題:如何改變屬性值與augeas在XML中的位置變化?

我的XML(簡體):

<?xml version="1.0" encoding="UTF-8"?> 
<configuration> 
    <properties> 
    <property name="username">USERNAME</property> 
    <property name="anything">blabla</property> 
    </properties> 
</configuration> 

我需要更換與augeas Username值。 它工作正常:

augtool> set /files/test.xml/configuration/properties/property[1]/#text NEWUSER 

但問題是:用戶名條目不總是在一個位置。 augeas有沒有辦法用「匹配」或某種正則表達式來尋找位置?

augtool> match /files/test.xml/configuration/properties/*/#attribute/name username 

工作正常的結果

/files/test.xml/configuration/properties/property[1]/#attribute/name 

但我不知道如何設置值時使用此信息。

+0

默認情況下,augeas只能查看/ etc /和/ boot中的文件,您是否創建了自定義鏡頭? – spuder

回答

7

你需要做的是:

set /files/test.xml/configuration/properties/property[#attribute/name='username']/#text NEWUSER 

這將選擇屬性(/files/test.xml/configuration/properties/property),其#attribute/name子節點匹配username,並設置其#text子節點作爲NEWUSER

+0

工程就像一個魅力。謝謝! – Dakkar

+0

如何檢查插入一個新的具有上述屬性的XML元素(如果它不存在),並使用上述屬性修改XML元素(如果它存在)? – fruitJuice