'XPath selector based on element above

I'm not sure if my request is even possible, but since Wordpress XML export is a mess I need to find a way of getting what I need. Here is some example XML output that I have.

<xml>
  <wp:postmeta>
    <wp:meta_key>_thumbnail_id</wp:meta_key>
    <wp:meta_value><![CDATA[108]]></wp:meta_value>
  </wp:postmeta>
  <wp:postmeta>
    <wp:meta_key>_aioseop_keywords</wp:meta_key>
    <wp:meta_value><![CDATA[keyword1, keyword2]]>
  </wp:meta_value>
</xml>

Now I am trying to get the value of keywords, I have tried to use an exact path selector but the problem is sometimes the keyword element area may sit above the _thumbnail_id.

I want to get the value of 'wp:meta_value' based on 'wp:meta_key' value being equal to '_aioseop_keywords'.

How can this be achieved, the other less favourable alternative is how to get Wordpress to properly extract decent XML.



Solution 1:[1]

This XPath

/xml/wp:postmeta[wp:meta_key = '_aioseop_keywords']/wp:meta_value

will give you the

value of 'wp:meta_value' based on 'wp:meta_key' value being equal to '_aioseop_keywords'.

for the XML you provided. Be sure to register the namespace prefix.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1