Can I define custom function in XSLT for XPath Builder?

Having trouble installing Oxygen? Got a bug to report? Post it all here.
serioadamo97
Posts: 22
Joined: Tue Oct 17, 2017 5:05 pm

Can I define custom function in XSLT for XPath Builder?

Post by serioadamo97 »

I have some XPath expression in XPath Builder favorites .
These expression has a bit complicated and long, for example:

Code: Select all


let $contextNode := .,
$workspace := prov:getPluginWorkspace(),
$editorAccess := work:getCurrentEditorAccess($workspace, 0),
$pdu := editor-vars:expandEditorVariables('${pdu}', $editorAccess),
$global := doc(concat($pdu, '/TableB.xml'))
return $global//*[@name=$contextNode]
This xpath search starts in TableA.xml and find cross-reference in TableB.
It works fine, but the expression is too long. (I've simplified the expression for example only, the real expression is longer)

What I want to do is make $pdu as a common util function, which make my XPath expression shorter.
Is is possiable to define custom function in XSLT?
I created a util.xsl and add this file to Classpath of the framework, but it doesn't work.

Thanks.
tavy
Posts: 364
Joined: Thu Jul 01, 2004 12:29 pm

Re: Can I define custom function in XSLT for XPath Builder?

Post by tavy »

Hello,

We do not have support to use an XSLT library for the XPath execution.
If you want you can create a Java extension function and then used it from the XPath Builder .
For this you need to do something like this:
  • create a class like the "ProjectRelativeResolver" added below
  • then compile the class and it in a jar file
  • copy the jar file in the "[OxygenInstallDir]/lib" folder.
  • add a prefix-namespace mapping in the XPath options (ext - ro.sync.ecss.xpath.ProjectRelativeResolver), in the "XML / XSLT-FO-XQuery / XPath" options page
  • Then you can use the function from the XPath builder to obtain the URL like this "ext:getURL("TableB.xml")

Code: Select all


package ro.sync.ecss.xpath;

import ro.sync.basic.util.URLUtil;
import ro.sync.exml.workspace.api.PluginWorkspace;
import ro.sync.exml.workspace.api.PluginWorkspaceProvider;
import ro.sync.exml.workspace.api.editor.WSEditor;
import ro.sync.util.editorvars.EditorVariables;

/**
* Resolves a relative URL to the project URL
*/
public class ProjectRelativeResolver{
/**
*
* @param relativePath The relative path.
* @return Returns an absolute path
*/
public static String getURL(String relativePath) {
PluginWorkspace pluginWorkspace = PluginWorkspaceProvider.getPluginWorkspace();

String currentEditedFileURL = null;
WSEditor currentEditorAccess = pluginWorkspace.getCurrentEditorAccess(PluginWorkspace.MAIN_EDITING_AREA);
if (currentEditorAccess != null) {
currentEditedFileURL = currentEditorAccess.getEditorLocation().toString();
}

String projectURL = EditorVariables.expandEditorVariables("${pdu}/", currentEditedFileURL);

return URLUtil.makeAbsolute(projectURL, relativePath);
}
}
Best Regards,
Octavian
Octavian Nadolu
<oXygen/> XML Editor
http://www.oxygenxml.com
serioadamo97
Posts: 22
Joined: Tue Oct 17, 2017 5:05 pm

Re: Can I define custom function in XSLT for XPath Builder?

Post by serioadamo97 »

Hi tavy,

I create a Java extension function which return a node list like below:

Code: Select all


  public static NodeList getNode() throws SAXException, IOException, ParserConfigurationException {
DocumentBuilder db = ParserCreator.newDocumentBuilder();
Document doc = db.parse(new File("D:/Global.xml"));
return doc.getChildNodes();
}
And call ext:getNode() in XPath Builder, it works. (I can see all child nodes in XPath result view)
But when click a record in the Results View, it can not open D:/Global.xm and locate corresponding element.
How to return a value in Java extension function which can be located in XPath Results View?
Thanks.
Radu
Posts: 9041
Joined: Fri Jul 09, 2004 5:18 pm

Re: Can I define custom function in XSLT for XPath Builder?

Post by Radu »

Hi,

Octavian asked me to help you with this. Unfortunately once your custom Java method will be responsible with returning the nodes which are further used by the XPath expression, Oxygen can no longer localize the results. And we do not have a workaround for this.
Maybe instead of using our XPath view you could create separate XQuery files for each expression, at least in the XQuery files you could import common library XQuery files.

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
serioadamo97
Posts: 22
Joined: Tue Oct 17, 2017 5:05 pm

Re: Can I define custom function in XSLT for XPath Builder?

Post by serioadamo97 »

Radu wrote:Hi,

Maybe instead of using our XPath view you could create separate XQuery files for each expression, at least in the XQuery files you could import common library XQuery files.

Regards,
Radu
But the results of executing separate XQuery files are not in XPath Result View.
XPath's Results View is very useful, because we can click a record to locate and highlight its corresponding element, but XQuery Result View not has this feature.

So now, I can only copy very big XPath expression from XQuery files to XPath view every time :(
Radu
Posts: 9041
Joined: Fri Jul 09, 2004 5:18 pm

Re: Can I define custom function in XSLT for XPath Builder?

Post by Radu »

Hi,

If you create an XQuery document and for it create a transformation scenario, in the "Output" tab of the transformation scenario config dialog you have the option to "Present as a sequence".

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
serioadamo97
Posts: 22
Joined: Tue Oct 17, 2017 5:05 pm

Re: Can I define custom function in XSLT for XPath Builder?

Post by serioadamo97 »

Radu wrote:Hi,

If you create an XQuery document and for it create a transformation scenario, in the "Output" tab of the transformation scenario config dialog you have the option to "Present as a sequence".

Regards,
Radu
Hi,
I have checked "Present as a sequence" and the result sequence can display in the XQuery view. But I can't click it to navigate to the node.(I hope oxygen xml editor can open the xml file and locate to that element queried when I click the result record)
Thanks.
Radu
Posts: 9041
Joined: Fri Jul 09, 2004 5:18 pm

Re: Can I define custom function in XSLT for XPath Builder?

Post by Radu »

Hi,

Indeed right now the nodes displayed after running an XQuery point to intervals in the output XML.
From what I looked in our code we could possibly add a contextual menu action on each result node to also open the original XML document from which the node was loaded and navigate to the node line and column.
I'll add an internal issue to look into this improvement, maybe we'll have some time after Oxygen 20.0 is released.

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
serioadamo97
Posts: 22
Joined: Tue Oct 17, 2017 5:05 pm

Re: Can I define custom function in XSLT for XPath Builder?

Post by serioadamo97 »

Radu wrote:Hi,

Indeed right now the nodes displayed after running an XQuery point to intervals in the output XML.
From what I looked in our code we could possibly add a contextual menu action on each result node to also open the original XML document from which the node was loaded and navigate to the node line and column.
I'll add an internal issue to look into this improvement, maybe we'll have some time after Oxygen 20.0 is released.

Regards,
Radu
That will be great, if Oxygen XML 2.0 support this feature.
In addition, I hope the I can right click the element in xml file, the editor will popup a context menu.
Then we can select a XQuery file (maybe has a history list which include recent used).
Radu
Posts: 9041
Joined: Fri Jul 09, 2004 5:18 pm

Re: Can I define custom function in XSLT for XPath Builder?

Post by Radu »

Hi,

About this remark:
In addition, I hope the I can right click the element in xml file, the editor will popup a context menu.
Then we can select a XQuery file (maybe has a history list which include recent used).
So some kind of way to quickly execute an XQuery over the current XML document? Showing results which backmap to the XML document?

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
serioadamo97
Posts: 22
Joined: Tue Oct 17, 2017 5:05 pm

Re: Can I define custom function in XSLT for XPath Builder?

Post by serioadamo97 »

XPath expression support context node, it is very convenient.
Because we can use mouse cursor position infomation in original xml, it is good if XQuery also support it.
Otherwise, we have to pass the parameter to XQuery manually.
LBarth
Posts: 26
Joined: Mon Mar 04, 2013 1:42 pm

Re: Can I define custom function in XSLT for XPath Builder?

Post by LBarth »

Hi Radu,
So some kind of way to quickly execute an XQuery over the current XML document? Showing results which backmap to the XML document?
Yes I would like that a lot :wink:.

Best regards,
Lionel
Radu
Posts: 9041
Joined: Fri Jul 09, 2004 5:18 pm

Re: Can I define custom function in XSLT for XPath Builder?

Post by Radu »

Hi Lionel,

Thanks, I will add your feedback to the opened issue.

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
Post Reply