XPath背后的故事是什么以及对名称空间的支持?XPath作为规范在命名空间之前吗?如果我有一个文档,其中元素已被赋予默认命名空间:
<foo xmlns="uri" />
Run Code Online (Sandbox Code Playgroud)
似乎某些XPath处理器库//foo由于名称空间而无法识别,而其他库将会识别.我的团队所考虑的选项是使用正则表达式向XPath添加名称空间前缀(可以通过XmlNameTable添加名称空间前缀),但这似乎很脆弱,因为在节点测试时XPath是一种非常灵活的语言.
是否有适用于此的标准?
我的方法有点hackish但似乎工作正常; 我xmlns用搜索/替换删除声明,然后应用XPath.
string readyForXpath = Regex.Replace(xmldocument, "xmlns=\".+\"", String.Empty );
Run Code Online (Sandbox Code Playgroud)
这是一种公平的方法还是有人以不同方式解决了这个问
我正在尝试在我的RSS阅读器中添加对stackoverflow提要的支持,但SelectNodes和SelectSingleNode没有任何效果.这可能与我还不了解的ATOM和xml命名空间有关.
我已经通过从feed标签中删除所有属性来实现它,但这是一个黑客,我想正确地做到这一点.那么,你如何使用带有原子馈送的SelectNodes?
这是一个饲料片段.
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule" xmlns:thr="http://purl.org/syndication/thread/1.0">
<title type="html">StackOverflow.com - Questions tagged: c</title>
<link rel="self" href="http://stackoverflow.com/feeds/tag/c" type="application/atom+xml" />
<subtitle>Check out the latest from StackOverflow.com</subtitle>
<updated>2008-08-24T12:25:30Z</updated>
<id>http://stackoverflow.com/feeds/tag/c</id>
<creativeCommons:license>http://www.creativecommons.org/licenses/by-nc/2.5/rdf</creativeCommons:license>
<entry>
<id>http://stackoverflow.com/questions/22901/what-is-the-best-way-to-communicate-with-a-sql-server</id>
<title type="html">What is the best way to communicate with a SQL server?</title>
<category scheme="http://stackoverflow.com/feeds/tag/c/tags" term="c" /><category scheme="http://stackoverflow.com/feeds/tag/c/tags" term="c++" /><category scheme="http://stackoverflow.com/feeds/tag/c/tags" term="sql" /><category scheme="http://stackoverflow.com/feeds/tag/c/tags" term="mysql" /><category scheme="http://stackoverflow.com/feeds/tag/c/tags" term="database" />
<author><name>Ed</name></author>
<link rel="alternate" href="http://stackoverflow.com/questions/22901/what-is-the-best-way-to-communicate-with-a-sql-server" />
<published>2008-08-22T05:09:04Z</published>
<updated>2008-08-23T04:52:39Z</updated>
<summary type="html"><p>I am going to …Run Code Online (Sandbox Code Playgroud) 我有一个简单的要求,我需要获取属性的值,xml:id即af1.我正在使用a SAXParser而这里是我xpath:a/aff/@xml:id的相反我能够获取使用的值xpath:a/aff/@value.
但我无法找回价值,请你帮忙吗?
<?xml version="1.0" encoding="UTF-8" ?>
<a>
<aff xml:id="af1" value="a">
<uAff>
Hello
</uAff>
</aff>
<aff xml:id="corr1">
<uAff>
Hello1
</uAff>
</aff>
</a>
Run Code Online (Sandbox Code Playgroud)
提前致谢.
我在这个xml上尝试一个非常基本的XPath (与下面相同),它没有找到任何东西.我正在尝试.NET和这个网站,以及XPaths等//PropertyGroup,/PropertyGroup并且//MSBuildCommunityTasksPath根本不适合我(他们编译但返回零结果).
源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.Tasks\bin\Debug</MSBuildCommunityTasksPath>
</PropertyGroup>
<Import
Project="$(MSBuildProjectDirectory)\MSBuild.Community.Tasks\MSBuild.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 Files\Microsoft FxCop 1.32\Xml\FxCopReport.xsl" />
</Target>
</Project>
Run Code Online (Sandbox Code Playgroud) 我有这个带有命名空间的XML文档,我想用XPath提取一些节点.
这是文件:
<ArrayOfAnyType xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
<anyType xsi:type="Document">
<Id>5</Id>
<Title>T1</Title>
</anyType>
<anyType xsi:type="Document">
<Id>15</Id>
<Title>T15</Title>
</anyType>
</ArrayOfAnyType>
Run Code Online (Sandbox Code Playgroud)
如果我想用xsi:type ="Document"提取所有"anyType"元素,XPath表达式会是什么?
我试过这个:
//anyType[@xsi:type="Document"]
Run Code Online (Sandbox Code Playgroud)
它不起作用:
xmllint --xpath "//project" test.xml
Run Code Online (Sandbox Code Playgroud)
失败了
<?xml version="1.0" encoding="UTF-8"?>
<projects>
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
</project>
</projects>
Run Code Online (Sandbox Code Playgroud)
但如果我删除xmlns属性,请成功:
<?xml version="1.0" encoding="UTF-8"?>
<projects>
<project>
<modelVersion>4.0.0</modelVersion>
</project>
</projects>
Run Code Online (Sandbox Code Playgroud)
这有什么问题吗?xmlns在非顶级标签上是否合法?
我正在使用Java Maven:
mvn help:effective-pom
Run Code Online (Sandbox Code Playgroud)
并在非顶部元素上生成带有xmlns的xml,如图所示.
这个问题是对已回答问题的跟进: XmlDocument.SelectSingleNode和前缀+ xmlNamespace问题
问题是,将来可能会在没有警告的情况下更改收到的xml的名称空间前缀,因此我们想知道是否有任何方法可以使用SelectSingleNode但省略元素的前缀.
(我们知道我们可以删除传入xml的所有前缀,但它需要更多步骤....虽然如果提供代码我们会认为它是一个有效的答案......)
xml ×6
xpath ×4
c# ×3
namespaces ×3
.net ×1
android ×1
atom-feed ×1
java ×1
maven ×1
rss ×1
saxparser ×1
selectnodes ×1
xml-parsing ×1
xmllint ×1
xmlnode ×1