首页 文章

保存XML文档,打破了我的XSI声明

提问于
浏览
0

我有个问题:

我正在使用python xml解析器(beautifulsoup)解析具有命名空间的XML,当我保存该xml时,解析器将使用{http://www.w3.org/2001/XMLSchema-instance}替换名称空间中的"xsi:"我怎么能阻止他这样做呢?

例:

<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">

任何人都可以帮我解决这个问题吗?

此致,Bojan

2 回答

  • 1

    filed a bug for you.我也提交了一个修复程序,它将在下一个版本的Beautiful Soup中发布 .

  • 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)
    

相关问题