ruby – 使用Nokogiri按名称查找节点
发布时间:2020-12-17 03:28:12 所属栏目:百科 来源:网络整理
导读:试图按名称查找节点.这是我的xml: Project ItemGroup Compile Include="....CommonAssemblyInfo.cs" LinkPropertiesCommonAssemblyInfo.cs/Link /Compile Compile Include="Global.asax.cs" DependentUponGlobal.asax/DependentUpon /Compile Compile In
试图按名称查找节点.这是我的xml:
<Project> <ItemGroup> <Compile Include="....CommonAssemblyInfo.cs"> <Link>PropertiesCommonAssemblyInfo.cs</Link> </Compile> <Compile Include="Global.asax.cs"> <DependentUpon>Global.asax</DependentUpon> </Compile> <Compile Include="PropertiesAssemblyInfo.cs" /> </ItemGroup> <ItemGroup> <ProjectReference Include="..myproject1.csproj"> <Name>Myproject1</Name> </ProjectReference> <ProjectReference Include="..Myproject2.csproj"> <Name>MyProject2</Name> </ProjectReference> <ProjectReference Include="..myproject3.csproj"> <Name>MyProject3</Name> </ProjectReference> </ItemGroup> </Project> 这是我从上面的XML获取所有Name节点的代码: f = File.open(projectpath) @doc = Nokogiri::XML(f) #print f.read names = @doc.xpath("Name") print names f.close 我的代码从XML搜索中得不到任何东西. 解决方法
您需要通配符路径构造(//),否则您只需查看根级别的元素.
names = @doc.xpath("//Name") 也许您正在考虑CSS搜索,它将使用您提供的字符串: names = @doc.css("Name") 或者您可能已经使用了搜索方法,该方法试图进行有根据的猜测,无论您使用的是CSS还是XPath.在这种情况下它会正常工作: names = @doc.search("Name") (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |