我的 XPath/XML 有什么问题? [英] What's wrong with my XPath/XML?

查看:13
本文介绍了我的 XPath/XML 有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 this xml 上尝试一个非常基本的 XPath(下同),但它没有找到任何东西.我正在尝试 .NET 和 this website,以及 XPath,例如 //PropertyGroup/PropertyGroup//MSBuildCommunityTasksPath 根本不适合我(它们编译但返回零结果).

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, /PropertyGroup and //MSBuildCommunityTasksPath are simply not working for me (they compiled but return zero results).

源 XML:

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <!-- $Id: FxCop.proj 114 2006-03-14 06:32:46Z pwelter34 $ -->
    <PropertyGroup>
        <MSBuildCommunityTasksPath>$(MSBuildProjectDirectory)MSBuild.Community.TasksinDebug</MSBuildCommunityTasksPath>
    </PropertyGroup>
    <Import
        Project="$(MSBuildProjectDirectory)MSBuild.Community.TasksMSBuild.Community.Tasks.Targets" />
    <Target Name="DoFxCop">
        <FxCop TargetAssemblies="$(MSBuildCommunityTasksPath)MSBuild.Community.Tasks.dll"
            RuleLibraries="@(FxCopRuleAssemblies)" 
            AnalysisReportFileName="Test.html"
            DependencyDirectories="$(MSBuildCommunityTasksPath)" 
            FailOnError="True"
            ApplyOutXsl="True"
            OutputXslFileName="C:Program FilesMicrosoft FxCop 1.32XmlFxCopReport.xsl" />
    </Target>
</Project>

推荐答案

您可以在代码中添加命名空间等等,但您可以有效地使用通配符命名空间.试试下面的 XPath 习惯用法.

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() 通常也可以,如:

name() usually works as well, as in:

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

命名空间很棒,我并不是说它们不重要,但是在将原型代码、一次性桌面工具、XSLT 试验和等等.平衡您对便利的需求与手头任务的可接受风险.仅供参考,如果需要,您还可以剥离或重新分配命名空间.

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.

这篇关于我的 XPath/XML 有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