开发者

real example of using xmlns?

I am wondering anyone can give me an example of the real usage of xmln开发者_开发问答s? In XPath?


I assume that what you want is to access a node that is part of a different namespace than the default.

Given the following XML:

<root xmlns:x="http://www.example.com" xmlns:y="http://www2.example.com">
  <test>
    <x:child y:attr="hey" />
  </test>
</root>

If you wanted to select the child element which is part of the x namespace, all you have to do is use its full name in your XPath expression:

//test/x:child

It works for attributes too:

//test/x:child/@y:attr


I guess that the most popular XML vocabulary in a namespace is XHTML:

<html xmlns="http://www.w3.org/1999/xhtml">
 <body>
  <h1>Hello</h1>
 </body>
</html>

The way to write an easily understandable XPath expression selecting the h1 element is:

  1. Register in your XPath engine a namespace binding some prefix (say "x") to the XHTML namespace ("http://www.w3.org/1999/xhtml").

  2. Use this XPath expression:

    /x:html/x:body/x:h1

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