【发布时间】:2013-11-08 22:03:06
【问题描述】:
我目前正在尝试获取包含用于翻译的数据的简单 XML 表的数据,其结构如下:
<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 -->
有时后面会有 cmets 为译者多描述一下内容。 我想得到那些,虽然我设法写了一条评论,但我无法获得 1 条可靠的评论......
我的想法是: 例如,如果我想获得“action_setting”的评论,我使用 xpath 来选择这个区域:
<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 -->
我已经尝试使用此代码进行存档:
<?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." ");
}
?>
如您所见,注释行只是在我的特定元素之后选择每个注释节点并选择行中的第一个。这样做的问题是我无法确保评论真的在特定元素之后,或者只是在几行之后的元素评论。(所以如果我想得到“action_error”,它会给我“属于“action_menu”的评论2”
如您所见,我已经尝试选择此所需区域,但当有评论时它根本不返回任何内容。(我的来源:XPath select all elements between two specific elements)
因此,如果您能解释一下我在两个特定元素之间使用 cmets 所面临的问题的解决方案,我将不胜感激。
【问题讨论】:
-
您能否增强您的第二个示例摘录以实际包含您想要检索的内容?这是为了测试。
-
添加了 cmets。并用一个例子解释了我第一种获取 cmets 的方法的问题。