SimpleXML:选择具有特定属性值的元素

发布于 2024-07-24 00:49:57 字数 530 浏览 7 评论 0原文

在 XML 文档中,我有共享相同名称的元素,但属性的值定义了它的数据类型,并且我想从文档中选择所有具有特定值的元素。 我是否需要使用 XPath(如果需要,您能否建议正确的语法)或者是否有更优雅的解决方案?

下面是一些示例 XML:

<object>
  <data type="me">myname</data>
  <data type="you">yourname</data>
  <data type="me">myothername</data>
</object>

我想选择类型为 me的所有 子级标记的内容。

PS - 我正在尝试使用 PHP 与 Netflix API 进行交互 - 这对我的问题来说应该不重要,但如果你想建议一个好的/更好的方法来做到这一点,我洗耳恭听。

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?

Here's some example XML:

<object>
  <data type="me">myname</data>
  <data type="you">yourname</data>
  <data type="me">myothername</data>
</object>

And I want to select the contents of all <data> tags children of <object> who's type is me.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笙痞 2024-07-31 00:49:57

试试这个 XPath:

/object/data[@type="me"]

读作:

  • 选择 (/) 当前元素的子元素 (object)
  • 选择 (/) 他们的子元素 () data
  • 过滤器 ([...]) 列出元素,其中...
    • 属性类型@ 表示“属性”)
    • 具有文本值me

所以:

$myDataObjects = $simplexml->xpath('/object/data[@type="me"]');

如果 object 不是文档的根目录,您可能需要使用 //object/data[@type="me"] 代替。 // 表示“查找所有后代”而不是“查找所有子代”。

Try this XPath:

/object/data[@type="me"]

Which reads as:

  • Select (/) children of the current element called object
  • Select (/) their children called data
  • Filter ([...]) that list to elements where ...
    • the attribute type (the @ means "attribute")
    • has the text value me

So:

$myDataObjects = $simplexml->xpath('/object/data[@type="me"]');

If object is not the root of your document, you might want to use //object/data[@type="me"] instead. The // means "find all descendents" rather than "find all children".

与酒说心事 2024-07-31 00:49:57

我只是做了一个函数来为我做这件事; 但它只获取第一个结果。 你的旅费可能会改变。

function query_attribute($xmlNode, $attr_name, $attr_value) {
  foreach($xmlNode as $node) { 
    if($node[$attr_name] == $attr_value) {
        return $node;
    }
  }
}

用法:(

echo query_attribute($MySimpleXmlNode->Customer, "type", "human")->Name;

对于下面的 XML)

<Root><Customer type="human"><Name>Sam Jones</name></Customer></Root>

I just made a function to do this for me; it only grabs the first result though. Your mileage may vary.

function query_attribute($xmlNode, $attr_name, $attr_value) {
  foreach($xmlNode as $node) { 
    if($node[$attr_name] == $attr_value) {
        return $node;
    }
  }
}

Usage:

echo query_attribute($MySimpleXmlNode->Customer, "type", "human")->Name;

(For the XML below)

<Root><Customer type="human"><Name>Sam Jones</name></Customer></Root>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文