Kraigolas
Kraigolas

Reputation: 5590

How can I configure BaseX for use in VS Code

What's the problem

I am trying to get XQuery to work in VS Code using BaseX, but every time I try to execute a query using XML Tools, it fails because its execution in the command is returning an error code.

How do I know that is the problem?

On the repository for the extension it says:

The output of the execution engine is captured via the stdout for your operating system. If the process exits with any code other than 0, any content written to stdout is shown as an error message. If the content from stdout contains a line number, the error prompt will contain a button to take you to the line in question.

The result that I receive when I run my query is the exact query result written as an error, which is exactly what you'd expect given the quote above.

How am I configuring the settings.json

On the aforementioned repository, the settings.json is written as follows:

{
    "xmlTools.xqueryExecutionEngine": "C:\\Program Files (x86)\\Altova\\AltovaXML2008\\AltovaXML.exe",
    "xmlTools.xqueryExecutionArguments": [
        "-xquery", "$(script)",
        "-in", "$(input)"
        "-out", "$(input).output.xml"
    ]
}

But, I am using BaseX, so I tried to create the equivalent of the above using it:

"xmlTools.xqueryExecutionEngine": "basex.bat",
"xmlTools.xqueryExecutionArguments": [
    "$(script)",
    "-i", "$(input)",
    "-o", "output.xml"
], 

From the basex docs, this looks like the right execution idea.

Note:

  1. If output.xml doesn't already exist, Windows sends an error code 9009. To get around that, I just use a predefined file but if there's a way to avoid this problem and just create the file without an error, I'd love to know how.
  2. Writing the following

Alternate settings.json

"xmlTools.xqueryExecutionArguments": [
    "-o", "output.xml",
    "$(script)",
], 

does not work, although that's the way I have to write it in the cmd (I have also tried this while including the input file, but as I don't use it in my particular case, I tried both ways).

What Works

In the cmd, writing

basex -o output.xml data.xq

Works, and if output.xml exists, then

echo %ErrorLevel%

returns 0.

I am a bit confused on how settings.json is translated to the cmd, so I'm not sure how to recreate the problem encountered in VS Code in my cmd. If there is a way to ignore exit codes (and prevent halting the execution of my command), that would resolve my problem. Even better would be to find the correct way to write to settings.json that allows me to run my XQueries from VS Code.

Test data

For testing's sake, here are the files I use

data.xsd

<?xml version="1.0" encoding="UTF-8"?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="personType">
    <xs:sequence>
        <xs:element name="fname" type="xs:string"/>
        <xs:element name="minit" type="xs:string"/>
        <xs:element name="lname" type="xs:string"/>
    </xs:sequence>
</xs:complexType> 
<xs:complexType name="peopleType">
    <xs:sequence>
        <xs:element name="person" type="personType" minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
</xs:complexType>
<xs:element name="peopleDB" type="peopleType"></xs:element>
</xs:schema>

data.xml

<?xml version="1.0" ?>
<peopleDB xsi:noNamespaceSchemaLocation="data.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <person>
        <fname>John</fname>
        <minit>A</minit>
        <lname>Roger</lname>
    </person>
    <person>
        <fname>Fred</fname>
        <minit>X</minit>
        <lname>Gerald</lname>
    </person>
</peopleDB>

data.xq

<personDB>
{
let $d:=doc("data.xml") for $p in 
$d/peopleDB/person
return $p
}
</personDB>

Upvotes: 4

Views: 2170

Answers (2)

Brainz80
Brainz80

Reputation: 31

This seems to partially work. I know my answer comes pretty late; but maybe someone might still find this useful.

"xmlTools.xqueryExecutionEngine": "C:\\Program Files (x86)\\BaseX\\bin\\basex.bat",
"xmlTools.xqueryExecutionArguments": [
    "-i", "$(input)",
    "-o", "$(input).output.xml",
    "$(script)"
]

NOTE! Your path to BaseX might differ from mine.

One thing I haven't figured out yet is how to have BaseX ignore XML-namespaces.

Upvotes: 2

pgfearo
pgfearo

Reputation: 2256

The XML Tools extension for VSCode is not being maintained currently. There are however a few extensions for general XML programming tasks that I would recommend (I maintain the latter two):

While none of these extensions yet have direct support for executing XQuery with BaseX, VS Code has built-in support for Tasks that can execute shell commands.

You could therefore setup a user task (workspace-level tasks are also supported):

  1. From the Command Palette (Ctrl-Shift-P), invoke the Open User Tasks command
  2. From the 'Select a task template' options, select Others
  3. A tasks.json file will be created, edit this file to be something like:
       {
         "version": "2.0.0",
         "tasks": [
           {
             "label": "Run BaseX XQuery",
             "type": "shell",
             "command": "\"path\\to\\basex.bat\" \"-xquery\" ${file} \"-in\" \"${workspaceFolder}\\input.xml\" \"-out\" \"${workspaceFolder}\\output.xml\"
           }
         ]
       }
  1. Save the file (Ctrl-S)

Note: Care should be taken when escaping charaters like \ and * in JSON

With these 4 steps completed, you can now run this task from VS Code (provided you have a workspace folder open) using the Run Task command.

You probably don't want to hard code the paths in the script like I've done in this example, but you can either use Variable References to create something more flexible (perhaps even where you're prompted to select a file) or create a number of tasks with different label values and different paths.

Upvotes: 2

Related Questions