简单XML Xpath查询问题


Simple XML Xpath Query Issues

对于在PHP中使用简单的XML库完全陌生,并且一直在使用w3 xpath语法来获得帮助。

我有一个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>

我试图找出哪个模块有b的Front_Name。现在我只试图得到只是属性匹配,不关心得到父,这是我所尝试的:

$xmlObj->xpath('/Xml_Configs/*[@Front_Name="b"]');

这让我什么都没有,然而:"/Xml_Configs/*/Front_Name"确实给了我一个数组的简单的xml对象与a, b和c.和"/Xml_Configs/*/[@Front_Name="b"]"给我无效的表达式错误。

任何你能给的帮助是感激的,谢谢!

我想找出哪个模块有b的Front_Name

    `$xmlObj->xpath('/Xml_Configs/*[@Front_Name="b"]');`
 That gets me nothing

是,因为顶端元素Xml_Configs的子元素没有任何属性。

你想要的

:

/*/*[Front_Name = 'b']

选择top元素的所有子元素,子元素名为Font_Name,字符串值为"b"

基于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>

,当对提供的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>

它将所选节点复制到输出:

<Bar_Module>
   <Front_Name>b</Front_Name>
</Bar_Module>

我建议您获取XPath Visualizer——这个工具已经帮助成千上万的开发人员在学习XPath的同时获得乐趣。