Page 1 of 1

Can I define custom function in XSLT for XPath Builder?

Posted: Fri Feb 23, 2018 1:10 pm
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.

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

Posted: Fri Feb 23, 2018 5:26 pm
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

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

Posted: Fri Feb 23, 2018 9:34 pm
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.

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

Posted: Mon Feb 26, 2018 4:27 pm
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

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

Posted: Mon Feb 26, 2018 7:53 pm
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 :(

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

Posted: Tue Feb 27, 2018 10:07 am
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

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

Posted: Tue Feb 27, 2018 11:37 am
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.

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

Posted: Tue Feb 27, 2018 3:54 pm
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

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

Posted: Tue Feb 27, 2018 9:16 pm
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).

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

Posted: Wed Feb 28, 2018 9:05 am
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

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

Posted: Wed Feb 28, 2018 4:17 pm
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.

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

Posted: Tue Mar 27, 2018 4:03 pm
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

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

Posted: Wed Mar 28, 2018 2:34 pm
by Radu
Hi Lionel,

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

Regards,
Radu