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 ...
随机推荐
- 【XSY2384】【GDOI2017】微信
致去年的我:这是道广义SAM模板题啊…… 题意: Description Input Output HINT $1\leq N\leq 20$,$1\leq Q\leq 10^5$,字符串总长$\le ...
- 【hdu 6396】Swordsman
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 开k个优先队列.每个队列都满足从小到大那种.. 首先将所有的怪物加入到第一个队列中. 然后对于v[i]>=pq[i].top( ...
- FreeMarker 语法 include 引用模板
一.java 代码 @Test public void testFreeMarker() throws Exception { //1.创建一个模板文件 //2.创建一个Configuration对象 ...
- volatile可见性和指令重排
volatile关键字的2个作用 1.线程的可见性 2.防止指令重排 什么是线程的可见性? 线程的可见性 就是一个线程对一个变量进行更改操作 其他线程获取会获得最新的值. 线程在执行的行 操作主线程的 ...
- HDU5976 Detachment
/* HDU5976 Detachment http://acm.hdu.edu.cn/showproblem.php?pid=5976 数论 等差数列 * * */ #include <cst ...
- poj1035Spell checker
暴力解决. 先把字典里的每一个单词的长度存起来.在查找的时候.就比較长度,在多一个少一个之间找, #include<stdio.h> #include<string.h> #i ...
- getColor()方法过时的替代方法
Android SDK 升級到 23 之後,getResource.getColor(R.color.color_name) 過時 使用新加入的方法 ContextCompat.getColor(co ...
- linux 下password加密程序(能够用于替换shadow文件里的用户password)
源代码例如以下: #include <stdio.h> #include <unistd.h> int main(int argc, char *argv[]){ if(arg ...
- Cocos2d-x 动手实现游戏主循环
因为Cocos2d-x封装的非常好,所以对于非常多新手,他们仅仅知道先new一个场景,在场景上加入布景或精灵,然后用Director的runWithScene便能够执行游戏了.假设给一个精灵加个动作, ...
- intellij idea 运行jedis
到这里下载 http://mvnrepository.com/ jar包! 将jar包放入项目目录中,并引入! 引入包到项目中!创建对象! package com.company; import re ...