xmllint命令
xmllint是一个很方便的处理及验证xml的工具,linux下只要安装libxml2就可以使用这个命令,下面整理一些常用功能
1. --format
<person><name>ball</name><age>30</age><sex>male</sex></person>
执行:
xmllint --format person.xml
得到易读的xml
<?xml version="1.0"?>
<person>
<name>ball</name>
<age>30</age>
<sex>male</sex>
</person>
2. --noblanks
假设xml(person.xml)内容如下
<?xml version="1.0"?>
<person>
<name>ball</name>
<age>30</age>
<sex>male</sex>
</person>
执行:
xmllint --noblanks person.xml
得到去掉空白后的xml
<?xml version="1.0"?>
<person><name>ball</name><age>30</age><sex>male</sex></person>
3.--schema
<?xml version="1.0"?>
<person>
<name>ball</name>
<age>30</age>
<sex>male</sex>
</person>
person.xsd
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="name" type="xs:string"/>
<xs:element name="age" type="xs:integer"/>
<xs:element name="sex">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="male"/>
<xs:enumeration value="female"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="person">
<xs:complexType>
<xs:all>
<xs:element ref="name"/>
<xs:element ref="age"/>
<xs:element ref="sex"/>
</xs:all>
</xs:complexType>
</xs:element>
</xs:schema>
执行:
xmllint --schema person.xsd person.xml
得到
<?xml version="1.0"?>
<person>
<name>ball</name>
<age>30</age>
<sex>male</sex>
</person>
person.xml validates
注意,默认情况下,验证后会输出验证的文件内容,可以使用 --noout选项去掉此输出,这样我们可以只得到最后的验证结果。
执行
xmllint --noout --schema person.xsd person.xml
得到
person.xml validates
下面我们改动person.xml
<?xml version="1.0"?>
<person>
<name>ball</name>
<age>not age</age>
<sex>test</sex>
</person>
xmllint --noout --schema person.xsd person.xml
得到:
person.xml:4: element age: Schemas validity error : Element 'age': 'not age' is not a valid value of the atomic type 'xs:integer'.
person.xml:5: element sex: Schemas validity error : Element 'sex': [facet 'enumeration'] The value 'test' is not an element of the set {'male', 'female'}.
person.xml:5: element sex: Schemas validity error : Element 'sex': 'test' is not a valid value of the local atomic type.
person.xml fails to validate
xmllint成功的报出了错误!
4. 关于--schema的输出
<?php
$command = "xmllint --noout --schema person.xsd person.xml";
exec($command, $output, $retval);
//出错时返回值不为0
if ($retval != 0){
var_dump($output);
}
else{
echo "yeah!";
}
我们保持上文中person.xml的错误。
执行此代码,你会发现,你拿到的output不是错误,而是array(0) {}, amazing!
为什么会这样呢?
因为xmllint --schema,如果验证出错误,错误信息并不是通过标准输出(stdout)显示的,而是通过标准错误(stderr)进行显示的。
而exec的output参数拿到的,只能是标准输出(stdout)显示的内容。
所以,为了拿到出错信息,我们需要将标准错误重定向到标准输出,对应修改代码:
$command = "xmllint --noout --schema person.xsd person.xml 2>&1";
再次执行valid.php,错误信息顺利拿到!
参考:
https://blog.csdn.net/qmhball/article/details/8955588
https://blog.csdn.net/whackw/article/details/51802530
xmllint命令的更多相关文章
- Android源码编译出错解决办法
编译环境:Ubuntu12.04 64位 Android源码:Android 4.3 以下问题是笔者亲自碰到,通过网上查询整合在一起的. 1.error while loading shared li ...
- 73条日常Linux shell命令汇总,总有一条你需要!
转载: 73条日常Linux shell命令汇总,总有一条你需要! 1.检查远程端口是否对bash开放: echo >/dev/tcp/8.8.8.8/53 && echo &q ...
- Linux中shell命令的用法和技巧
使用Linux shell是我每天的基本工作,但我经常会忘记一些有用的shell命令和l技巧.当然,命令我能记住,但我不敢说能记得如何用它执行某个特定任务.于是,我开始在一个文本文件里记录这些用法,并 ...
- Linux Shell常用命令手册(Updating)
检查远程端口是否对bash开放: nc -nvv $IP $PORT telnet $IP $PORT 当前任务的前后台切换: Ctrl + z fg 截取变量前5个字符: ${variable:0: ...
- Linux shell日常命令和技巧
转自:http://www.vaikan.com/linux-shell-tips-and-tricks/ 原文:http://www.techbar.me/linux-shell-tips/ 使用L ...
- 73条日常shell命令汇总,总有一条你需要!
1.检查远程端口是否对bash开放: echo >/dev/tcp/8.8.8.8/53 && echo "open" 2.让进程转入后台: Ctrl + z ...
- centos LAMP第一部分-环境搭建 Linux软件删除方式,mysql安装,apache,PHP,apache和php结合,phpinfo页面,ldd命令 第十九节课
centos LAMP第一部分-环境搭建 Linux软件删除方式,mysql安装,apache,PHP,apache和php结合,phpinfo页面,ldd命令 第十九节课 打命令之后可以输入: e ...
- linux命令汇总1
允许非root用户使用“sudo” root身份登录系统,执行“visudo”,根据示例添加新的一个规则(记住输入的密码是当前用户密码,而不是root密码)#不需要密码执行sudo命令hadoop ...
- 常用Linux shell命令汇总
1.检查远程端口是否对bash开放:echo >/dev/tcp/8.8.8.8/53 && echo "open" 2.让进程转入后台:Ctrl + z 3 ...
随机推荐
- 【hihocoder 1303】模线性方程组
[题目链接]:http://hihocoder.com/problemset/problem/1303 [题意] [题解] /* x % m[1] = r[1] x % m[2] = r[2] x = ...
- UVA11827 Maximum GCD
/* UVA11827 Maximum GCD https://vjudge.net/contest/153365#problem/V 数论 gcd 水题,然而读入比较坑 * */ #include ...
- ubuntu12.04安装翻译软件stardict及卸载
下载: 1.打开软件中心.搜索stardict,星际译王,即ubuntu下的翻译软件. 点击下载就可以. 2.打开终端,输入 $sudo apt-get install stardict 按提示就可以 ...
- winform显示系统托盘,双击图片图表显示窗体,退出窗体是否提示
private void Form1_FormClosing(object sender, FormClosingEventArgs e) { DialogResult result = Messag ...
- jquery源码ajax分析
http://www.cnblogs.com/aaronjs/p/3683925.html
- elcipse 编译cocos2d-x android
http://blog.csdn.net/eyu8874521/article/details/22605695 最開始学习cocos2dx.大多数人可能是被复杂的环境配置过程搞死的,尤其是和Andr ...
- unity3d面试题摘选(全)
======================================= 数据结构和算法非常重要.图形学也非常重要! 大的游戏公司非常看重个人基础.综合能力.也有的看重你实际工作能力,看你的De ...
- 1. 批量梯度下降法BGD 2. 随机梯度下降法SGD 3. 小批量梯度下降法MBGD
排版也是醉了见原文:http://www.cnblogs.com/maybe2030/p/5089753.html 在应用机器学习算法时,我们通常采用梯度下降法来对采用的算法进行训练.其实,常用的梯度 ...
- 【POJ 1704】 Georgia and Bob
[题目链接] http://poj.org/problem?id=1704 [算法] 阶梯博弈 [代码] #include <algorithm> #include <bitset& ...
- 将字符串序列化Object格式
using Newtonsoft.Json; 首先引用 Newtonsoft.Json; 定义一个字符串 string str = "[{'ID':8.0,'PAGEID':201.0,' ...