2015-11-04 63 views
0

我有一個字符串,其中包含一些帶命名空間的xml標記,我試圖從結束標記中刪除命名空間。我試過使用下面的方法SoaupUI - Groovy替換全部

staticData = staticData.replaceAll('/Group xmlns="http://socialservices.gov.au/ebo/QualityIndicators"','/Group>') 

它沒有找到字符串中的文本。任何人都可以看到我要去哪裏錯了

感謝

回答

0

你表達它只是用來刪除特定的標籤特定的命名空間(除了你添加和額外>到組)。

可以嘗試使用replaceAll更具通用性,使用下面的正則表達式從任何已關閉的標記中刪除名稱空間。

def staticData = 
'''<root> 
<Group> 
</Group xmlns="http://a"> 
<Group> 
</Group xmlns="http://b"> 
<Group> 
</Group xmlns="http://socialservices.gov.au/ebo/QualityIndicators"> 
<Different> 
</Different xmlns="http://socialservices.gov.au/ebo/QualityIndicators"> 
<Normal> 
</Normal> 
</root>''' 

staticData = staticData.replaceAll(/\<\/(\w*)\s[\S-\>]*\>/){ match, capture -> 
    return "</$capture>" 
} 

println staticData 

此腳本返回:

<root> 
<Group> 
</Group> 
<Group> 
</Group> 
<Group> 
</Group> 
<Different> 
</Different> 
<Normal> 
</Normal> 
</root> 

正則表達式的解釋/\<\/(\w*)\s[\S-\>]*\>/

它通過捕獲該組((\w*))0或n個字符與</\<\/),接着開始的文本匹配,然後跟着一個空格(\s),然後除了空格和> 0或n次([\S-\>]*)和最終ly > char(\>)。

希望它有幫助,