0

In sample XML draft:

<unit name="Alpha">
    <unit name="One">
         █1
    </unit>
    <unit name="Two">
         █2
    </unit>
</unit>

I want to find any way how to display XPath containing

  • unit@name="Alpha"/unit@name="One" when the caret is at position █1

or

  • unit@name="Alpha"/unit@name="Two" when the caret is at position █2

so that I can edit large XML files without collapsing their outline and reading name attributes by myself.

Ideally, I would like to find a way to get such an XPath in some free text editor in Windows.

  • It's good task for CudaText plugin, if you know Python. Plugin "CudaExt" already has command which shows current Code Tree path in statusbar: "Show current path in statusbar". You can make new one. – RProgram Jul 27 '18 at 23:33
  • @RProgram – hm, it's not so much about Python than about learning necessary editor API's and libraries. At the moment I need something ready, – miroxlav Jul 28 '18 at 8:28
  • I will make this plugin, if you make base Python module which finds XPath string, from text lines and cursor position. – RProgram Jul 28 '18 at 12:41
  • @RProgram – thank you, if I'll get that far, then it will be relatively low effort to add the rest :) That thing you mention is already done in Notepad++ XML Tools plugin (in C++) and in VS Code XML Tools plugin (most likely in C#, but I am not sure right now if its code is public). I mean, they already display the XPath, but as elements without attribute values. I would prefer to modify that Notepad++ solution. So are you fine with C++, too? – miroxlav Jul 28 '18 at 13:20
  • Well, I need python only code, since CudaText is cross platform. – RProgram Jul 28 '18 at 19:41
0

After no answer I developed simple Python script to be used with Python Script plugin in Notepad++ editor. It can be attached to a keyboard shortcut or a toolbar icon* to show all parent XML tags of tag which is in cursor position.

import re

matches = []
def match_found(m):
 matches.append(m.group(0))

editor.research('<[A-Za-z0-9_]+[^/>]*>|</[A-Za-z0-9_]+>', match_found, 0, 0, editor.getCurrentPos())
path = []
for m in matches:
 if m[:2] == "</":
  path.pop()
 elif m[-2:] != "/>":
  path.append(m)

msg = ""
for m in path:
 msg += m + "\n"

notepad.messageBox(msg, "Parents of current tag", 0)

*) due to current internal problem of the plugin, the script works only when it is attached to second or higher toolbar icon added by this plugin. The first icon has problems, attach anything to it and do not click it. More effective way is simply to bind a keyboard shortcut via Settings > Shortcut Mapper > Plugin Commands.

  • Will try to adapt this script to make CudaText command in plugin CudaExt. – RProgram Aug 1 '18 at 10:10
  • @RProgram – OK :) just please note that this is quick and dirty solution sufficient for my needs. It does not handle XML comments, for example. A proper solution might require regular XML parsing using XML library and then maybe use created document object model to determine the path. – miroxlav Aug 1 '18 at 11:33
0

CudaText editor (free) has plugin CudaExt (install in Plugins/AddonManager), it gives command "Code Tree: Show current path in statusbar".

For XML file, command shows path in XML tree, like on screenshot (yellowish bar on bottom). Only note: cursor must be inside <tag ... >, not after it.

CudaText

  • Based on the screenshot, I'm afraid that path provided by the plugin does not fulfill criteria given in the question. Elements in the path are not shown with their attributes. Such a plugin (showing path without attributes) is available in many editors, but for me it is useless, because if many similar parent sections exist (in my example they would be "Alpha", "Beta", ....), you don't know in which one your are currently located, if they contain hundreds of records like "one", "two" which require scrolling up many screens to find parent XML tag. – miroxlav Aug 1 '18 at 19:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.