• Login
Community
  • Login

Colored keywords in XML

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
xmlkeywordscolor
18 Posts 3 Posters 11.2k Views
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K
    Kriz Smee
    last edited by Kriz Smee Aug 3, 2018, 9:55 AM Aug 3, 2018, 9:54 AM

    Hi @all,

    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?

    Best regards

    Kriz

    C 1 Reply Last reply Aug 3, 2018, 11:33 AM Reply Quote 1
    • C
      Claudia Frank @Kriz Smee
      last edited by Aug 3, 2018, 11:33 AM

      @Kriz-Smee

      as XML is a pretty straightforward it should be possible to create an UDL for it.
      Modifying an existing lexer isn’t always possible/easy without changing the code.

      Cheers
      Claudia

      1 Reply Last reply Reply Quote 1
      • K
        Kriz Smee
        last edited by Aug 3, 2018, 2:42 PM

        Hi Claudia,

        thanks for your answer. I did this but wasn’t able to define the all xml-based features like marking up the end-tag when clicking on the start-tag and so on.

        Best regards

        Kriz

        1 Reply Last reply Reply Quote 1
        • C
          Claudia Frank
          last edited by Aug 3, 2018, 4:57 PM

          Hi Kriz,

          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.

          Cheers
          Claudia

          1 Reply Last reply Reply Quote 1
          • K
            Kriz Smee
            last edited by Kriz Smee Aug 8, 2018, 2:40 PM Aug 8, 2018, 2:38 PM

            Hi Claudia,

            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 ;-)

            Cheers

            Kriz

            1 Reply Last reply Reply Quote 0
            • K
              Kriz Smee
              last edited by Aug 8, 2018, 2:50 PM

              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.

              S 1 Reply Last reply Aug 8, 2018, 3:09 PM Reply Quote 0
              • S
                Scott Sumner @Kriz Smee
                last edited by Aug 8, 2018, 3:09 PM

                @Claudia-Frank said:

                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.

                1 Reply Last reply Reply Quote 0
                • S
                  Scott Sumner
                  last edited by Aug 8, 2018, 3:31 PM

                  @Kriz-Smee

                  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):

                  Imgur

                  1 Reply Last reply Reply Quote 0
                  • K
                    Kriz Smee
                    last edited by Kriz Smee Aug 8, 2018, 3:58 PM Aug 8, 2018, 3:56 PM

                    @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).

                    Cheers

                    Kriz

                    S 1 Reply Last reply Aug 8, 2018, 8:15 PM Reply Quote 2
                    • S
                      Scott Sumner @Kriz Smee
                      last edited by Scott Sumner Aug 8, 2018, 8:17 PM Aug 8, 2018, 8:15 PM

                      @Kriz-Smee

                      So what you could do is to create/maintain a string that looks like this from the words you mentioned:

                      (?-i)\b((WordsStyle)|(name)|(styleID))\b

                      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.

                      1 Reply Last reply Reply Quote 1
                      • C
                        Claudia Frank
                        last edited by Claudia Frank Aug 8, 2018, 10:05 PM Aug 8, 2018, 10:03 PM

                        Hi Scott, thanks for jumping in.

                        Hi Kriz,

                        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 Child or a background color
                        as in chyld.

                        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.

                        Cheers
                        Claudia

                        S 1 Reply Last reply Aug 9, 2018, 12:35 AM Reply Quote 3
                        • S
                          Scott Sumner @Claudia Frank
                          last edited by Aug 9, 2018, 12:35 AM

                          @Claudia-Frank

                          Whew. Sounds exhausting! I think I’d go with the Mark solution recorded as a macro…and call it a day!

                          :-D

                          C 1 Reply Last reply Aug 9, 2018, 12:50 AM Reply Quote 1
                          • C
                            Claudia Frank @Scott Sumner
                            last edited by Aug 9, 2018, 12:50 AM

                            @Scott-Sumner

                            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.

                            Cheers
                            Claudia

                            1 Reply Last reply Reply Quote 1
                            • K
                              Kriz Smee
                              last edited by Aug 9, 2018, 10:27 AM

                              Hi,
                              thanks for all these proposals and your time! I will check these this evening and give you an answer :-)

                              Cheers

                              Kriz

                              1 Reply Last reply Reply Quote 0
                              • K
                                Kriz Smee
                                last edited by Aug 13, 2018, 3:48 PM

                                Hi again,

                                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 :-)

                                Cheers

                                Kriz

                                C 1 Reply Last reply Aug 13, 2018, 5:57 PM Reply Quote 1
                                • C
                                  Claudia Frank @Kriz Smee
                                  last edited by Aug 13, 2018, 5:57 PM

                                  @Kriz-Smee

                                  if it is all about validating your xml why not creating an xml schema and using
                                  XML Tools plugin to do the verification?

                                  Cheers
                                  Claudia

                                  1 Reply Last reply Reply Quote 2
                                  • K
                                    Kriz Smee
                                    last edited by Aug 14, 2018, 7:49 AM

                                    @Claudia-Frank

                                    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’.

                                    Cheers

                                    Kriz

                                    C 1 Reply Last reply Aug 14, 2018, 1:14 PM Reply Quote 1
                                    • C
                                      Claudia Frank @Kriz Smee
                                      last edited by Aug 14, 2018, 1:14 PM

                                      @Kriz-Smee

                                      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.
                                      https://www.w3schools.com/xml/schema_intro.asp

                                      Cheers
                                      Claudia

                                      1 Reply Last reply Reply Quote 2
                                      10 out of 18
                                      • First post
                                        10/18
                                        Last post
                                      The Community of users of the Notepad++ text editor.
                                      Powered by NodeBB | Contributors