Reference to undeclared namespace prefix when parsing MSXML

前端 未结 3 1145
一生所求
一生所求 2021-02-02 13:15

How do I solve the

Reference to undeclared namespace prefix: \'%s\'

problem with Microsoft\'s msxml implementation?


I\'m using a

3条回答
  •  半阙折子戏
    2021-02-02 14:11

    Using SelectionNamespaces is the correct approach, you are just missing a namespace.

    Notice that your XML document explicitly sets the default namespace as follows:

    xmlns="http://purl.org/rss/1.0/"
    

    This means that any element without a prefix, such as the item element, is actually in the default namespace. So if you want to select that element with an XPath expression, you must first set an appropriate selection namespace.

    To do this, you can change your call to setProperty like so:

    doc.setProperty('SelectionNamespaces', 'xmlns:rss="http://purl.org/rss/1.0/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:cb="http://www.cbwiki.net/wiki/index.php/Specification_1.1"');
    

    Here you've assigned the default namespace from the document to the rss: prefix in your XPath expression. With that change in place, the following XPath expression should work correctly:

    nodes = doc.selectNodes('/rdf:RDF/rss:item/cb:statistics/cb:exchangeRate/cb:targetCurrency');
    

    It works because it references the item element using the correct namespace. The fact that the prefix differs between the XPath expression and the original document is immaterial. It is the namespace which the prefix is bound to that matters.

提交回复
热议问题