XML xpath attribute value result to contain other results

天涯浪子 提交于 2019-12-12 00:52:21

问题


I recently asked a question about how to select a parent's node attribute and its values at the same time using xpath, What i ended up with is :

Parent/@attr|Parent/x

It grab the parent attr's value and all of its x's nodes..

Array
(
    [0] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [attr] => attrValue
                )
        )

    [1] => SimpleXMLElement Object
        (
            [0] => 1
        )

    [2] => SimpleXMLElement Object
        (
            [0] => 2
        )

    [3] => SimpleXMLElement Object
        (
            [0] => 3
        )

)

The thing now.. In case there are several Parent nodes, it will mix them together and i wouldn't know which attr belongs to its x's .. it will result something like this :

Array
(
    [0] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [attr] => attrValue
                )
        )

    [1] => SimpleXMLElement Object
        (
            [0] => 1
        )

    [2] => SimpleXMLElement Object
        (
            [0] => 2
        )

    [3] => SimpleXMLElement Object
        (
            [0] => 3
        )

    [4] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [attr] => anotherAttrValue
                )
        )

    [5] => SimpleXMLElement Object
        (
            [0] => 1
        )

    [6] => SimpleXMLElement Object
        (
            [0] => 2
        )

    ...
)

You see, they are all treated as a normal result, what i am trying to do is to make the attr contain all of it's results ( 1, 2, 3 ) in an array or something and the same thing goes to the other attr's to end up with something like this :

Array
(
    [0] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [attr] => attrValue
                )
        )
        Array
        (
            [0] => SimpleXMLElement Object
                (
                    [0] => 1
                )

            [1] => SimpleXMLElement Object
                (
                    [0] => 2
                )

            [2] => SimpleXMLElement Object
                (
                    [0] => 3
                )
        )

    [1] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [attr] => anotherAttrValue
                )
        )
        Array
        (
            [0] => SimpleXMLElement Object
                (
                    [0] => 1
                )

            [1] => SimpleXMLElement Object
                (
                    [0] => 2
                )

            [2] => SimpleXMLElement Object
                (
                    [0] => 3
                )
        )
)

using the xpath and the way given at first

来源:https://stackoverflow.com/questions/13887094/xml-xpath-attribute-value-result-to-contain-other-results

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!