我测试的XML结构:如何使用XPath检索subsubchild?
<Articles>
...
<Article ID="333">
<author>Paul</author>
<title>i hate xpath </title>
<child1>bla</child1>
<child2>blabla
<subchild>
<subsubchild DEEP = "Attribute"></subsubchild> //this is my nightmare
</subchild>
</child2>
<child3>testing</child3>
</Article>
...
</Articles>
什么XPath表达式将获取并更新subsubchild
ATTRIB价值?如果父ID(本例中为333)是已知的,我主要关心的是获取和更新任何节点的子值,无论其位置有多深或多浅。
目前我使用:
$query= "//*[@ID=333]//*node()[@DEEP and @DEEP = "Attribute"]";
$outcome = $xml->xpath($query);
echo isset($outcome[0][0]) ? "found" : "unavailable";
和几个相关的表述是,他们都折腾我的无效表达报告工作。
function XMLChangeAttribValue($ParentID, $attirbName, $attribValue, $NewValue){
$xml //Never mind, i've create this sucessfully
$query = "//*[@ID=$ParentID]//*[@$attirbName = $attribValue]";
$outcome = $xml->xpath($query);
echo isset($outcome[0][0]) ? "found" : "unavailable";
}
我不知道你的问题是什么。 “无效的表达式报告”是否意味着XPath引擎报告错误?然后,请显示它。你使用PHP吗?然后,标记问题。 XPath不是用于更新XML文档的正确工具,请将您的预期输出显示为XML。 – 2015-01-04 15:04:22
避免个人言论 - 他们不完全帮助你的问题得到一个很好的答案。但是,处理我的意见将有所帮助。 – 2015-01-04 15:29:04
很抱歉马蒂亚斯如果我imodest响起,我没有故意的,我是唯一在乌尔说的XPath supprised不能使用(或者说是不正确的工具)用于更新XML。有什么更好的?一个foreach循环?以及通过更新我的意思是我想改变DEEP(属性)的价值为我选择的东西。如果你有想法,请告诉我。再一次,接受我的同情。 – 2015-01-04 15:43:02