I'm currently trying to fetch the data of a simple XML-Sheet which contains data for translation use it's structured like this:
<string name="action_settings">Settings</string><!-- Comment1 -->
<string name="action_error">Something went wrong.</string>
<string name="action_menu">You've got the choice</string><!-- Comment2 -->
Sometimes there are comments after to describe the content a bit more for the translator. I'd like to get those, while I managed to write a comment, I couldn't manage to get 1 comment reliable ...
My idea is: If I want to get a comment of "action_setting" for example I use xpath to select this area:
<string name="action_settings">Settings</string>|AREASTART|<!-- Comment1 -->
|AREAEND|<string name="action_error">Something went wrong.</string>
<string name="action_menu">You've got the choice</string><!-- Comment2 -->
I already tried archieving this with this code:
<?php
$doc = new DOMDocument();
$doc->load('strings.xml');
$xpath = new DOMXPath($doc);
//foreach ($xpath->query("//string[@name='debug']/following::comment()[1]") as $comment)
foreach ($xpath->query("/*/string[count(preceding-sibling::string[@name='debug'])=1]/comment()[1]") as $comment)
{
var_dump($comment->textContent." ");
}
?>
As you can see the commented line is simply selecting every comment node after my specific element and picks the first one in row. The problem with this is that I can't make sure that the comment is really after the specific element or just the comment of an element a few lines later.(so if I'd like to get "action_error" it would give me "Comment 2" which belongs to "action_menu)
As you can see I tried already to select this desired area but it simply doesn't return anything when there's a comment.(My source: XPath select all elements between two specific elements)
So I'd be grateful if you would explain me a solution for this problem I'm facing with comments between 2 specific elements.