Identify empty xml files?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












I have a requirement to identify and write all xml file names which are empty to a text file for reporting purpose. Empty , here means the xml file has the usual header tag <?xml version="1.0" encoding="UTF-8"?> followed by an empty open and close tag.



Sample file:
1)



<?xml version="1.0" encoding="UTF-8"?>
<STBTests>
</STBTests>


2)



<?xml version="1.0" encoding="UTF-8"?>
<UMTTests>
</UMTTests>


There are no data in the xml files apart from this. Any suggestions on how to approach this would be great.







share|improve this question






















  • Can you be specific about how you define an "empty" file? Is it (at most) one tag on the first line, followed by pairs of open and close tags with nothing but whitespace between them?
    – Sparhawk
    Mar 15 at 20:56










  • How about file size, it could be a good tell if non-empty files are bigger, use find /path -size -128c to find files with less than 128 bytes
    – Dalvenjia
    Mar 15 at 21:06











  • other option is to count the lines, for F in *.xml; do if [ $(wc -l "$F") -lt 4 ]; then echo "$F"; fi; done
    – Dalvenjia
    Mar 15 at 21:11







  • 1




    @Dalvenjia: very bad idea. Think of an inline node like <foo>xxxxxxxxxxxxxxxxxxxxxxxx</foo>
    – Gilles Quenot
    Mar 15 at 21:44











  • What about comments and processing instructions?
    – ErikF
    Mar 15 at 23:07














up vote
1
down vote

favorite












I have a requirement to identify and write all xml file names which are empty to a text file for reporting purpose. Empty , here means the xml file has the usual header tag <?xml version="1.0" encoding="UTF-8"?> followed by an empty open and close tag.



Sample file:
1)



<?xml version="1.0" encoding="UTF-8"?>
<STBTests>
</STBTests>


2)



<?xml version="1.0" encoding="UTF-8"?>
<UMTTests>
</UMTTests>


There are no data in the xml files apart from this. Any suggestions on how to approach this would be great.







share|improve this question






















  • Can you be specific about how you define an "empty" file? Is it (at most) one tag on the first line, followed by pairs of open and close tags with nothing but whitespace between them?
    – Sparhawk
    Mar 15 at 20:56










  • How about file size, it could be a good tell if non-empty files are bigger, use find /path -size -128c to find files with less than 128 bytes
    – Dalvenjia
    Mar 15 at 21:06











  • other option is to count the lines, for F in *.xml; do if [ $(wc -l "$F") -lt 4 ]; then echo "$F"; fi; done
    – Dalvenjia
    Mar 15 at 21:11







  • 1




    @Dalvenjia: very bad idea. Think of an inline node like <foo>xxxxxxxxxxxxxxxxxxxxxxxx</foo>
    – Gilles Quenot
    Mar 15 at 21:44











  • What about comments and processing instructions?
    – ErikF
    Mar 15 at 23:07












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I have a requirement to identify and write all xml file names which are empty to a text file for reporting purpose. Empty , here means the xml file has the usual header tag <?xml version="1.0" encoding="UTF-8"?> followed by an empty open and close tag.



Sample file:
1)



<?xml version="1.0" encoding="UTF-8"?>
<STBTests>
</STBTests>


2)



<?xml version="1.0" encoding="UTF-8"?>
<UMTTests>
</UMTTests>


There are no data in the xml files apart from this. Any suggestions on how to approach this would be great.







share|improve this question














I have a requirement to identify and write all xml file names which are empty to a text file for reporting purpose. Empty , here means the xml file has the usual header tag <?xml version="1.0" encoding="UTF-8"?> followed by an empty open and close tag.



Sample file:
1)



<?xml version="1.0" encoding="UTF-8"?>
<STBTests>
</STBTests>


2)



<?xml version="1.0" encoding="UTF-8"?>
<UMTTests>
</UMTTests>


There are no data in the xml files apart from this. Any suggestions on how to approach this would be great.









share|improve this question













share|improve this question




share|improve this question








edited Mar 15 at 20:54









Sparhawk

8,32863487




