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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T10:46:02+00:00 2026-06-09T10:46:02+00:00

This is valid XPath in Javascript: id(priceInfo)/div[@class=standardProdPricingGroup]/span[1] And this turned into valid PHP XPath

  • 0

This is valid XPath in Javascript:

id("priceInfo")/div[@class="standardProdPricingGroup"]/span[1]

And this turned into valid PHP XPath to be used with DOMXPath->query() is

//*[@id="priceInfo"]//div[@class="standardProdPricingGroup"]//span[1]
  1. do you know any libraries or custom components that already do this transformation?
  2. do you know available documentation that lists the two syntax differences?

My main concern is that there could be a lot of differences, and I am looking to identify these differences, and I have problems to identify these.

The question could be put also in different way: Since Javascript can have different valid XPath formats, how to normalize them to work with the PHP.

One of the updates also mention that the id() function is valid XPath if there is a valid DTD that contains this definition. I don’t have power over the input DTD, and if there is a way to find a solution that works without any specific DTD it would be awesome.

Update:

I want to transform the first format into the second with an algorithm. My input is the first one and not the second one. Can’t change this.

As @Nison Maël pointed out, the 2nd format is valid Javascript XPath as presented here: http://jsbin.com/elatum/2/edit this unfortunately just adds to the problem of Javascript XPath “fragmentation”.

@salathe pointed out that the valid Javascript XPath query works fine in PHP if the input documented has valid DTD ( @Dimitre Novatchev mentioned this in a comment, but overlooked the importance). Unfortunately I don’t have control of the input DTD, so now I have to investigate a way to overcome this, or to find a solution that works even without valid DTD.

  • 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-09T10:46:04+00:00Added an answer on June 9, 2026 at 10:46 am

    Just seeing that Salathe actually answered the same, but taking your comment into account and to stress this a bit more:

    You do not need to specify any DTD. As long as you use the DOMDocument::loadHTML or DOMDocument::loadHTMLFile functions, the HTML id attribute is actually registered for the the xpath id() function. With the demo HTML given in http://jsbin.com/elatum/2/edit, you even get an error when you load the document:

    Warning: DOMDocument::loadHTMLFile(): ID priceInfo already defined in …

    Which is already a sign that this is a true ID attribute because it moans about duplicates. A related sample code looks like:

    $xpath = 'id("priceInfo")/div[@class="standardProdPricingGroup"]/span[1]';
    
    $doc = new DOMDocument();
    $doc->loadHTMLFile(__DIR__ . '/../data/file-11796340.html');
    $xp = new DOMXPath($doc);
    
    $r = $xp->query($xpath);
    echo $xpath, "\n";
    echo $r ? $r->length : 0, ' elements found', "\n";
    if (!$r) return;
    foreach($r as $node) {
        echo " - ", $node->nodeValue, "\n";
    }
    

    The output is:

    id("priceInfo")/div[@class="standardProdPricingGroup"]/span[1]
    1 elements found
     - hello
    

    In case you need more control, first run an xpath to mark all HTML id attributes as ID for xpath:

    $r = $xp->query("//*[@id]");
    if ($r) foreach($r as $node) {
        $node->setIdAttribute('id', true);
    }
    

    You can then use the same xpath with the id() function, no need to change it.

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

Sidebar

Related Questions

is this a valid JQuery usage pattern to : <script type=text/javascript> $(body).prepend('<input type=hidden id=error
Is this a valid url parameter in jquery.ajax(), <script type=text/javascript> $(document).ready(function() { getRecordspage(); });
Is this xpath a valid XPath expression? (It does what it should ). #!/usr/bin/env
What am I doing wrong with this XPath query? Within the feed tag, there
i have this xpath defined for moxy in a jaxb class @XmlPath(child::*/REG) public List
Is this valid and correct? RewriteRule ^myOldPage.html$ /index.php#info [R] I'm specifically interested about the
Is this valid ? //p//div/a Expected to match a regardless any element between p
Is this valid/portable/legal code: class Vector3 { public: float& operator [] ( const size_t
Is this valid C# code ? public class Product { [CompilerGenerated] private string <Name>k__BackingField;
I've got this xpath query: /html/body//tbody/tr[*]/td[*]/a[@title]/@href It extracts all the links with the title

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.