rosshadden / sublime-xpath

Sublime Text plugin for easier cursor navigation of XML and HTML files using XPath 1.0.
45 stars 9 forks source link

Sublime Text - XPath Plugin

Features:

Settings:

See default settings for details and default values.

No key bindings are set by default, but an example sublime-keymap file is included, to show the available commands and arguments. See this documentation for more details about keybindings in ST3.

Demonstrations

Installation

The recommended way to install the Sublime Text XPath plugin is via Package Control. Package Control will install the plugin on your system and keep it up to date.

  1. Ensure Package Control is installed.
  2. In Sublime Text, open the Preferences menu, and select Package Control.
  3. Select Package Control: Install Package.
  4. Start typing xpath. When you see it, select it.
  5. Wait for it to install.
  6. Restart Sublime Text to be sure everything is loaded properly.
  7. Enjoy!

Troubleshooting

Context menu items disabled

This can happen if the lxml dependency didn't load properly. You'll see errors in the ST console.

Mac

On a Mac with Apple silicon, the version of lxml installed by Package Control 4 doesn't seem to work. In ST console, we can see that ST build 4180 is using Python 3.8.12:

import sys; sys.version_info

So you can build lxml manually using this version of Python. You will need to download the source code release asset, as lxml's git repository doesn't contain some .c files which are required to build. lxml v5.1.1 for sure works:

brew install pyenv
pyenv init # follow instructions

pyenv install 3.8.12
pyenv shell 3.8.12

cd ~/Downloads/lxml-5.1.1
python setup.py build

(If you were to try downloading lxml-5.1.1-cp38-cp38-macosx_10_9_universal2.whl for example, and extracting that into ST's lib folder mentioned above, when you restart ST, you would be told that an .so file in Lib/python38/lxml/ folder isn't trusted, and there would be no option to "allow". You could go in Mac Settings -> Privacy and Security and it should show up there with an option to allow it. But you'd still see that the lxml dependency fails to load in ST, and the only solution seems to be building from source on the Mac.)

CDATA Nodes

When working with XML documents, you are probably used to the Document Object Model (DOM), where CDATA nodes are separate to text nodes. XPath sees text() nodes as all adjacent CDATA and text node siblings together. If you really need to work with separate text and CDATA nodes in XPath, you will need to ensure that an XML comment separates the nodes in the source document.

Example:

<hello><![CDATA[world]]>foobar</hello>
<hello><![CDATA[world]]><!-- separator, so that the CDATA and text nodes are non-adjacent -->foobar</hello>

The XPath /hello[1]/text() on the first example will return a single text node: worldfoobar. On the second example, it will return two text nodes: world and foobar. More information can be found at this Stack Overflow Q&A.

Namespaces

XPath 1.0 does not have the concept of a default namespace. Therefore, if a node in the XML document being queried defines a default namespace, that namespace should be mapped to a prefix in the XPath query expression for easier access. This plugin will do that for you automatically. See the included example_xml_ns.xml file for more details.

Nodes before the root element

Note that due to the way ElementTree (the Python XML module) works, comments, processing instructions or doctypes that come before the root node of the document won't be navigatable by this plugin.

Potential future improvements:

Feature requests, bug reports/fixes and usability suggestions are always welcome.

In no particular order, here are some ideas of how this plugin could be made even more awesome:

Contributors