What's wrong with my XPath/XML?

后端 未结 3 504
梦如初夏
梦如初夏 2020-12-05 09:09

I\'m trying a very basic XPath on this xml (same as below), and it doesn\'t find anything. I\'m trying both .NET and this website, and XPaths such as //PropertyGroup

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-05 09:39

    You can add namespaces in your code and all that, but you can effectively wildcard the namespace. Try the following XPath idiom.

    //*[local-name()='PropertyGroup']
    //*[local-name()='MSBuildCommunityTasksPath']
    

    name() usually works as well, as in:

    //*[name()='PropertyGroup']
    //*[name()='MSBuildCommunityTasksPath']
    

    EDIT: Namespaces are great and i'm not suggesting they're not important, but wildcarding them comes in handy when cobbling together prototype code, one-off desktop tools, experimenting with XSLT, and so forth. Balance your need for convenience against acceptable risk for the task at hand. FYI, if need be, you can also strip or reassign namespaces.

提交回复
热议问题