简体   繁体   English

XML DOMDocument PHP-查询

[英]XML DOMDocument php - query

I've got this problem. 我有这个问题。 I have an xml file which contains this code: 我有一个包含以下代码的xml文件:

<Products>
<Product>
    <ID_Product>1</ID_Product>
    <ID_ProductKind>green</ID_ProductKind>
    <ID_ProductStatus>apple</ID_ProductStatus>
    </Product>
    <Product>
    <ID_Product>2</ID_Product>
    <ID_ProductKind>yellow</ID_ProductKind>
    <ID_ProductStatus>banana</ID_ProductStatus>
    </Product>
 </Products>

Now I would like to select ID_ProductKind and ID_ProductStatus where ID_Product is equal to one(1). 现在,我想选择ID_Product等于one(1)的ID_ProductKind和ID_ProductStatus。 Anyway the result of my query should be: 无论如何,我的查询结果应该是:

green apple 青苹果

It's so simple in mysql. 在mysql中是如此简单。 How can I do this in DOMDocument? 如何在DOMDocument中做到这一点?

Regards, David 问候,大卫

I assume you're looking for a basic XPath expression. 我假设您正在寻找基本的XPath表达式。

It's not particularly elegant, but you might try something like this: "//ID_ProductKind[preceding-sibling::ID_Product='1']/text()". 它不是特别优雅,但是您可以尝试如下操作:“ // ID_ProductKind [preceding-sibling :: ID_Product ='1'] / text()”。

You can use something similar for ID_ProductStatus. 您可以对ID_ProductStatus使用类似的内容。

I'm not quite sure about concatenating the values -- perhaps you can handle this in your php code. 我不太确定是否要串联这些值-也许您可以在php代码中进行处理。 I see that there's a string-join function in later versions of XPath. 我看到更高版本的XPath中有一个字符串联接函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM