Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3616238
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T22:28:29+00:00 2026-05-18T22:28:29+00:00

Just getting started with XPath, and using it’s implementation with PHP’s SimpleXML objects. Right

  • 0

Just getting started with XPath, and using it’s implementation with PHP’s SimpleXML objects. Right now I’m using //zuq:* to create an array of SimpleXML objects with the zuq prefix in a given document. However, I’d like the SimpleXML objects to reference all descendants regardless of namespace. I tried using //child::zuq:*, but the SimpleXML trees it creates don’t seem to be complete.

Essentially, the objects captured should be all the top level objects of the zuq namespace throughout the document, containing all descendant elements regardless of namespace, including zuq.

tl;dr: How can I create a SimpleXML object tree from a given document where each SimpleXML root object is the highest level document element of a given namespace (such as zuq) containing all descendants of said element regardless of the descendant namespace? XPath is not a requisite but appears to be the best choice based on my reading.


test.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:zuq="http://localhost/zuq">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
    <h1>Heading</h1>
    <p>Paragraph</p>
    <zuq:region name="myRegion">
        <div class="myClass">
            <h1><zuq:data name="myDataHeading" /></h1>
            <p>
                <zuq:data name="myDataParagraph">
                    <zuq:format type="trim">
                        <zuq:param name="length" value="200" />
                        <zuq:param name="append">
                            <span class="paragraphTrimOverflow">...</span>
                        </zuq:param>
                    </zuq:format>
                </zuq:data>
            </p>
        </div>
    </zuq:region>
</body>
</html>

$sxml = simplexml_load_file('test.html');
$sxml_zuq = $sxml->xpath('//zuq:*/descendant-or-self::node()');
print_r($sxml_zuq);

Produces:

Array
(
[0] => SimpleXMLElement Object
    (
        [@attributes] => Array
            (
                [name] => myRegion
            )

        [div] => SimpleXMLElement Object
            (
                [@attributes] => Array
                    (
                        [class] => myClass
                    )

                [h1] => SimpleXMLElement Object //I don't know why these don't contain their zuq descendants
                    (
                    )

                [p] => SimpleXMLElement Object
                    (
                    )

            )

    )

[1] => SimpleXMLElement Object
    (
        [@attributes] => Array
            (
                [name] => myRegion
            )

        [div] => SimpleXMLElement Object
            (
                [@attributes] => Array
                    (
                        [class] => myClass
                    )

                [h1] => SimpleXMLElement Object
                    (
                    )

                [p] => SimpleXMLElement Object
                    (
                    )

            )

    )

[2] => SimpleXMLElement Object
    (
        [@attributes] => Array
            (
                [class] => myClass
            )

        [h1] => SimpleXMLElement Object
            (
            )

        [p] => SimpleXMLElement Object
            (
            )

    )

[3] => SimpleXMLElement Object
    (
        [@attributes] => Array
            (
                [class] => myClass
            )

        [h1] => SimpleXMLElement Object
            (
            )

        [p] => SimpleXMLElement Object
            (
            )

    )

[4] => SimpleXMLElement Object
    (
    )

[5] => SimpleXMLElement Object
    (
        [@attributes] => Array
            (
                [name] => myDataHeading
            )

    )

[6] => SimpleXMLElement Object
    (
        [@attributes] => Array
            (
                [class] => myClass
            )

        [h1] => SimpleXMLElement Object
            (
            )

        [p] => SimpleXMLElement Object
            (
            )

    )

[7] => SimpleXMLElement Object
    (
    )

[8] => SimpleXMLElement Object
    (
    )

[9] => SimpleXMLElement Object
    (
        [@attributes] => Array
            (
                [name] => myDataParagraph
            )

    )

[10] => SimpleXMLElement Object
    (
        [@attributes] => Array
            (
                [name] => myDataParagraph
            )

    )

[11] => SimpleXMLElement Object
    (
        [@attributes] => Array
            (
                [type] => trim
            )

    )

[12] => SimpleXMLElement Object
    (
        [@attributes] => Array
            (
                [type] => trim
            )

    )

[13] => SimpleXMLElement Object
    (
        [@attributes] => Array
            (
                [name] => length
                [value] => 200
            )

    )

[14] => SimpleXMLElement Object
    (
        [@attributes] => Array
            (
                [type] => trim
            )

    )

[15] => SimpleXMLElement Object
    (
        [@attributes] => Array
            (
                [name] => append
            )

        [span] => ...
    )

[16] => SimpleXMLElement Object
    (
        [@attributes] => Array
            (
                [name] => append
            )

        [span] => ...
    )

[17] => SimpleXMLElement Object
    (
        [@attributes] => Array
            (
                [class] => paragraphTrimOverflow
            )

        [0] => ...
    )

[18] => SimpleXMLElement Object
    (
        [@attributes] => Array
            (
                [class] => paragraphTrimOverflow
            )

        [0] => ...
    )

[19] => SimpleXMLElement Object
    (
        [@attributes] => Array
            (
                [name] => append
            )

        [span] => ...
    )

[20] => SimpleXMLElement Object
    (
        [@attributes] => Array
            (
                [type] => trim
            )

    )

[21] => SimpleXMLElement Object
    (
        [@attributes] => Array
            (
                [name] => myDataParagraph
            )

    )

[22] => SimpleXMLElement Object
    (
    )

[23] => SimpleXMLElement Object
    (
        [@attributes] => Array
            (
                [class] => myClass
            )

        [h1] => SimpleXMLElement Object
            (
            )

        [p] => SimpleXMLElement Object
            (
            )

    )

[24] => SimpleXMLElement Object
    (
        [@attributes] => Array
            (
                [name] => myRegion
            )

        [div] => SimpleXMLElement Object
            (
                [@attributes] => Array
                    (
                        [class] => myClass
                    )

                [h1] => SimpleXMLElement Object
                    (
                    )

                [p] => SimpleXMLElement Object
                    (
                    )

            )

    )

)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T22:28:30+00:00Added an answer on May 18, 2026 at 10:28 pm

    Don’t trust the output of the print_r statement … it seems to be showing an empty object, but in my testing the children are actually still there. For example, starting with your code above:

    $sxml = simplexml_load_file('test.html');
    $sxml_zuq = $sxml->xpath('//zuq:*/descendant-or-self::node()');
    

    If I subsequently try a command like this:

    print_r($sxml_zuq[0]->div->h1);
    

    I get this output:

    SimpleXMLElement Object
    (
    )
    

    It seems to be empty, right? But if I modify the command to look like this:

    echo $sxml_zuq[0]->div->h1->asXML();
    

    I get the resultant tree with the namespaced child:

    <h1><zuq:data name="myDataHeading"/></h1>
    

    I’m not 100% sure why this is; it probably has something to do with the print_r statement trying to flatten the simplexml object and not dealing with the namespaces properly. But when you keep to the simplexml objects themselves that are returned from your xpath call, all of the children are preserved.

    Now, in regards to your xpath itself, you probably DON’T want the “descendant-or-self” axis, because that will match not only the top-level zuq element, but also match all its children and create a larger array than you’re actually seeking to return (unless I’m misunderstanding what you’re asking). If you try something like this:

    $sxml_zuq = $sxml->xpath('//zuq:*[not(ancestor::zuq:*)]');
    

    then you’ll get back an array of ONLY the top level of zuq namespaced elements. (while your example XML only had one such top-level element, your actual data may have several siblings at that level). You can then capture the content of each of these top level elements like this:

    foreach ($sxml_zuq as $zuq_node) {
         echo ($zuq_node->asXML());
    }
    

    Things get a little trickier if you want to repeat this process but do the search for top-level (or any) elements in the default namespace; you’d have to use the registerNamespace function to give the default namespace a temporary prefix, and do the xpath search on that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just getting started using MVC in ASP.NET, I'm going to have it so users
I am just getting started with flex and am using the SDK (not Flex
I am just getting started creating an AJAX application using server side push. I
I'm just getting started with jQuery. I was using it to make a photo
I am just getting started again with Mac development. I am using CoreMIDI which
I am just getting started using PDO. I am wondering if there is a
Just getting started with Lucene.Net. I indexed 100,000 rows using standard analyzer, ran some
Im just getting started with PHP, and I need to export a table from
Just getting started with Linq to SQL so forgive the newbie question. I'm trying
I'm just getting started with learning about Unit testing (and TDD in general). My

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.