Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
625 views
in Technique[技术] by (71.8m points)

xpath - Select xml node by attribute name ignoring namespace of that attribute

I have a node like this:

<meta name="og:description" content="Here's the content" />

I want to be able to select this element if the name is "description" whether it's in a namespace or not. I need to be able to select the meta tag if it's name is "og:description", "description", "blah:description", etc.

I've seen resources for xpath that show how to select within a namespace, but not irrespective of a namespace.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use:

//meta[@*[local-name() = 'description']]

This selects all meta elements in the XML document that have an attribute with local-name "description".

By definition, the standard XPath function local-name() produces the name of the node from which the namespace prefix (if any) is stripped off.

Do note: Always avoid using the // pseudo operator if the structure of the XML document is statically known. Often using // causes slow execution.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...