开发者

real example of using xmlns?

开发者 https://www.devze.com 2023-02-15 06:56 出处:网络
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.

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

精彩评论

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