特定のタグを含むXMLを検証する方法

特定のタグを含むXMLを検証する方法

xmlに次のタグが含まれていることをxmllintで確認できますか?

  1. 名前

  2. ユニーク_文字列

  3. バージョン

3つのタグを持つXMLの例:

<?xml version="1.0" encoding="UTF-8" ?>
<!-- tasks configuration file -->
<tasks>
   <Name>destry_srorage_luns</Name>
   <uniq_String>destry_srorage_luns run once at day</uniq_String>
    <Version>14.03.23</Version>

使用しない場合xmllint

xmlで定義されているタグを確認するためにawk / sed / perl onelinerを使用する他の代替方法は何ですか?

XML に Name、uniq_String、Version という 2 つのタグが含まれている場合に予想される出力

 XML_VALID=TRUE

xmlに2つのタグ(Name、uniq_String、Version)が含まれていない場合に予想される出力

 XML_VALID=FALSE

ベストアンサー1

Xmlstarlet解決策:

if xmlstarlet sel -Q -t -c "//*[Name and uniq_String and Version]" input.xml; then
    echo "XML_VALID=TRUE"
else 
    echo "XML_VALID=FALSE"
fi

http://xmlstar.sourceforge.net/doc/UG/xmlstarlet-ug.html#idm47077139652416


xmllint代替ソリューション:

if xmllint --xpath "//*[Name and uniq_String and Version]" input.xml &>/dev/null; then
    echo "XML_VALID=TRUE"
else 
    echo "XML_VALID=FALSE"
fi

おすすめ記事