xpath 选择 注释节点如何访问包含命名空间的节点

使用 XPath 导航选择节点
不是 IT 专业人员?
TechNet 库
要查看英语原文,请勾选“英语”复选框。也可将鼠标指针移到文本上,在弹出窗口中显示英语原文。
使用 XPath 导航选择节点
Visual Studio 2010
可以使用 XPath 查找单个特定节点,或查找与某个条件匹配的所有节点。
方法返回符合选择条件的第一个节点。
方法返回包含匹配节点的 。
方法来选择其作者姓氏符合指定条件的第一个 book 节点。
bookstore.xml 文件(在本主题末尾提供)用作输入文件。
// Load the document and set the root element.
XmlDocument doc = new XmlDocument();
doc.Load("bookstore.xml");
XmlNode root = doc.DocumentE
// Add the namespace.
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("bk", "urn:newbooks-schema");
// Select and display the first node in which the author's
// last name is Kingsolver.
XmlNode node = root.SelectSingleNode(
"descendant::bk:book[bk:author/bk:last-name='Kingsolver']", nsmgr);
Console.WriteLine(node.InnerXml);
方法来选择其价格大于指定金额的所有书节点。
然后,以编程方式将选定列表中的每本书的价格减去 10%。
bookstore.xml 文件(在本主题末尾提供)用作输入文件。
// Load the document and set the root element.
XmlDocument doc = new XmlDocument();
doc.Load("bookstore.xml");
XmlNode root = doc.DocumentE
// Add the namespace.
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("bk", "urn:newbooks-schema");
// Select all nodes where the book price is greater than 10.00.
XmlNodeList nodeList = root.SelectNodes(
"descendant::bk:book[bk:price&10.00]", nsmgr);
foreach (XmlNode book in nodeList)
// Discount prices by 10%.
price = Math.Round(Convert.ToSingle(
book.LastChild.InnerText) * 0.9, 2);
book.LastChild.InnerText = price.ToString();
// Display the updated document.
doc.Save(Console.Out);
上面的示例从文档元素开始执行 XPath 查询。
设置 XPath 查询的起始点即设置了上下文节点,该节点是 XPath 查询的起始点。
如果不希望在文档元素处开始,而是希望从文档元素的第一个子级开始,可以编写 Select 语句的代码,如下所示:
this doc.DocumentElement.FirstChild.SelectNodes(. . .);
对象都与基础文档同步。
中修改节点时,也会修改基础文档。
注意修改基础文档时,最好重新运行此 Select 语句。
如果修改的节点可能导致该节点被添加到节点列表(当先前没有添加它时),或者现在会导致它从节点列表中被移除,则无法保证节点列表现在是精确的。
XPath 表达式可以包含命名空间。
支持命名空间解析。
来解析 bookstore.xml 文档的命名空间。
注意如果 XPath 表达式不包含前缀,则假定命名空间统一资源标识符 (URI) 是空的命名空间。
;否则,不会选择任何节点。
下面的 bookstore.xml 文件在本主题的示例中用作输入文件。
&?xml version='1.0'?&
&bookstore xmlns="urn:newbooks-schema"&
&book genre="novel" style="hardcover"&
&title&The Handmaid's Tale&/title&
&first-name&Margaret&/first-name&
&last-name&Atwood&/last-name&
&price&19.95&/price&
&book genre="novel" style="other"&
&title&The Poisonwood Bible&/title&
&first-name&Barbara&/first-name&
&last-name&Kingsolver&/last-name&
&price&11.99&/price&
&book genre="novel" style="paperback"&
&title&The Bean Trees&/title&
&first-name&Barbara&/first-name&
&last-name&Kingsolver&/last-name&
&price&5.99&/price&
&/bookstore&
此页面有帮助吗?
更多反馈?
1500 个剩余字符
我们非常感谢您的反馈。
页面加载速度够快吗?
您喜欢网页的设计吗?
请告诉我们更多意见&nbsp>&nbsp
&nbsp>&nbsp
&nbsp>&nbsp
dom4j中使用xpath解析带命名空间的xml文件,取不到节点的解决办法
摘要:使用DOM4J的xpath非常方便,但是,直接使用xpath取带命名空间的xm文件,会出现取不到节点的问题.具体问题如下&messageto=&&from=&/Smack&type=&chat&&&subject&zscasvgsadg&/subject&&body&sdvgsdvsaddzsf
使用 DOM4J 的xpath 非常方便,但是,直接使用xpath 取带命名空间的xm文件,会出现取不到节点的问题.具体问题如下
to=&& from=&/Smack& type=&chat&&&subject&zscasvgsadg&/subject&&body&sdvgsdvsaddzsfbnzsdfbvdas&/body&&thread&0pMmg6&/thread&&ext xmlns=&infoair:obcs:msg:ext&&&identify&3admin38&/identify&&sendType&NOR&/sendType&&awokeTime&60&/awokeTime&&x xmlns=&infoair:obcs:msg:source& var=&334345&/&&/ext&&/message&&
& 如果想取& identify 的值:& 使用xpath expression : “/message/ext/identify”& 将不能取到值& 取x的属性值& :& 使用xpath& expression: “/message/ext/identify/x/@var“;& 也不能取到x var 的属性值& 那么该怎么做了,直接上代码:
public static MessageNode getNode1(String msg) throws DocumentException
{SAXReader reader = new SAXReader();Document document = reader.read(new StringReader(msg));XPath xPath = document.createXPath(&/message/a:ext/a:identify&);Map&String, String& names = new HashMap&String, String&();names.put(&a&, &infoair:obcs:msg:ext&);
MessageNode messageNode = new MessageNode();xPath.setNamespaceURIs(names);Node node = xPath.selectSingleNode(document);messageNode.setIdentify(node.getText());xPath = document.createXPath(&/message/a:ext/b:x/@var&);names.put(&b&, &infoair:obcs:msg:source&);xPath.setNamespaceURIs(names);node = xPath.selectSingleNode(document);messageNode.setParentId(node.getText());return messageN}
names.put(&a&, &infoair:obcs:msg:ext&);
&& 只要 xpath 选择时,加上命名空间就行了&&& &a&& 表示前缀,&infoair:obcs:msg:ext&& 表示命名空间。 具体原因,请见:& XML 命名空间以及它们如何影响 XPath 和 XSLT (Extreme XML)
以上是的内容,更多
的内容,请您使用右上方搜索功能获取相关信息。
若你要投稿、删除文章请联系邮箱:zixun-group@service.aliyun.com,工作人员会在五个工作日内给你回复。
云服务器 ECS
可弹性伸缩、安全稳定、简单易用
&40.8元/月起
预测未发生的攻击
&24元/月起
邮箱低至5折
推荐购买再奖现金,最高25%
&200元/3月起
你可能还喜欢
你可能感兴趣
阿里云教程中心为您免费提供
dom4j中使用xpath解析带命名空间的xml文件,取不到节点的解决办法相关信息,包括
的信息,所有dom4j中使用xpath解析带命名空间的xml文件,取不到节点的解决办法相关内容均不代表阿里云的意见!投稿删除文章请联系邮箱:zixun-group@service.aliyun.com,工作人员会在五个工作日内答复
售前咨询热线
支持与服务
资源和社区
关注阿里云
InternationalJavascript is disabled
Please enable javascript and refresh the page
Try searching Microsoft Support to find a solution
Please enable cookies and refresh the page
CV: {{ getCv() }}jdom xpath定位带xmlns命名空间的节点
jdom xpath定位带xmlns命名空间的节点关键词:jdom xpath xmlns 命名空间 openjweb 在jdom中用 xpath定位节点通常采用以下方式:XPath xpath=Element anode =SAXBuilder sb = new SAXBuilder();Document doc =try{
doc = sb.build("xxx.xml");}catch(Exception ex){
return "解析xml失败!";}xpath = XPath.newInstance("/节点1/节点2[@属性1='值1']");anode=(Element)xpath.selectSingleNode(doc);但是在处理spring的bean文件时,发现这种方式定位不到想找的节点,下面是openjweb的core-service-demo.xml,现在要查找此文件里的hibernate配置,文件格式:&?xml version="1.0" encoding="GB2312"?&&beans xmlns="" xmlns:xsi="" xmlns:context=""
xmlns:tx=""
xsi:schemaLocation="
...... &bean id="demosessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&
&property name="dataSource"&
&ref local="demoDatasource" /&
&/property&
&property name="mappingResources"&
&value&org/openjweb/core/entity/CommSubSystem.hbm.xml&/value&
&/list&&/property&&/bean&&/beans&现在要通过程序查找list节点,但是按照下面的方式发现取不到list的Element:xpath = XPath.newInstance("/beans/bean/property[@name='mappingResources']/list");anode = (Element)xpath.selectSingleNode(doc );后来从网上查找解决方案,发现是因为beans根节点带有xmlns命名空间。需要注册命名空间,见下面的代码:xpath = XPath.newInstance("/ns:beans/ns:bean/ns:property[@name='mappingResources']/ns:list");//ns是随便起的名字xpath.addNamespace("ns","");anode = (Element)xpath.selectSingleNode(doc );上面定义了一个ns命名空间,另外在xpath的查找字符串中,每级节点都要增加 ns:,采用这种方式就可以查找list节点了。
没有更多推荐了,
加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!XPath 节点
XPath 节点
在 XPath 中,有七种类型的节点:元素、属性、文本、命名空间、处理指令、注释以及文档节点(或称为根节点)。
XPath 术语
节点(Node)
在 XPath 中,有七种类型的节点:元素、属性、文本、命名空间、处理指令、注释以及文档(根)节点。XML 文档是被作为节点树来对待的。树的根被称为文档节点或者根节点。
请看下面这个 XML 文档:
&?xml version=&1.0& encoding=&ISO-8859-1&?&
&bookstore&
&title lang=&en&&Harry Potter&/title&
&author&J K. Rowling&/author&
&year&2005&/year&
&price&29.99&/price&
&/bookstore&
上面的XML文档中的节点例子:
&bookstore& (文档节点)
&author&J K. Rowling&/author& (元素节点)
lang=&en& (属性节点)
基本值(或称原子值,Atomic value)
基本值是无父或无子的节点。
基本值的例子:
J K. Rowling
项目(Item)
项目是基本值或者节点。
父(Parent)
每个元素以及属性都有一个父。
在下面的例子中,book 元素是 title、author、year 以及 price 元素的父:
&title&Harry Potter&/title&
&author&J K. Rowling&/author&
&year&2005&/year&
&price&29.99&/price&
子(Children)
元素节点可有零个、一个或多个子。
在下面的例子中,title、author、year 以及 price 元素都是 book 元素的子:
&title&Harry Potter&/title&
&author&J K. Rowling&/author&
&year&2005&/year&
&price&29.99&/price&
同胞(Sibling)
拥有相同的父的节点
在下面的例子中,title、author、year 以及 price 元素都是同胞:
&title&Harry Potter&/title&
&author&J K. Rowling&/author&
&year&2005&/year&
&price&29.99&/price&
先辈(Ancestor)
某节点的父、父的父,等等。
在下面的例子中,title 元素的先辈是 book 元素和 bookstore 元素:
&bookstore&
&title&Harry Potter&/title&
&author&J K. Rowling&/author&
&year&2005&/year&
&price&29.99&/price&
&/bookstore&
后代(Descendant)
某个节点的子,子的子,等等。
在下面的例子中,bookstore 的后代是 book、title、author、year 以及 price 元素:
&bookstore&
&title&Harry Potter&/title&
&author&J K. Rowling&/author&
&year&2005&/year&
&price&29.99&/price&
&/bookstore&

我要回帖

更多关于 xpath 父节点 的文章

 

随机推荐