8,32863487










asked Mar 15 at 20:52









Kavin Palaniswamy

204




204











  • Can you be specific about how you define an "empty" file? Is it (at most) one tag on the first line, followed by pairs of open and close tags with nothing but whitespace between them?
    – Sparhawk
    Mar 15 at 20:56










  • How about file size, it could be a good tell if non-empty files are bigger, use find /path -size -128c to find files with less than 128 bytes
    – Dalvenjia
    Mar 15 at 21:06











  • other option is to count the lines, for F in *.xml; do if [ $(wc -l "$F") -lt 4 ]; then echo "$F"; fi; done
    – Dalvenjia
    Mar 15 at 21:11







  • 1




    @Dalvenjia: very bad idea. Think of an inline node like <foo>xxxxxxxxxxxxxxxxxxxxxxxx</foo>
    – Gilles Quenot
    Mar 15 at 21:44











  • What about comments and processing instructions?
    – ErikF
    Mar 15 at 23:07
















  • Can you be specific about how you define an "empty" file? Is it (at most) one tag on the first line, followed by pairs of open and close tags with nothing but whitespace between them?
    – Sparhawk
    Mar 15 at 20:56










  • How about file size, it could be a good tell if non-empty files are bigger, use find /path -size -128c to find files with less than 128 bytes
    – Dalvenjia
    Mar 15 at 21:06











  • other option is to count the lines, for F in *.xml; do if [ $(wc -l "$F") -lt 4 ]; then echo "$F"; fi; done
    – Dalvenjia
    Mar 15 at 21:11







  • 1




    @Dalvenjia: very bad idea. Think of an inline node like <foo>xxxxxxxxxxxxxxxxxxxxxxxx</foo>
    – Gilles Quenot
    Mar 15 at 21:44











  • What about comments and processing instructions?
    – ErikF
    Mar 15 at 23:07















Can you be specific about how you define an "empty" file? Is it (at most) one tag on the first line, followed by pairs of open and close tags with nothing but whitespace between them?
– Sparhawk
Mar 15 at 20:56




Can you be specific about how you define an "empty" file? Is it (at most) one tag on the first line, followed by pairs of open and close tags with nothing but whitespace between them?
– Sparhawk
Mar 15 at 20:56












How about file size, it could be a good tell if non-empty files are bigger, use find /path -size -128c to find files with less than 128 bytes
– Dalvenjia
Mar 15 at 21:06





How about file size, it could be a good tell if non-empty files are bigger, use find /path -size -128c to find files with less than 128 bytes
– Dalvenjia
Mar 15 at 21:06













other option is to count the lines, for F in *.xml; do if [ $(wc -l "$F") -lt 4 ]; then echo "$F"; fi; done
– Dalvenjia
Mar 15 at 21:11





other option is to count the lines, for F in *.xml; do if [ $(wc -l "$F") -lt 4 ]; then echo "$F"; fi; done
– Dalvenjia
Mar 15 at 21:11





1




1




@Dalvenjia: very bad idea. Think of an inline node like <foo>xxxxxxxxxxxxxxxxxxxxxxxx</foo>
– Gilles Quenot
Mar 15 at 21:44





@Dalvenjia: very bad idea. Think of an inline node like <foo>xxxxxxxxxxxxxxxxxxxxxxxx</foo>
– Gilles Quenot
Mar 15 at 21:44













What about comments and processing instructions?
– ErikF
Mar 15 at 23:07




What about comments and processing instructions?
– ErikF
Mar 15 at 23:07










2 Answers
2






active

oldest

votes

















up vote
2
down vote



accepted










Try this using xmllint with a xpath expression :



#!/bin/sh

for xml in *.xml; do
bool=$(xmllint --xpath 'count(//*)=1 and string-length(//*[1])=1' "$xml")
if [ $bool = true ]; then
echo "$xml" >> xml_list_files
fi
done

cat xml_list_files


The expression test that the file have only one node without any text content. In this case, the command return true






