Colored keywords in XML
Scott Sumner last edited by
To illustrate your desired coloring, you could use the Mark feature of Notepad++ (to get a RED) and also this feature of Notepad++ (there’s a GREEN in there somewhere):
Kriz Smee last edited by Kriz Smee
@Scott Sumner: Thanks for the hint how to set a color for ‘all marks’, havn’t seen this before.
@Claudia: If I wasn’t precise enough, here is what I ment: For my example I choose the stylers.xml : Notepad++ would not do what I want, if I name a Tag ‘WordStile’ instead of ‘WordStyle’. So I would like to write a list with all my keywords (like ‘WordStyle’, ‘name’, ‘styleID’ and so on) that the ‘right color’ will show the coder that there is no typing error (like in many other developing environments). I think that would be helpfull for many programmers who have to design xml files with certain conventions - fill in keywords -> choose a color (like designing an UDL).
Scott Sumner last edited by Scott Sumner
So what you could do is to create/maintain a string that looks like this from the words you mentioned:
and then when you want to do a “validation” on your document (e.g. stylers.xml ), call up the Mark dialog (Search (menu) -> Mark…), and fill it out as follows:
Find what zone: the string above
Purge for each search: ticked
Wrap around: ticked
Search mode: Regular expression
Action: Press Find All button
If your word list is “stable”, record the above into a macro for easy repeated use. Sure, it doesn’t give you as-you-type highlighting, but as a quck-n-dirty it is a pretty good solution.
For an as-you-type solution, the Pythonscript presented in this thread could be modified slightly to give you that …with a fixed word list; making the word list have a dynamic nature would take a bit more effort.
Claudia Frank last edited by Claudia Frank
Hi Scott, thanks for jumping in.
currently I see three ways to solve your issue.
a) writing a lexer entirely with python script
b) writing an “xml-linter” with python script
c) using npp builtin features like Scott already showed.
a) Writing a lexer, the software part which is responsible for
coloring and folding, does only make sense if the file is not
really a standard xml file but some kind of derivate.
But this means also you cannot use the builtin xml lexer anymore
as scintilla, the underlying component which at the end does all of this,
doesn’t support two independent lexers working on the same document
b) Writing some kind of xml-linter which analyzes the document consistently
for potential errors. (my preferred solution). Consistently means that the script
registers some callbacks like on_file_save or on_document_modified and runs
when those events get fired.
c) As Scott already pointed out there are ideas which can be used without writing
an additional script. But this is more of a manual way to trigger “linting”.
Of course could be automated but than it needs to use, again, some plugins which act
on certain events.
What’s your favorite solution?
Beside those solutions let me ask some questions which needs to be clarified if
an xml-linter kind of solution should be realized.
Assuming an xml file like, for example,
<?xml version="1.0" encoding="UTF-8"?> <root> <children> <child nam="test" val="42"/> <child> <name>test</name> <value>42</value> </child> <Child name="test2" value="24"/> <chyld> <name>test</Name> <Value>42</value> </chyld> </children> </root>
could result in something like this
or that if a dark theme is used.
This already raises a question about the RED color, as you see red is the standard attribute color in the default styler.
In addition you see that we can either use a foreground color like in
Childor a background color
But this, again, raises a question, how does the script know that either Child or chyld is
wrong and not another valid tag? Do you think about defining all possible tag/attributes names as keywords?
Or do those share some common string to identify when an tag might be misspelled?
Or does it has some schema which can be used to check validity?
What kind of event should be used, whenever you save the file it checks or on a modify event, which means
that when typing it checks. (If the xml is big, several thousand tags, this might be a performance issue)
How do you like to have the script been execute? It can be configured to get executed when notepad++ starts
start it manually for the first time or manually whenever you want to check something.
Enough for the moment.
Scott Sumner last edited by
Whew. Sounds exhausting! I think I’d go with the Mark solution recorded as a macro…and call it a day!
well, it depends what should be realized.
For example, my EnhancedPythonLexer is only about ~70 LOC,
Ok, it does just 2 things, coloring self class method and function parameters
but is still nice.
thanks for all these proposals and your time! I will check these this evening and give you an answer :-)
I’ve checked the possibilities and came to conclusion that I will do a mix of using the standard xml-language while writing the xml-file and switch to an UDL with all my keywords in it for a visual check at the end. It’s not worth that someone spends his/her freetime in writing many lines of complicated code for a single users wish.
Thank you taking some time for my problem, I thought (hoped) that this will be solved with a notepad++ feature that I didn’t knew before :-)
if it is all about validating your xml why not creating an xml schema and using
XML Tools plugin to do the verification?
actually that’s what I meant: creating an xml with ‘xml language’ and use the plugin to make shure that all tags are closed and that there are no syntax errors. The second round at the end with my UDL is just to make Shure, that all tags are named correctly. If you open a tag and “tag auto-close” is activated, maybe you won’t see the tag got a wrong name and the plugin will find no errors.
Maybe I should have mention this at the beginning: There will be a few users which create different xml files to configure my Software for different situiations, I want to make it as easy as possible for them because they are not used to write ‘lines of code’.
I would suggest to deliver an xml schema together with your software and
a short description on how to use it together with the xml tools plugin.
Multiple free software is available to let the schema being created for you,
if you want to avoid learning how to create one from scratch.