microsoft / XmlNotepad

XML Notepad provides a simple intuitive User Interface for browsing and editing XML documents.
https://microsoft.github.io/XmlNotepad/
MIT License
968 stars 207 forks source link

Enhancement - copy all parents of a child node in either slash or dot format #344

Closed JeffBenedict closed 9 months ago

JeffBenedict commented 9 months ago

Disclaimer: Please move if this enhancement request is in the wrong place. Please excuse my terminology syntax if it's incorrect.

It would be nice to have another "copy" option (Copy Parents for example) that could copy all the parents of a child node as either a slash or dot format. For example: given the following simple XML :

<channel>
  <source>
    <properties>
      <enabled>
        true
      </enabled>
    </properties>
  </source>
</channel>

if "true" is selected, then the Edit/Copy Parents command or Right Click/Copy Parents command would put into the clipboard either channel/source/properties/enabled or channel.source.properties.enabled (or options for both)

This for me would be extremely useful when using Powershell to parse XML files and pull data out.

lovettchris commented 9 months ago

How about "Copy Path" which would produce an XPath expression (the same one you see in the Find Dialog when you select XPath). For the above the xpath is "channel/source/properties/enabled" but let's say there were 5 blocks and this was the last one the xpath can also pinpoint the location using expressions like this "channel/source/properties[5]/enabled"

JeffBenedict commented 9 months ago

Yes, I believe that would work, and then for me, I could replace the / with dots in my Powershell stuff.

lovettchris commented 9 months ago

or your powershell could use xpath:

[xml]$xml = Get-Content 'C:\path\to\your.xml'
$xml.selectNodes('channel/source/properties[5]/enabled') 
JeffBenedict commented 9 months ago

thanks for the info. i'm doing mine something like this: (of course it's a little bit more complex), and I'm probably making it more complex that it really needs to be. but it works .. for me.. :)

$configfile = 'c:\path\to\myfile.xml'
$config = [xml]::new()
$config.load($configfile)
$channel = $config.channel.source

then i can iterate thru the properties with something liek

foreach ($property in $channel.properties) { 
     do something, 
     like look for $property.enabled 
}
lovettchris commented 9 months ago

that's fine if it works for you but my understanding is the "." access to elements only works for singleton elements, whereas the xpath is more general and can handle finding things that are buried in collections.

JeffBenedict commented 9 months ago

i understand. my particular case with this powershell script is to do a selective parsing of a very complex xml and populate certain data in specific columns and rows on specific tabs in an excel spreadsheet...

lovettchris commented 9 months ago

Ok, I added new "Copy XPath" command to the Edit and Context menus which will be available in version 2.9.0.7 shortly.

JeffBenedict commented 9 months ago

Awesome. Cant wait for the update to be available.

lovettchris commented 9 months ago

Release 2.9.0.7 is now available.