2012-08-15 25 views
0

我有一個問題:保存XML文檔,打破我的XSI聲明

我解析有與Python中的XML解析器(beautifulsoup)命名空間的XML,當我保存XML解析器代替:「 xsi:「在{http://www.w3.org/2001/XMLSchema-instance}的命名空間中,我怎麼能阻止他這樣做?

例子:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 

變爲:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" {http://www.w3.org/2001/XMLSchema-instance}schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 

誰能幫我這個?

問候, 博揚

+0

聽起來像BS中的錯誤。 – 2012-08-15 09:31:43

+0

我知道,我只是想弄清楚是否有人知道如何解決這個錯誤,因爲我找不到解決這個問題的快速方法。 – 2012-08-15 10:18:23

回答

0

這是怎麼了我暫時解決它。

soupOut = str(soup) 
ns = re.search("<project [^>]* xmlns:xsi=\"(?P<ns>[^\"]*)\"[^>]*>",soupOut) 
if ns: 
    soupOut = soupOut.replace("{%s}"%ns.group('ns'), 'xsi:') 
file.write(soupOut)