2012-04-25 138 views
0

A有一個字符串,如'<node attr="some_value">'。如何從這個字符串中刪除attr="some_value"?我知道只有attr屬性名稱並且不知道"some_value"的值。
P.S.我使用的是JavaScript,但任何語言的解決方案都很棒。提前致謝。從字符串中刪除XML屬性

回答

2

試試這個:需要jQuery的。

var xml = '<node attr="some_value">'; 
var newXml = $(xml).removeAttr('attr'); 
0

經典解決方案:使用字符串函數,用於爲例:

str = str.substring(0,str.indexOf("attr")-1) + ">" 
1

使用Regexs與XML乞求災難的路線玩。我會使用內置的Xml功能來做到這一點。

w3schools.com

xmlDoc=loadXMLDoc("books.xml"); 

x=xmlDoc.getElementsByTagName('book'); 

document.write(x[0].getAttribute('category')); document.write("<br />"); 

x[0].removeAttribute('category'); 

document.write(x[0].getAttribute('category')); 

凡XML是

<bookstore> 
<book category="cooking"> 
<title lang="en">Everyday Italian</title> 
<author>Giada De Laurentiis</author> 
<year>2005</year> 
<price>30.00</price> 
</book> 
<book category="children"> 
<title lang="en">Harry Potter</title> 
<author>J K. Rowling</author> 
<year>2005</year> 
<price>29.99</price> 
</book> 
<book category="web"> 
<title lang="en">XQuery Kick Start</title> 
<author>James McGovern</author> 
<author>Per Bothner</author> 
<author>Kurt Cagle</author> 
<author>James Linn</author> 
<author>Vaidyanathan Nagarajan</author> 
<year>2003</year> 
<price>49.99</price> 
</book> 
<book category="web" cover="paperback"> 
<title lang="en">Learning XML</title> 
<author>Erik T. Ray</author> 
<year>2003</year> 
<price>39.95</price> 
</book> 
</bookstore>