I am looking to search for "d_value" tags and find there distinct parent tags. This tag shows up under different parent tag as shown above.
From example above - the output will be "<division>" and "<opened_by>" and <sys_domain>
as these three are parent tag containing child <d_value> tag.
@decostaronny1 , Welcome. The 'xml' posted does not validate , please supply
valid xml (use https://codebeautify.org/xmlvalidator ) to validate, then edit the post and replace with the well formed xml. thks
Furthermore , the forum is primarily a cooperation - post your challenge, along with your attempt(s) - including failed/incomplete , the team can review, give suggestions (which may include complete solutions, suggested alternatives etc .... ) . You state you are using bash - but haven't supplied that code - please do. There are xml specific tools that are specifically written to handle XML, bash certainly isn't, have you researched any of those tools ?
I have updated the xml snippet in my query above.
I tried the code below. It gives me the whole block with parent tag. If somehow it can be formatted, I can try to grab the parent tag.
ruby gives a "less interesting"(in my view) approach than bash, but here it is, since you asked. You might like it:
require 'nokogiri'
# Parse the XML file
doc = Nokogiri::XML(File.read('input.xml'))
# Find and print distinct parent tags of <d_value>
doc.xpath('//d_value/..').map(&:name).uniq.each do |parent|
puts "<#{parent}>"
end
The bash script looks nicer, in my view only because I like simple and easy to read and modify without a lot of "thinking"; and the bash code does not require an external lib.