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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:32:41+00:00 2026-05-16T06:32:41+00:00

I am writing a fairly basic GreaseMonkey script that locates text in a specific

  • 0

I am writing a fairly basic GreaseMonkey script that locates text in a specific element and then uses that text to do things later. The relevant bits of code are as follows:

In the HTML there is a span with the class ‘someclass’, which contains a small string of text:

<span class="someclass">some text</span>

Then in the JavaScript i am trying to find this class and pull its contents (the ‘some text’) into a variable using the standard XPath jazz:

document.evaluate("//span[@class='someclass']/text()", document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);

Here’s the problem: When i run this on pages where ‘some text’ is a basic string with basic characters, everything works fine, but when i run it on pages where ‘some text’ contains entities, then it fails. For example, these are all fine and XPath returns the text i want:

<span class="someclass">some text</span>
<span class="someclass">some other text</span>
<span class="someclass">sometext</span>
<span class="someclass">some text 12345</span>

However, this gives me an error:

<span class="someclass">some text&#39;s text</span>

The error returned is:

Error: The expression is not a legal expression.
Source File: file:///blahblahblah.user.js
Line: (JS line i gave above)

I found a few results on here and on Google talking about how XPath has trouble with entities, but they were all doing things like [text() = 'blah &racquo; blah'] — in other words, their entities are in the XPath query itself. Mine aren’t, they’re in the text that i’m trying to return from the XPath query.

Is this the same problem? Is there any easy way around it?

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-05-16T06:32:41+00:00Added an answer on May 16, 2026 at 6:32 am

    The problem is that a string literal in an XPath expression must be surrounded by either quotes or apostrophes and should not contain the surrounding character.

    A literal string that contains both quotes and apostrophes needs to be transformed (in your case by your Javascript program) into one that doesn’t contain both these types of characters.

    The simplest way to do this is to replace each instance of one of these types of characters with its character entity — say replace every ' with &#39; and use the ' as surrounding character for the literal string.

    A second way is to replace

    some text&#39;s text
    

    with the XPath expression:

    concat('some text', "'", ' text')
    

    Warning: It is not a good idea to use untrusted data to create an XPath expression — this may result in XPath injection. To avoid XPath injections, if your programming language and function libraries allow this, always compile your XPath expression and run it with passing the data as parameter(s).

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

Sidebar

Related Questions

I'm writing a fairly simple register php script that uses PHP's built in mail()
As I'm writing JavaScript I'm always missing some fairly basic language features that JavaScript
I am fairly new to vbs and i am writing a script that checks
I'm fairly new to Java and am writing an app that needs an XML
I have a fairly complicated installer that I'm writing in Wix that has a
I need some assistance writing what should be a fairly basic .BAT file. I
I am writing a wordpress theme, and have run into a fairly basic problem.
I'm writing a fairly long running script, which prints progress messages as it goes.
I'm writing a fairly simply script and I'm trying to upload a file to
I am writing a fairly simple MVC3 application that allows a user to retrieve

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.