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 7181351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:32:21+00:00 2026-05-28T17:32:21+00:00

I want to get values and attributes from a selector and then get attributes

  • 0

I want to get values and attributes from a selector
and then get attributes and values of its children based on a query.

allow me to give an example.

this is the structure

<div class='message'>
   <div>
   <a href='http://www.whatever.com'>Text</a>
   </div>

   <div>
    <img src='image_link.jpg' />
   </div>

</div>

<div class='message'>
   <div>
   <a href='http://www.whatever2.com'>Text2</a>
   </div>

   <div>
    <img src='image_link2.jpg' />
   </div>

</div>

So I would like to make a query to match all of those once.

Something like this:

 //$dom is the DomDocument() set up after loaded HTML with $dom->loadHTML($html);
$dom_xpath = new DOMXpath($dom);
$elements = $dom_xpath->query('//div[@class="message"], //div[@class="message"] //a, //div[@class="message"] //img');

foreach($elements as $ele){
   echo $ele[0]->getAttribute('class'); //it should return 'message'
   echo $ele[1]->getAttribute('href'); //it should return 'http://www.whatever.com' in the 1st loop, and 'http://www.whatever2.com' in the second loop
   echo $ele[2]->getAttribute('src'); //it should return image_link.jpg in the 1st loop and 'image_link2.jpg' in the second loop
}

Is there some way of doing that using multiple xpath selectors like I did in the example? to avoid making queries all the time and save some CPU.

  • 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-28T17:32:22+00:00Added an answer on May 28, 2026 at 5:32 pm

    Use:

    (//div[@class='message'])[$k]//@*
    

    This selects all three attributes that belong to the $k-th div (and any of its descendants) in the document whose class attribute has string value "message"

    You can evaluate N such XPath expressions — for $k from 1 to N, where N is the total count of //div[@class='message']

    XSLT – based verification:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
    
     <xsl:template match="/">
      <xsl:for-each select="//div[@class='message']">
        <xsl:variable name="vPos" select="position()"/>
    
        <xsl:apply-templates select=
        "(//div[@class='message'])[0+$vPos]//@*"/>
     ================
      </xsl:for-each>
     </xsl:template>
    
     <xsl:template match="@*">
      <xsl:value-of select=
      "concat('name = ', name(), ' value = ', ., '&#xA;')"/>
     </xsl:template>
    </xsl:stylesheet>
    

    when this transformation is applied on the provided XML document (wrapped in a single top element to become well-formed):

    <html>
        <div class='message'>
            <div>
                <a href='http://www.whatever.com'>Text</a>
            </div>
            <div>
                <img src='image_link.jpg' />
            </div>
        </div>
        <div class='message'>
            <div>
                <a href='http://www.whatever2.com'>Text2</a>
            </div>
            <div>
                <img src='image_link2.jpg' />
            </div>
        </div>
    </html>
    

    The XPath expression is evaluated twice and the selected attributes are formatted and output:

    name = class value = message
    name = href value = http://www.whatever.com
    name = src value = image_link.jpg
    
     ================
    name = class value = message
    name = href value = http://www.whatever2.com
    name = src value = image_link2.jpg
    
     ================
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to get a files these attributes as integer values.
Using VB.Net I want to get a all datagrid cell values, then insert into
I want to get all values of 'id' attribute of 'span' tag with html
I want to get all values of a set interface in one go as
I want to get the distinct values in a list, but not by the
I gave the above string,I want to get the serveripaddress,DB1,uid and **** these values
How do I simply get GET and POST values with JQuery? What I want
This loads a set of values from an XML file and places them into
I am trying to extract values from a string, I have tried to get
I want to get the value of my integer array, at an index. The

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.