tags:

views:

56

answers:

2

I have a xml file

<category>
    <type name="SWEATERS">
        <product id =1>
            <itemNumber>1</itemNumber>
            <name>ProductA2</name>
            <price>345</price>
        </product>
   </type>
   <type name="PANTS">
        <product id=2>
            <itemNumber>4</itemNumber>
            <name>Product Test </name>
            <price>456</price>
        </product>
    </type>
</category>

I used the xquery

$xml->xpath('//category/type/product[@id=1]');

The out put will give the itemNumber name and price.How i can get the type name i mean SWEATERS

+2  A: 

You can get this using the parent .., then the @name attribute.

$xml->xpath('//category/type/product[@id=1]/../@name');

xPath Syntax

Gus
+2  A: 

You are looking for the name attribute of the type with a product element with an id of 1:

$xml->xpath('//category/type[product/@id=1]/@name');
Oliver Hallam