morbac / xmltools

XML Tools plugin for Notepad++
GNU General Public License v3.0
260 stars 57 forks source link

Evaluate XPath expression, XPath copying through `Current XML path` gives no result and dialog gives no hint #204

Open BdR76 opened 1 year ago

BdR76 commented 1 year ago

Description of the problem

I'm trying to extract specific information from a large XML file using Notepad++ and the XML Tools plug-in. I've copied the XPath through the XML Tools menu Plugins > XML Tools > Current XML path and I've pasted that in the "XPath expression" dialog. However, I just get "no result" and no further hints from the XML Tools dialog, and I've got no clue what to do next. I suspect it has to do with xml namespace and I've tried entering different namespaces but no matter what I enter I get the same "no results", so at this point I'm just blind guessing.

Example

See example XML file in the attachement (the real file is larger, about 2.5MB)

<Bundle xmlns="http://hl7.org/fhir">
  <id value="bundle-transaction" />
  <meta>
    <lastUpdated value="2023-09-15T12:55:05+02:00" />
  </meta>
  <type value="transaction" />
  <entry>
    <resource>
      <ValueSet xmlns="http://hl7.org/fhir">
        <id value="8695af4d-a19c-484b-87b0-c627f8e19948" />
        <name value="Anomaly location" />
        <status value="active" />
        <compose>
          <include>
            <concept>
              <code value="1" />
              <display value="Left leg" />
            </concept>
            <concept>
              <code value="2" />
              <display value="Right leg" />
            </concept>
          </include>
        </compose>
      </ValueSet>
    </resource>
  </entry>
  <entry>
    <resource>
      <ValueSet xmlns="http://hl7.org/fhir">
        <id value="a3513ab8-d6ae-44b6-bfb3-a4e142521230" />
        <name value="Respiratory rate method" />
        <status value="active" />
        <compose>
          <include>
            <concept>
              <code value="99" />
              <display value="Other (remarks)" />
            </concept>
            <concept>
              <code value="0" />
              <display value="Manual" />
            </concept>
            <concept>
              <code value="1" />
              <display value="Automatic" />
            </concept>
          </include>
        </compose>
      </ValueSet>
    </resource>
  </entry>
</Bundle>

I move the cursor to the first name value, so "Anomaly location", and then select the menu item Plugins > XML Tools > Current XML path. It copies the XPath which is "/Bundle/entry/resource/ValueSet/name/@value" and as far as I can see this is the correct XPath.

Then I go to the menu item Plugins > XML Tools > Evaluate XPath expression. In the textbox for XPath expression I paste the xpath /Bundle/entry/resource/ValueSet/name/@value, and leave the textbox "Namespace definition" empty. When I press the button "Evaluate" I just get no result. Btw I also tried entering http://hl7.org/fhir in the textbox "Namespace definition" but that gives the same result.

xmltools_xpath_evaluate

For this small example file I would expect the result to be something like this:

"Anomaly location"
"Respiratory rate method"

Additional notes

I suspect that it has to do with xml namespaces, because previously I've had trouble with that as well, using different tools on a different case. But, if that is indeed the case, is it possible for XML Tools to evaluate the XPath and ignore the exact namespace?

Also, if there is some obvious mistake in the XPath, like if it requires [] or * at the end or something like that, maybe the dialog could give a hint or suggestion to what the XPath should be?

Notepad++ debug info, should it be needed

Notepad++ v8.5.4 (64-bit) Build time : Jun 12 2023 - 01:19:45 Path : C:\Program Files\Notepad++\notepad++.exe Command Line : Admin mode : OFF Local Conf mode : OFF Cloud Config : OFF OS Name : Windows Server 2019 Standard (64-bit) OS Version : 1809 OS Build : 17763.4737 Current ANSI codepage : 1252 Plugins : ComparePlugin (2.0.2) CSVLint (0.4.6.5) CsvQuery (1.2.9) FWDataViz (2.6.2) JsonTools (5) mimeTools (2.9) NppConverter (4.5) NppExport (0.4) NPPJSONViewer (2.0.4) NppMarkdownPanel (0.7.3) PoorMansTSqlFormatterNppPlugin (1.6.13.31508) RandomValuesNppPlugin (0.2.1) XMLTools (3.1.1.13)

epilogic-ch commented 1 year ago

I agree xpath evaluation on namespaced xml is not straightforward and I'll try to make this easier... just don't know when 😩

Currently, namespaces declared in xpath evaluation dialog must be named and expression must use these names, for instance:

image

BdR76 commented 1 year ago

XPath: /x:Bundle/x:entry/x:resource/x:ValueSet/x:name/@value Namespace: xmlns:x='http://hl7.org/fhir'

Thanks for showing how the namespace and xpath should be formatted in this case, that does work. 👍 I think when the namespace is incorrectly formated, then the plug-in could implicitly interpret the namespace by adding xmlns:x= and xpath x:. Either that, or explicitly correct the namespace formatting by first prompting the user.

So for example in the onExit (of onValidate) of the namespace textbox, check if namespace is correctly formatted, and if not prompt user to auto-reformat it. So something like:

1) user enters XPath /Bundle/entry/resource/ValueSet/name/@value in first textbox 2) user changes namespace in second textbox to http://hl7.org/fhir 3) The onExit checks if the namespace textbox starts with xmlns 4) if it doesn't, then prompt user Not formatted properly, do you want to auto-format namespace? yes/no 5) If user answers yes then correctly reformat the namespace value and add the x: to all XPath tags

imho that would go a long way in making it more userfriendly to search with namespaces. Or maybe the Plugins > XML Tools > Current XML path menuitem could also copy the namespace to the clipboard somehow?