share|improve this answer






















  • This is what exactly i was looking for, works like a charm! Thanks a lot bud! Really appreciate the quick response.
    – Kavin Palaniswamy
    Mar 16 at 0:19










  • Thanks. Don't forget to accept/vote up if it fit your needs :)
    – Gilles Quenot
    Mar 16 at 0:22






  • 1




    Accepted and up voted! Thanks again ;)
    – Kavin Palaniswamy
    Mar 16 at 0:23










  • I had fun finding the solution, not that easy at a first glance
    – Gilles Quenot
    Mar 16 at 0:24






  • 1




    Awesome! I know !!! you are a genius sir! I have been braking my head around this with the very limited unix knowledge i got. Am glad i asked here. This the first time am on this website, and definitely not the last. Am gonna try and help others in need like this :) Cheers!
    – Kavin Palaniswamy
    Mar 16 at 0:28

















up vote
0
down vote














to identify and write all xml file names which are empty to a text
file for reporting purpose




find + xmlstarlet solution:



find . -type f -name "*.xml" -exec bash -c 
'v=$(xmlstarlet sel -t -i "count(//*)=1 and //*[1][not(normalize-space())]" -o 1 -b $1);
[[ -n "$v" ]] && echo "$1" >> "empty_xml.txt"' _ ;



empty_xml.txt file should contain a list of needed filenames/filepaths






share|improve this answer






















  • Thanks a lot of the response bud. Unfortunately i was not able to use/test it as i don't have the xmlstartlet installed. Am gonna gonna try and get it installed and get back to you. Thanks again.
    – Kavin Palaniswamy
    Mar 16 at 0:22










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f430487%2fidentify-empty-xml-files%23new-answer', 'question_page');

);

Post as a guest






























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote



accepted










Try this using xmllint with a xpath expression :



#!/bin/sh

for xml in *.xml; do
bool=$(xmllint --xpath 'count(//*)=1 and string-length(//*[1])=1' "$xml")
if [ $bool = true ]; then
echo "$xml" >> xml_list_files
fi
done

cat xml_list_files


The expression test that the file have only one node without any text content. In this case, the command return true






share|improve this answer






















  • This is what exactly i was looking for, works like a charm! Thanks a lot bud! Really appreciate the quick response.
    – Kavin Palaniswamy
    Mar 16 at 0:19










  • Thanks. Don't forget to accept/vote up if it fit your needs :)
    – Gilles Quenot
    Mar 16 at 0:22






  • 1




    Accepted and up voted! Thanks again ;)
    – Kavin Palaniswamy
    Mar 16 at 0:23










  • I had fun finding the solution, not that easy at a first glance
    – Gilles Quenot
    Mar 16 at 0:24






  • 1




    Awesome! I know !!! you are a genius sir! I have been braking my head around this with the very limited unix knowledge i got. Am glad i asked here. This the first time am on this website, and definitely not the last. Am gonna try and help others in need like this :) Cheers!
    – Kavin Palaniswamy
    Mar 16 at 0:28














up vote
2
down vote



accepted










Try this using xmllint with a xpath expression :



#!/bin/sh

for xml in *.xml; do
bool=$(xmllint --xpath 'count(//*)=1 and string-length(//*[1])=1' "$xml")
if [ $bool = true ]; then
echo "$xml" >> xml_list_files
fi
done

cat xml_list_files


The expression test that the file have only one node without any text content. In this case, the command return true






share|improve this answer






















  • This is what exactly i was looking for, works like a charm! Thanks a lot bud! Really appreciate the quick response.
    – Kavin Palaniswamy
    Mar 16 at 0:19










  • Thanks. Don't forget to accept/vote up if it fit your needs :)
    – Gilles Quenot
    Mar 16 at 0:22






  • 1




    Accepted and up voted! Thanks again ;)
    – Kavin Palaniswamy
    Mar 16 at 0:23










  • I had fun finding the solution, not that easy at a first glance
    – Gilles Quenot
    Mar 16 at 0:24






  • 1




    Awesome! I know !!! you are a genius sir! I have been braking my head around this with the very limited unix knowledge i got. Am glad i asked here. This the first time am on this website, and definitely not the last. Am gonna try and help others in need like this :) Cheers!
    – Kavin Palaniswamy
    Mar 16 at 0:28












up vote
2
down vote



accepted







up vote
2
down vote



accepted






Try this using xmllint with a xpath expression :



#!/bin/sh

for xml in *.xml; do
bool=$(xmllint --xpath 'count(//*)=1 and string-length(//*[1])=1' "$xml")
if [ $bool = true ]; then
echo "$xml" >> xml_list_files
fi
done

cat xml_list_files


The expression test that the file have only one node without any text content. In this case, the command return true






share|improve this answer














Try this using xmllint with a xpath expression :



#!/bin/sh

for xml in *.xml; do
bool=$(xmllint --xpath 'count(//*)=1 and string-length(//*[1])=1' "$xml")
if [ $bool = true ]; then
echo "$xml" >> xml_list_files
fi
done

cat xml_list_files


The expression test that the file have only one node without any text content. In this case, the command return true







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 15 at 23:02

























answered Mar 15 at 21:30









Gilles Quenot

15.3k13448




15.3k13448











  • This is what exactly i was looking for, works like a charm! Thanks a lot bud! Really appreciate the quick response.
    – Kavin Palaniswamy
    Mar 16 at 0:19










  • Thanks. Don't forget to accept/vote up if it fit your needs :)
    – Gilles Quenot
    Mar 16 at 0:22






  • 1




    Accepted and up voted! Thanks again ;)
    – Kavin Palaniswamy
    Mar 16 at 0:23










  • I had fun finding the solution, not that easy at a first glance
    – Gilles Quenot
    Mar 16 at 0:24






  • 1




    Awesome! I know !!! you are a genius sir! I have been braking my head around this with the very limited unix knowledge i got. Am glad i asked here. This the first time am on this website, and definitely not the last. Am gonna try and help others in need like this :) Cheers!
    – Kavin Palaniswamy
    Mar 16 at 0:28
















  • This is what exactly i was looking for, works like a charm! Thanks a lot bud! Really appreciate the quick response.
    – Kavin Palaniswamy
    Mar 16 at 0:19










  • Thanks. Don't forget to accept/vote up if it fit your needs :)
    – Gilles Quenot
    Mar 16 at 0:22






  • 1




    Accepted and up voted! Thanks again ;)
    – Kavin Palaniswamy
    Mar 16 at 0:23










  • I had fun finding the solution, not that easy at a first glance
    – Gilles Quenot
    Mar 16 at 0:24






  • 1




    Awesome! I know !!! you are a genius sir! I have been braking my head around this with the very limited unix knowledge i got. Am glad i asked here. This the first time am on this website, and definitely not the last. Am gonna try and help others in need like this :) Cheers!
    – Kavin Palaniswamy
    Mar 16 at 0:28















This is what exactly i was looking for, works like a charm! Thanks a lot bud! Really appreciate the quick response.
– Kavin Palaniswamy
Mar 16 at 0:19




This is what exactly i was looking for, works like a charm! Thanks a lot bud! Really appreciate the quick response.
– Kavin Palaniswamy
Mar 16 at 0:19












Thanks. Don't forget to accept/vote up if it fit your needs :)
– Gilles Quenot
Mar 16 at 0:22




Thanks. Don't forget to accept/vote up if it fit your needs :)
– Gilles Quenot
Mar 16 at 0:22




1




1




Accepted and up voted! Thanks again ;)
– Kavin Palaniswamy
Mar 16 at 0:23




Accepted and up voted! Thanks again ;)
– Kavin Palaniswamy
Mar 16 at 0:23












I had fun finding the solution, not that easy at a first glance
– Gilles Quenot
Mar 16 at 0:24




I had fun finding the solution, not that easy at a first glance
– Gilles Quenot
Mar 16 at 0:24




1




1




Awesome! I know !!! you are a genius sir! I have been braking my head around this with the very limited unix knowledge i got. Am glad i asked here. This the first time am on this website, and definitely not the last. Am gonna try and help others in need like this :) Cheers!
– Kavin Palaniswamy
Mar 16 at 0:28




