I am creating a custom XPath function in Java that modifies the text-nodes in a node-set. I need to pass in a node-set, have the code loop through every node and return a node-set. I have seen many examples of custom XPath functions that modify strings, but none that take in a node set and return a node set successfully. I also don't know how to map the returning node-set.
Take this source XML for example.
<Library>
<Bookshelf>
<Book>alice in wonderland</Book>
<Book>the giving tree</Book>
<Author>shel silverstein</Author>
</Bookshelf>
</Library>
Then I want this to be my target XML. I chose to capitalize the first letter of each word, but this is just an example. Don't worry about the text modification part, I got that.
<Library>
<Bookshelf>
<Book>Alice In Wonderland</Book>
<Book>The Giving Tree</Book>
<Author>Shel Silverstein</Author>
</Bookshelf>
</Library>
The biggest thing here is that I want to implement this as a custom XPath function using Java so that it can be drag-and-dropped in Designer mode. And I am using a File Adapter on each side of this transformation, so the structure of all the nodes is already given and I have to make my results fit in.
You need to create your own implementation of NodeList. Something like what I use:
package org.gramar.model;
import java.util.List;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class NodeArray implements NodeList {
private Node nodes[];
public NodeArray(Node[] nodes) {
this.nodes = nodes;
}
public NodeArray(List<Node> nodes) {
this.nodes = new Node[nodes.size()];
nodes.toArray(this.nodes);
}
@Override
public Node item(int index) {
return nodes[index];
}
@Override
public int getLength() {
return nodes.length;
}
}
Your XPathFunction code would have something like this:
ArrayList<Node> nodes = ... logic to gather the nodes you want to return
return new NodeArray(nodes);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.