print
function that can be used as a debugging aid by logging nodesets etc. to the console.$contexts
variable.See default settings for details and default values.
show_hierarchy_only
- Whether to show only hierarchy in status bar, instead of exact xpath. i.e. /Root/Node
instead of /Root/Node[3]
show_all_attributes
- Whether to show all attributes in the path. If false, will use a provided whitelist.case_sensitive
- Whether to ignore case when determining tag index and whether an attribute matches one defined in the whitelist. Ignored for when querying an xpath.copy_unique_path_only
- Whether to copy only unique xpaths to the clipboard when there are multiple selections.attributes_to_include
- Specific attributes or namespaces to include in the XPath.show_attributes_in_hierarchy
- Whether or not to include attributes when in hierarchy mode. (If show_all_attributes
is false and the attributes_to_include
whitelist is empty, this will have no effect.)live_mode
- whether to show the results of the xpath query while it is being typed. If false, will only show the results after the user presses enter in the input box.default_namespace_prefix
- the prefix to use when the xml document contains a default namespace with no prefix. e.g. <test xmlns="http://uri/">
XPath 1.0 doesn't support blank prefixes, so, for convenience, this plugin can set one for you.show_namespace_prefixes_from_query
- in case of blank namespace prefixes (see default_namespace_prefix
) or multiple namespace URIs being referenced from the same prefix, the plugin will automatically make them unique, so that you can easily use them in a query. If this is turned on, the xpaths that are shown in the status bar and copied to the clipboard will be directly queryable by this plugin. If this is turned off, element names in the path will reflect those in the source document.only_show_xpath_if_saved
- whether or not to only show the current xpath in the status bar if the view is not dirty. This could be useful to save wasting CPU cycles (from constant parsing) when editing a document, for example.max_results_to_show
- the maximum number of results to show from the xpath query. Set to <= 0 for no limit. Useful to speed up display of results when there are lots.normalize_whitespace_in_preview
- whether or not to normalize whitespace for text results in the preview. Defaults to false
, because there are situations when it is important to see exact results.variables
- a dictionary of custom variables, which can be used when writing an XPath query expression.auto_completion_triggers
- characters that, when typed while entering an XPath expression, will automatically show autocompletions. If empty, autocompletion can still be triggered manually.intelligent_auto_complete
- whether or not to include intelligent autocompletion suggestions from the document.goto_element
- when an element is selected via an XPath query, which aspect of it the cursor should move to. Possible values are:
open
- Select the name of the element in the open tag.close
- Select the name of the element in the close tag.names
- Select the name of the element in both the open and the close tag.open_attributes
- Select all the attributes in the open tag.content
- Select the content of the element.entire
- Select the entire element i.e. it's open tag, contents and close tag.none
- Do not move the cursor.goto_attribute
- when an attribute is selected via an XPath query, which aspect of it the cursor should move to. Possible values are:
name
- Select the name of the attribute.value
- Select the value (inside the quotes) of the attribute.entire
- Select the name and the value of the attribute.element
- Select the element that the attribute belongs to, using the goto_element
rules.none
- Do not move the cursor.sgml_selector
- a scope selector to determine what to parse as XML and enable XPath functions for. Defaults to HTML and XML, excluding things like ASP and PHP.show_xml_parser_errors
- whether or not errors encountered while parsing the document should be shown in the status bar. Disable it if you have other plugins that also show XML parsing/validation errors.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.
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.
Preferences
menu, and select Package Control
.Package Control: Install Package
.xpath
. When you see it, select it.This can happen if the lxml
dependency didn't load properly. You'll see errors in the ST console.
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:
lxml-5.1.1.tar.gz
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
~/Downloads/lxml-5.1.1/build/lib.macosx-15.1-arm64-3.8/lxml
into ~/Library/Application Support/Sublime Text/Lib/python38/lxml
, overwriting anything already there.(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.)
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.
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.
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.
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: