简体   繁体   English

Xpath在同一棵树中查找特定值和其他标签的值

[英]Xpath find specific value and values of other tags in the same tree

Using PHP, SimpleXML and XPATH; 使用PHP,SimpleXML和XPATH; I want to find a specific serviceId and then find the DMA and Abbreviation values. 我想找到一个特定的serviceId,然后找到DMA和缩写值。 I can find a specific serviceId using $xml->xpath(//d:ServiceId[.= '123']) , but I am not sure how to modify the xpath query to also return the DMA and Abbreviation as well. 我可以使用$xml->xpath(//d:ServiceId[.= '123'])找到一个特定的serviceId,但是我不确定如何修改xpath查询以也返回DMA和缩写。

<?xml>
    <feed xmlns:m="" xmlns:d="">
        <entry>
            <content>
                <m:properties>
                    <d:ServiceId>123</d:ServiceId>
                    <d:ServiceName>Service 1</d:ServiceName>
                    <d:DMA>DMA 1</d:DMAName>
                    <d:Abbreviation>ABC</d:Abbreviation>
                </m:properties>
            </content>
            <content>
                <m:properties>
                    <d:ServiceId>456</d:ServiceId>
                    <d:ServiceName>Service 2</d:ServiceName>
                    <d:DMA>DMA 2</d:DMAName>
                    <d:Abbreviation>DEF</d:Abbreviation>
                </m:properties>
            </content>
            <content>
                <m:properties>
                    <d:ServiceId>789</d:ServiceId>
                    <d:ServiceName>Service 3</d:ServiceName>
                    <d:DMA>DMA 3</d:DMAName>
                    <d:Abbreviation>HIJ</d:Abbreviation>
                </m:properties>
            </content>
        </entry>
    </feed>
</xml>

You need to get m:properties node first. 您需要首先获取m:properties节点。 To get it use this XPath: 要获取它,请使用以下XPath:

//m:properties[d:ServiceId = 123]

After that just query this node to retrieve d:DMA and d:Abbreviation elements. 之后,只需查询此节点即可检索d:DMAd:Abbreviation元素。 Sorry, I don't know PHP but it should be straightforward. 抱歉,我不了解PHP,但是它应该很简单。

As alternative try this XPath with union operator: 或者,尝试使用带有联合运算符的XPath:

//m:properties[d:ServiceId = 123]/d:DMA | //m:properties[d:ServiceId = 123]/d:Abbreviation

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

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