[英]Simple XML Xpath Query Issues
Completely new to using simple XML library in PHP, and have been using the w3 xpath syntax for assistance. 在PHP中使用简单XML库完全陌生,并且一直在使用w3 xpath语法提供帮助。
I have an xml file that looks roughly like this: 我有一个看起来像这样的xml文件:
<Xml_Configs>
<Foo_Module>
<Front_Name>a</Front_Name>
</Foo_Module>
<Bar_Module>
<Front_Name>b</Front_Name>
</Bar_Module>
<Baz_Module>
<Front_Name>c</Front_Name>
</Baz_Module>
</Xml_Configs>
I'm trying to figure out which module has the Front_Name of b. 我试图找出哪个模块的Front_Name为b。 Right now I've only been trying to get just the attribute to match, not caring about getting the parent, and here's what I've tried: 现在,我只想尝试使属性匹配,而不在乎获取父项,这是我尝试过的方法:
$xmlObj->xpath('/Xml_Configs/*[@Front_Name="b"]');
That gets me nothing, however: "/Xml_Configs/*/Front_Name" does give me an array of simple xml objects with a, b, and c. 但是,那什么都没给我:“ / Xml_Configs / * / Front_Name”确实给了我一个带有a,b和c的简单xml对象数组。 And "/Xml_Configs/*/[@Front_Name="b"]" gives me invalid expression errors. 和“ / Xml_Configs / * / [@ Front_Name =” b“]”给了我无效的表达式错误。
Any help you can give is appreciated, thanks! 感谢您提供的任何帮助,谢谢!
I'm trying to figure out which module has the Front_Name of b 我试图找出哪个模块的Front_Name为b
`$xmlObj->xpath('/Xml_Configs/*[@Front_Name="b"]');` That gets me nothing
Yes, because none of the elements that are children of the top element Xml_Configs
have any attributes. 是的,因为作为顶级元素Xml_Configs
子元素的元素Xml_Configs
没有任何属性。
You want : 您要 :
/*/*[Front_Name = 'b']
This selects all children of the top element that have a child named Font_Name
with string value "b"
. 这将选择top元素中具有名为Font_Name
且字符串值为"b"
所有子代。
XSLT - based verification : 基于XSLT的验证 :
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:copy-of select="/*/*[Front_Name = 'b']"/>
</xsl:template>
</xsl:stylesheet>
when this transformation is applied on the provided XML document : 当此转换应用于提供的XML文档时 :
<Xml_Configs>
<Foo_Module>
<Front_Name>a</Front_Name>
</Foo_Module>
<Bar_Module>
<Front_Name>b</Front_Name>
</Bar_Module>
<Baz_Module>
<Front_Name>c</Front_Name>
</Baz_Module>
</Xml_Configs>
it copies to the output the selected node : 它将所选节点复制到输出 :
<Bar_Module>
<Front_Name>b</Front_Name>
</Bar_Module>
I would recommend that you obtain the XPath Visualizer -- a tool that has helped thousands of developers learn XPath while having fun at the same time. 我建议您获得XPath Visualizer ,该工具已帮助成千上万的开发人员在乐趣中同时学习XPath。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.