Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get xpath in epubjs ? #1360

Open
rakshithpanjady opened this issue Sep 19, 2023 · 10 comments
Open

How to get xpath in epubjs ? #1360

rakshithpanjady opened this issue Sep 19, 2023 · 10 comments

Comments

@rakshithpanjady
Copy link

rakshithpanjady commented Sep 19, 2023

No description provided.

@rakshithpanjady
Copy link
Author

Can anyone help me on this ?

1 similar comment
@rakshithpanjady
Copy link
Author

Can anyone help me on this ?

@rakshithpanjady rakshithpanjady changed the title xpath related issue in epubjs How to get xpath in epubjs ? Sep 20, 2023
@rakshithpanjady
Copy link
Author

I need a help on this , Please anyone guide me on this ?

@johnfactotum
Copy link
Contributor

Step 1. Use Document.evaluate() to get the nodes (you can get the Document object from contents.document, either in the content hook or by calling rendition.getContents()).

Step 2. Construct a Range and use cfiFromRange() (or use cfiFromElement() if your target node is an Element) to get a CFI.

Step 3. Use rendition.display() to display the CFI.

@rakshithpanjady
Copy link
Author

Is there any method to generate xpath ?

@johnfactotum
Copy link
Contributor

Short answer: Don't. Use CFI instead.

Long answer: Firstly, generating XPaths has nothing to do with Epub.js. Second, XPath isn't designed to be used as an identifier. If you want identifiers, you should use CFI. Finally, if you still want to use XPath for whatever reason, there are virtually an infinite number of ways to get XPaths of any given node, depending on what you want to do. The most common approach is:

  1. Get the name of the element node. This is what goes after /.
  2. Enumerate all previous sibling elements and count the number of elements with the same name (including the element itself). This is what you'd put between [ and ].
  3. Repeat from step 1 with the parent element until you reach the document element, and concatenate the results.

@rakshithpanjady
Copy link
Author

Can you tell me stepToXpath(steps) , what does step means here and how to generate it ?

@johnfactotum
Copy link
Contributor

I don't know anything about "stepToXpath(steps)". Where does this function even come from?

In general, a step in XPath just means an expression that moves you towards the desired result. In practice you can just think of it as everything that comes after a /.

@panjadyboy
Copy link

![IMG20230922112134.jpg](https://github.com/futurepress/epub.js/assets/115630669/60c07ecf-5859-45ab-b264-06dc452af9c3
In epubjs there is a method named as stepToXpath(steps) , it is taking steps as parameter

@johnfactotum
Copy link
Contributor

It's "stepsToXpath", not "stepToXpath". Steps here means the parsed CFI steps, an array of objects in the form { type, index, id }, where type is either "element" or "text" (see he parseStep method).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants