SimpleXML get elements from attributes ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
JasonDFR
Forum Commoner
Posts: 40
Joined: Wed Jan 07, 2009 3:51 am

SimpleXML get elements from attributes ?

Post by JasonDFR »

I would like to use simpleXML ( or another method ONLY if ABSOLUTELY necessary ) to get all the children of an element with a certain attribute.

Example:

Code: Select all

<?xml version="1.0" encoding="ISO88591" ?>
<calendar ver="2.0">
    <yar yeartag="2009">
        <mn m="1">
            <dy d="1">
                <shorttext>Day 1 Short</shorttext>
                <link>Day 1 LINK</link>
                <longtext>Day 1 Long</longtext>
            </dy>
            <dy d="2">
                <shorttext />
                <link />
                <longtext />
            </dy>
            <dy d="3">
                <shorttext />
                <link />
                <longtext />
            </dy>
            <dy d="4">
                <shorttext />
                <link />
                <longtext />
            </dy>
        </mn>
        <mn m="2">
            <dy d="1">
                <shorttext />
                <link />
                <longtext />
            </dy>
            <dy d="2">
                <shorttext />
                <link />
                <longtext />
            </dy>
            <dy d="3">
                <shorttext>Month 2 day 3 short</shorttext>
                <link>LINK LINK</link>
                <longtext>Long text for day 2/3</longtext>
            </dy>
            <dy d="4">
                <shorttext />
                <link />
                <longtext />
            </dy>
        </mn>
        <mn m="3">
            <dy d="1">
                <shorttext />
                <link />
                <longtext />
            </dy>
            <dy d="2">
                <shorttext />
                <link />
                <longtext />
            </dy>
            <dy d="3">
                <shorttext>Month 3 day 3 short</shorttext>
                <link>LINK LINK</link>
                <longtext>Long text for day 3/3</longtext>
            </dy>
            <dy d="4">
                <shorttext />
                <link />
                <longtext />
            </dy>
        </mn>
    </yar>
</calendar>
I want all the days (dy) under the month (mn) that has the attribute m="1" .

I know I can loop through all the months and use an if ( $xml->yar->mn->attributes()->m == 1 ) then loop through each day and output whatever I want.

I don't want to loop through.

I want to just pick out all the days that are under the mn with the attribute of 1.

$days = $xml->yar->mn( WHERE THE ATTRIBUTE m == 1)->dy would be great.

Is this possible?

Thanks.
Post Reply