问题描述
在XML文档中,我具有共享相同名称的元素,但是属性的值定义了它是什么类型的数据,并且我想从文档中选择所有具有特定值的元素.我是否需要使用XPath(如果可以,建议使用正确的语法)还是有一个更优雅的解决方案?
In an XML document, I have elements which share the same name, but the value of an attribute defines what type of data it is, and I want to select all of those elements which have a certain value from the document. Do I need to use XPath (and if so, could you suggest the right syntax) or is there a more elegant solution?
以下是一些XML示例:
Here's some example XML:
<object>
<data type="me">myname</data>
<data type="you">yourname</data>
<data type="me">myothername</data>
</object>
我想选择类型为me
的所有<data>
标记子级<object>
的内容.
And I want to select the contents of all <data>
tags children of <object>
who's type is me
.
PS-我正在尝试使用PHP与Netflix API进行接口-这与我的问题无关紧要,但是如果您想提出一种更好的方法,我会全力以赴.
PS - I'm trying to interface with the Netflix API using PHP - this shouldn't matter for my question, but if you want to suggest a good/better way to do so, I'm all ears.
推荐答案
尝试使用此XPath:
Try this XPath:
/object/data[@type="me"]
所以:
$myDataObjects = $simplexml->xpath('/object/data[@type="me"]');
如果object
不是文档的根目录,请改用//object/data[@type="me"]
.
And if object
is not the root of your document, use //object/data[@type="me"]
instead.
这篇关于SimpleXML:选择具有特定属性值的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!