Colored keywords in XML
I’ve tried to add some keywords for the language ‘XML’.
Background is that I have to develop special XML-files and I want to color different tags with different colors.
e.g. <chance> </chance> in red and <power> </power> in blue (just the tag name, not neccecarily the ‘<>’.
I’ve tried to add the keyword tag in langs.xml and stylers.xml so I could name some keywords and set the colors (Settings- > Styles…) but it had no effect.
Is there any chance to do this?
that’s a pity - npps internal xml tag match function checks if the current lexer is
html, xml … but doesn’t allow userdefined language.
May I ask you what exactly you need and if you are interested to solve this with
a python script solution? But this means, of course, you need to install python script plugin.
sorry for answering pretty late, but I was too busy with other things. I actually just need the combination of choosing xml as the language (with all the inclulded features) and the possibility to add keywords for highlightning (i.e. special tag names and attributes). The reason is that I’m using xml files to control processes (coding them in Notepad++). If a tag or attribute isn’t named as aspected, it won’t work. So it would be helpfull if all correct tags would be green and others red.
To install the python script plugin would be no Problem ;-)
I was too late for edit my post so here is another one…
I don’t know what you mean with “… if you are interested to solve this with
a python script solution?” but I by myself have no experience with python script.
what exactly you need
I think the implication is that @Claudia-Frank will provide you a framework/example that will do what you need.
But I don’t know if you’ve done your best to give her what she would need to do so (sure, she could guess a bit…but…yuck). Why not provide a screenshot that you’ve marked up that illustrates well (but of course not to professional artistry standards) what you are looking for? All types of examples shown, etc. That avoids the usual, “Well, what you gave me was nice…sort of…but what I really had in mind was…”, after someone has worked hard to come up with free code for you.
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):
@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).
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.
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.
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.