3

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.

kjhughes
  • 106,133
  • 27
  • 181
  • 240
Key
  • 396
  • 4
  • 15

1 Answers1

3

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.

kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • Awesome, that worked exactly as i needed it to. Do you know of any documentation for it so i can fully understand the logic and similar selectors. Have looked but cannot find it. – Key Feb 05 '15 at 16:52
  • 1
    You can learn a lot from the examples given in the [Location Paths](http://www.w3.org/TR/xpath/#location-paths) section of the XPath Recommendation. – kjhughes Feb 05 '15 at 17:50