0

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
  • //category/type[@name="SWEATERS"] ? Commented Oct 11, 2010 at 11:26
  • I want type name with product id =1 Commented Oct 11, 2010 at 11:28

2 Answers 2

2

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

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

xPath Syntax

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

Comments

2

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');

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.