LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-28-2021, 09:08 AM   #16
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 2,321

Rep: Reputation: Disabled

Quote:
Originally Posted by crts View Post
No, an xpath only selects nodes, you cannot delete nodes via xpath.
Actually, depends on the tools used.
Code:
xmlstarlet ed -d '//*[@lang!="fin"]' input.xml
I don't recommend tools from LT-XML 2 for this, nevetheless lxreplace also works (more or less):
Code:
ln -s /usr/share/xmltv/xmltv.dtd
lxreplace -q '//*[@lang!="fin"]' -d input.xml|
xmllint --format -
xml_grep provided by Perl module XML::Twig supports only a subset of XPath and uses a slightly different syntax:
Code:
xml_grep -v '//*[@lang and @lang!="fin"]' input.xml|
xml_pp
And of course, you always can do it via XQuery. I'm pretty sure that Xidel, XQilla, Saxon or Zorba can handle this. E.g.
Code:
xidel --xml input.xml -se 'x:replace-nodes(//*[@lang!="fin"],())'|
xmllint --format -
Here is a quick and dirty solution using xml2 and sed:
Code:
xml2 <input.xml|
sed -n '/programme\/[^@]/!p;/@lang=fin$/,/=/p'|
2xml|
xmllint --format -
The same using xml-sed from xml-coreutils. Unfortunately, it's rather poorly documented, so perhaps this could be done more efficiently.
Code:
xml-sed '
  s/@lang=/@x=0&/x
  s/@x=0@lang=fin/@x=1@lang=fin/x
  s/.*@x=0@.*//z
  s/@x=1//x
  ' input.xml|xmllint --format -
Or using hxremove from HTML-XML-utils and filtering by CSS selectors
Code:
<input.xml hxremove programme ':not([lang=fin])'|
xmllint --format -
There is also yq, a Python wrapper around jq for YAML (not to be confused with another yq written in Go). It provides the command xq for XML processing. So you also can filter XML using jq expression:
Code:
xq -x 'del(.tv.programme[][][]?|select(."@lang"!="fin"))' input.xml

Last edited by shruggy; 04-28-2021 at 02:15 PM.
 
Old 04-29-2021, 04:04 AM   #17
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by shruggy View Post
Actually, depends on the tools used.
Code:
xmlstarlet ed -d '//*[@lang!="fin"]' input.xml
Actually, it is as I said. The xpath itself cannot delete nodes. The xpath used in xmlstarlet still only returns a selection of nodes. If one wants to delete this selection from the XML document then one needs to use another tool (e.g., an XSLT processor), just as I suggested. I am not familiar with xmlstarlet but if it can delete nodes based on an xpath selection then it is to be preferred over an XSLT processor.
 
Old 04-30-2021, 11:35 AM   #18
mimorek
Member
 
Registered: Feb 2013
Distribution: Debian (jessie)
Posts: 42

Rep: Reputation: Disabled
Code:
#!/bin/bash

while read line
do
  if [[ $line =~ 'lang=' ]]
  then
    if [[ $line =~ 'lang="fin"' ]]
    then
      echo $line
    fi
  else
    echo $line
  fi
done
 
  


Reply

Tags
xml


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Parse XML in bash script MikeyCarter Linux - Software 1 02-16-2007 01:19 PM
Bash scripting. Strip chars from file names, etc. mooreted Programming 7 02-11-2007 08:52 PM
Bash script to strip a certain directory out of directories in a directory? rylan76 Linux - General 3 08-29-2006 11:35 AM
Add file content to a variable (bash)? LinuxSeeker Programming 4 12-19-2005 01:41 PM
Need help to strip XML & XSL tags from multiple files dfrechet Programming 9 10-12-2005 06:52 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:32 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration