2

Completely new to using simple XML library in PHP, and have been using the w3 xpath syntax for assistance.

I have an xml file that looks roughly like this:

<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. 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. And "/Xml_Configs/*/[@Front_Name="b"]" gives me invalid expression errors.

Any help you can give is appreciated, thanks!

0

1 Answer 1

1

I'm trying to figure out which module has the Front_Name of 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.

You want:

/*/*[Front_Name = 'b']

This selects all children of the top element that have a child named Font_Name with string value "b".

XSLT - based verification:

<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_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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.