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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:41:11+00:00 2026-05-17T23:41:11+00:00

I would like to select the value of the first href attribute from documents

  • 0

I would like to select the value of the first “href” attribute from documents like this using XPath:

<div>
  <a href="#a">
    <span>foo</span>
  </a>

  <a href="#b">
    <span>bar</span>
  </a>

  <a href="#c">
    <span>baz</span>
  </a>
</div>

However, I am only interested in those a elements that govern spans with text content “bar” or “baz”. I was hoping that I could achieve that with the following Java code:

Document document = getDocument(); // returns non-null Document

XPath xpath = XPathFactory.newInstance().newXPath();
String href = xpath.evaluate("//a[fn:matches(span, '^ba.$')]/attribute::href", document);

but whenever I’m using one of the fn: functions in an XPathExpression, I get

javax.xml.transform.TransformerException: Unknown error in XPath.
    at com.sun.org.apache.xpath.internal.XPath.execute(XPath.java:363)
    at com.sun.org.apache.xpath.internal.XPath.execute(XPath.java:301)
    at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.eval(XPathImpl.java:210)
    at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.evaluate(XPathImpl.java:275)
    at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.evaluate(XPathImpl.java:365)
    at MyCode(MyCode.java:71)
Caused by: java.lang.NullPointerException
    at com.sun.org.apache.xpath.internal.functions.FuncExtFunction.execute(FuncExtFunction.java:206)
    at com.sun.org.apache.xpath.internal.axes.PredicatedNodeTest.executePredicates(PredicatedNodeTest.java:340)
[...]

Similar Exceptions are thrown when I use fn:starts-with. I’m using JDK 1.6 on GNU/Linux.

Any ideas what I’m doing wrong? 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-17T23:41:12+00:00Added an answer on May 17, 2026 at 11:41 pm

    Only the core xpath functions 1.0 (http://www.w3.org/TR/xpath/#corelib) are supported by default (as stated here: http://download.oracle.com/javase/6/docs/api/javax/xml/xpath/XPathFunctionResolver.html).

    Therefore, instead of matches you should use contains (http://www.w3.org/TR/xpath/#function-contains).

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

Sidebar

Related Questions

I would like to run something like: select * from table where field in
I would like to select a node and modify its attributes and child-nodes using
I would like to select a row from 2 different tables that are relational.
I would like to select all elements that have certain attribute or don't have
i would like to select * from table where dates between (some_date and another_date)
I would like my comboboxes to have the first value be empty so that
The first thing i would like to say is that this is not exactly
I have a table with a DATETIME column. I would like to SELECT this
I have a scenario where I would like to select rows from a table
I would like to select a subset of elements from a whole that satisfy

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.