rosshadden / sublime-xpath

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

processing-instruction() is missing #37

Closed anthonyBertrant closed 6 years ago

anthonyBertrant commented 6 years ago

Hi guy ! First: your plug-in is AMAZING and you've done an AMAZING work ! I use your plug-in for my studies in France and I love it ! But I think I found an issue with the XPath: Query Document function. When I want to launch processing-instruction(), the plug-in doesn't find it, or , it's a function of XPath and it can't find this instruction. Can you help me ?

Thanks in advance and sorry for my VERY bad english

;-)

keith-hall commented 6 years ago

Hi (Bonjour), thanks for the feedback :)

I can confirm that this seems to be a bug - with a simple XML document like:

<test>
    <?helloworld ?>
</test>

using XPath: Query Document to query for //node() or //processing-instruction() doesn't include the processing instruction helloworld in the results list.

I don't have time to investigate properly right now, but a quick fix to get it to appear would be to add the following method to lxml_parser.py's LocationAwareTreeBuilder class:

    def pi(self, target, data, location=None):
        self._appendNode(etree.PI(target, data))

(open it using https://packagecontrol.io/packages/PackageResourceViewer and save the change, then restart ST) Then the output will be like this:

image

I'm not yet sure how involved it will be to fix this properly - i.e. preserving location information - but I'll see what I can do when I get some free time.

EDIT: Adding the missing function completion for processing-instruction() should be easier - see https://github.com/rosshadden/sublime-xpath/blob/8cd248ae7fe74824a085d1c3c554f285865e991d/xpath.py#L961. It looks like I originally didn't implement this because I somehow thought that processing instructions can only appear before the root element, in which case they wouldn't be represented in the lxml library's document model for XPath to access, but I've since worked out how to overcome that.