Hot answers tagged xml
8
I've had the most luck with this:
:%!xmllint --format %
It's strict about your tags, though, so it will error out if your opening and closing tags don't match. It also adds an XML declaration at the top of your file, if you don't have one as well.
This page recommends the following, although I can't get it to work:
:set filetype=xml
:filetype indent on
...
8
You can use groups, e.g.:
$ sed -i 's/<\(.*\)>\(.*\)<.*>/<column name="\1">\2<\/column>/g' filename.xml
Probably the most confusing part about REs is that there are various syntactic flavors.
For example sed and vim use basic regular expressions where you have to quote () to get their meta-meaning.
With extended regular ...
5
Install the matchit plugin (see :help matchit-install for instructions).
Make sure automatic file-type detection and plugin-loading is enabled (:filetype plugin on).
Henceforth, whenever you edit a file detected as xml or html or some other tag-based markup language, the combination of the matchit plugin and the filetype plugin files will allow the % ...
5
This can be done from find directly using -exec:
find . -name "*.xml" -type f -exec xmllint --output '{}' --format '{}' \;
Whats passed to -exec will be invoked once per file found with the template parameters {} being replaced with the current file name. The \; on the end of the find command just terminates the line.
The use of xargs isn't really ...
4
Using Python (2.7) with standard modules:
file test.xml:
<Container>
<Placemark>
<KeepMe/>
</Placemark>
<Placemark>
<styleUrl>#m_ylw-pushpin330</styleUrl>
<LineString>
<tessellate>1</tessellate>
<coordinates>
0.0000000000000,0.0000000000000,0 ...
3
Given this test file:
start
<Placemark>
<tessellate>1</tessellate>
</Placemark>
middle1
<Placemark>
</Placemark>
middle2
<Placemark>
<tessellate>1</tessellate>
</Placemark>
end
If you do perl -0 -pe 's|<Placemark>.*?<tessellate>.*?</Placemark>||gs' like you ...
3
With xmlstarlet:
xmlstarlet ed -d '//Placemark[.//tessellate]' < myplaces.kml
And as kml uses namespaces, you have to define it first (see the xmlstarlet documentation)
xmlstarlet ed -N 'ns=http://www.opengis.net/kml/2.2' -d '//ns:Placemark[.//ns:tessellate]'
With perl, you'd need to process the file as a whole (not line by line) and add the s flag ...
3
jsawk will probably do what you need: https://github.com/micha/jsawk
Edit: However I found jshon to work much better. Here is an example:
curl 'http://twitter.com/users/username.json' | jshon -e "location"
Outputs:
"new hampshire"
3
This discussion is enlightening.
At the very least, even if not ideal, you should be able to do:
xmllint --xpath "//*[local-name()='product_version']/*[local-name()='name']/text()" file.xml
Or use xmlstarlet instead:
xmlstarlet sel -t -v //swid:product_version/swid:name file.xml
3
As Mat said, indentation (and whitespace in general) is not important in XML files. This:
<one><tags></tags></one>
Is exactly equivalent to:
<one>
<tags>
</tags>
</one>
But this will work while preserving indentation:
$ cat myfile.xml
<tags>
</tags>
$ sed '/<one>/ a\
<tags>\
...
2
Here's an awk script that attempts to produce decent indentation. It assumes the input is well-formed (e.g. the line before 3.2.2 must be 3.1 or 3.1.something, not 3).
#! /usr/bin/awk -f
BEGIN { printf "(bookmarks"; depth = 1; }
{
level = split($1, s, ".");
while (level < depth) {--depth; printf ")";}
print "";
depth = level + 1;
...
2
Sounds like your log files are actually an XML format. If that's the case, then what you REALLY should be doing is using an XML parser. Read this famous StackOverflow answer if you want some more info.
Based on your description, a SAX-based parser is probably your best bet: these are stream-oriented parsers and don't require you to load the whole file in ...
2
It's not free, but oXygen has this feature, and runs on all three major platforms. (It's Java-based.) They have a screencast demo of the feature.
You can get oXygen in both a standalone version and one that runs in Eclipse, which is nice since you may already be using Eclipse for developing the parts of the system that consume or produce the XML.
2
As far as I know the | separator can be used only on entire paths:
echo 'cat /root/child/@attr1|/root/child/@attr2|/root/child/@attr3' | xmllint --shell data.xml
(As // means at any depth, “//root” puts the parser to pointless extra work. Assuming your sample XML looks has similar structure as the real one (so root is indeed the XML's root node), better ...
2
I typically attack these problems with a layer of indirection.
Write a shell script that does what you want, and call that.
I'd suggest as a start
#! /bin/sh
for file
do
xmllint --format $file > $file.tmp && mv $file.tmp $file
done
The try it out on a file or two by hand, then you can replace it in the xargs
find . -name "*.xml" -type f | ...
2
A simple solution for simple cases - see my comment:
echo "<g:gtin>31806831001</g:gtin>" | sed 's|<g:gtin>.*</g:gtin>|<g:gtin></g:gtin>|'
Result:
<g:gtin></g:gtin>
It depends on the assumption that start and endtag are on the same line, and not more than one tag is on that line.
Since xml files are ...
2
Why don't you just split it in the files you propose and then just cat them all together?
cat rc-something.xml rc.keyboard.xml rc.mouse.xml > rc.xml
The only problem is that you will need to cat them each time you modify one of the individual files, but that should be trivial..
2
I did not test these validators, but from the top of my mind / little search:
XMLStarlet - can be used for other things as well
msv - Sun multi schema validator
HaXML - haskell xml tools contains command line utilities (one is a validator)
xsltproc should also verify documents at startup
There are plenty more option as most utilities will automatically ...
2
You can use sed for the same
sed -i.bkp 5'i'"s/search/replace/" inputfile
-i.bkp take backup as inpufile.bkp and edit original file
5'i' go to line 5
s/ search and replace
also you can search specific word say 'NAME' and in same line search 'RAM' and replace with 'SHYAM' , see example below
sed -e '/NAME/s/RAM/SHYAM/g' filename
reference link ...
2
You might like to try dtdgen, a program I wrote many years ago to generate a DTD for a document. It not only tells you whether a large file is well-formed, it also tells you what's in it (I wrote it because I wanted to know both).
1
Here's one way to do it. I just put your output into a file called sample.txt to make it easier to test, you can just append my commands to the end of your echo command:
sample.txt
Folder="FOLDER1M\1"
File="R1.txt"
Folder="FOLDER1M\2"
File="R2.txt"
Folder="FOLDER2M\3"
File="R3.txt"
command
% cat sample.txt | sed 'h;s/.*//;G;N;s/\n//g' | sed ...
1
As mentioned by @donothingsuccesfully modifying xml with simple text replacement when you have requirements like tag xyz inside tag abc is generally a bad idea unless you have a very strict, known format of the input xml file, which is rarely the case. You need something that understands xml.
xml-sed from xml-coreutils allows you to run sed commands on an ...
1
You can achieve what you want with the help of a small Python script (you'll need Python installed, as well as the lxml toolkit).
tagsort.py:
#!/usr/bin/python
import sys
from lxml import etree
filename, tag = sys.argv[1:]
doc = etree.parse(filename, etree.XMLParser(remove_blank_text=True))
root = doc.getroot()
root[:] = sorted(root, key=lambda el: ...
1
put this script in a file (ex: 'increase.awk') :
BEGIN { i = 1 }
/.*<process>value=""<\/process>.*/ { split($0, a, "value=\"\"") ; print a[1] "value=\"" i++ "\"" a[2] ; next }
/.*/ { print $0 }
and then call:
gawk -f increase.awk < yourinputfile
explanation: in awk, split("string", a, "separatorstring") splits the "string" into an array ...
1
I don't know how you're copied the content (copy-paste from other editor to vim is a very bad idea), so I'll indicate two forms for to copy the content without remove/add characters.
Open vim.
:e build.xml
Esc
v
Select the text.
y
:e destination.txt
p
:wq
Open vim.
:e destination.txt
:r build.xml
:wq
You can also do cat build.xml >> ...
1
In addition to using Gedit (per Mallard mailing list answer posted in the update to my question), another answer was posted (which does not require me to adopt another tool):
On Thu, 2012-01-05 at 21:44 -0500, The Geeko wrote:
Is there a tool available which properly handles Mallard documents?
I normally use Geany, Emacs and Vim... ...
1
Because of the consistant format of the data, head and tail are your friends. This should even work for the last shorter file.
cat file | tail -n +3 | head -n -1 > trimmed_file
The tail -n +3 takes everything from the 3rd line down to the end of the file, and head -n -1 takes everything except the last line of the file.
Once you have a set of trimmed ...
1
Any programming language will be able to parse your input example correctly.
Choose a programming language and then parse the input deliminating the input first by "." and " " at first and " " second.
I would use Perl, but whatever language the developer is most familiar with would work fine.
Keep in mind that automatic solutions will only work if the ...
1
If there are multiple lines between the /start/ and /end/ patterns then you can use ranges and d.
In general
/start/,/end/d
will delete all lines between /start/ and /end/ inclusive.
Create a file and add the following lines then save it as somefile
/<\"my:PDF\">/,/<\"\/my:PDF\">/d
...
Only top voted, non community-wiki answers of a minimum length are eligible
