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

  • SEARCH
  • Home
  • 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 7945921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:00:10+00:00 2026-06-04T01:00:10+00:00

Say I have an html file that I have loaded, I run this query:

  • 0

Say I have an html file that I have loaded, I run this query:

$url = 'http://www.fangraphs.com/players.aspx';
$html = file_get_contents($url);    
$myDom = new DOMDocument;
$myDom->formatOutput = true;
@$myDom->loadHTML($html);
$anchor = $xpath->query('//a[contains(@href,"letter")]');

That gives me a list of these anchors that look like the following:

<a href="players.aspx?letter=Aa">Aa</a>

But I need a way to only get “players.aspx?letter=Aa”.

I thought I could try:

$anchor = $xpath->query('//a[contains(@href,"letter")]/@href');

But that gives me a php error saying I couldn’t append node when I try the following:

$xpath = new DOMXPath($myDom);
$newDom = new DOMDocument;
$j = 0;
while( $myAnchor = $anchor->item($j++) ){
   $node = $newDom->importNode( $myAnchor, true );    // import node
   $newDom->appendChild($node);
}

Any idea how to obtain just the value of the href tags that the first query selects?? Thanks!

  • 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-06-04T01:00:11+00:00Added an answer on June 4, 2026 at 1:00 am

    Your XPath query is returning attributes themselves (i.e., DOMAttr objects) rather than elements (i.e., DOMElement objects). That’s fine, and that seems to be what you want, but appending them to the document is the problem. A DOMAttr is not a standalone node in the document tree; it’s associated with a DOMElement but is not a child in the usual sense. Thus, directly appending a DOMAttr to the document is invalid.

    From the W3C specs:

    Attr objects inherit the Node interface, but since they are not actually child nodes of the element they describe, the DOM does not consider them part of the document tree. . . . The DOM takes the view that attributes are properties of elements rather than having a separate identity from the elements they are associated with

    Either associate the DOMAttr with a DOMElement and append that element, or pull out the DOMAttr‘s value and use that as you wish.

    To just append its plain text value, use its value in a DOMText node and append that. For example, change this line:

        $newDom->appendChild($node);
    

    to this:

        $newDom->appendChild(new DOMText($node->value));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a pom file that looks similar to this <project xmlns=http://maven.apache.org/POM/4.0.0 xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xsi:schemaLocation=http://maven.apache.org/POM/4.0.0
Lets say I have a view file that is built like this: <html> ...
Lets say i have a html file that contain a form: <form method=post action=url>
I have an HTML file that contain iframe, say (called a.html): <body> <iframe width=430
Let us say I have a HTML page that contains a javascript file: The
Say i have a .html file that contains a web design and has variables
Say I have a HTML page that looks like this: That was created with
Say i have a file test.html that uses javascript cookies. (a) When i access
Let's say I'm including a file which contains html. The html have characters as
Say I have this HTML fragment <td width=25% style=text-align:right; > <span style=font-family: Arial; font-size:

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.