Awesome! I know !!! you are a genius sir! I have been braking my head around this with the very limited unix knowledge i got. Am glad i asked here. This the first time am on this website, and definitely not the last. Am gonna try and help others in need like this :) Cheers!
– Kavin Palaniswamy
Mar 16 at 0:28












up vote
0
down vote














to identify and write all xml file names which are empty to a text
file for reporting purpose




find + xmlstarlet solution:



find . -type f -name "*.xml" -exec bash -c 
'v=$(xmlstarlet sel -t -i "count(//*)=1 and //*[1][not(normalize-space())]" -o 1 -b $1);
[[ -n "$v" ]] && echo "$1" >> "empty_xml.txt"' _ ;



empty_xml.txt file should contain a list of needed filenames/filepaths






share|improve this answer






















  • Thanks a lot of the response bud. Unfortunately i was not able to use/test it as i don't have the xmlstartlet installed. Am gonna gonna try and get it installed and get back to you. Thanks again.
    – Kavin Palaniswamy
    Mar 16 at 0:22














up vote
0
down vote














to identify and write all xml file names which are empty to a text
file for reporting purpose




find + xmlstarlet solution:



find . -type f -name "*.xml" -exec bash -c 
'v=$(xmlstarlet sel -t -i "count(//*)=1 and //*[1][not(normalize-space())]" -o 1 -b $1);
[[ -n "$v" ]] && echo "$1" >> "empty_xml.txt"' _ ;



empty_xml.txt file should contain a list of needed filenames/filepaths






share|improve this answer






















  • Thanks a lot of the response bud. Unfortunately i was not able to use/test it as i don't have the xmlstartlet installed. Am gonna gonna try and get it installed and get back to you. Thanks again.
    – Kavin Palaniswamy
    Mar 16 at 0:22












up vote
0
down vote










up vote
0
down vote










to identify and write all xml file names which are empty to a text
file for reporting purpose




find + xmlstarlet solution:



find . -type f -name "*.xml" -exec bash -c 
'v=$(xmlstarlet sel -t -i "count(//*)=1 and //*[1][not(normalize-space())]" -o 1 -b $1);
[[ -n "$v" ]] && echo "$1" >> "empty_xml.txt"' _ ;



empty_xml.txt file should contain a list of needed filenames/filepaths






share|improve this answer















to identify and write all xml file names which are empty to a text
file for reporting purpose




find + xmlstarlet solution:



find . -type f -name "*.xml" -exec bash -c 
'v=$(xmlstarlet sel -t -i "count(//*)=1 and //*[1][not(normalize-space())]" -o 1 -b $1);
[[ -n "$v" ]] && echo "$1" >> "empty_xml.txt"' _ ;



empty_xml.txt file should contain a list of needed filenames/filepaths







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 15 at 22:41

























answered Mar 15 at 22:35









RomanPerekhrest

22.4k12144




22.4k12144











  • Thanks a lot of the response bud. Unfortunately i was not able to use/test it as i don't have the xmlstartlet installed. Am gonna gonna try and get it installed and get back to you. Thanks again.
    – Kavin Palaniswamy
    Mar 16 at 0:22
















  • Thanks a lot of the response bud. Unfortunately i was not able to use/test it as i don't have the xmlstartlet installed. Am gonna gonna try and get it installed and get back to you. Thanks again.
    – Kavin Palaniswamy
    Mar 16 at 0:22















Thanks a lot of the response bud. Unfortunately i was not able to use/test it as i don't have the xmlstartlet installed. Am gonna gonna try and get it installed and get back to you. Thanks again.
– Kavin Palaniswamy
Mar 16 at 0:22




Thanks a lot of the response bud. Unfortunately i was not able to use/test it as i don't have the xmlstartlet installed. Am gonna gonna try and get it installed and get back to you. Thanks again.
– Kavin Palaniswamy
Mar 16 at 0:22












 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f430487%2fidentify-empty-xml-files%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Bahrain

Postfix configuration issue with fips on centos 7; mailgun relay