使用xmllint处理/验证xml

来源:互联网 发布:淘宝童装店标志设计 编辑:程序博客网 时间:2024/06/06 21:44
xmllint是一个很方便的处理及验证xml的工具,linux下只要安装libxml2就可以使用这个命令,下面整理一些常用功能
1. --format
此参数用于格式化xml,使其具有良好的可读性。
假设有xml(person.xml)内容如下:
<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
与--format相反,有时为了节省传输量,我们希望去掉xml中的空白,这时我们可以使用--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
使用scheam验证xml文件的正确性(了解schema的知识请猛击这里)
假设有xml文件(person.xml)和scheam文件(person.xsd)文件,内容分别如下
person.xml
<?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>
显然,这份文件age字段和sex都是不符合xsd定义的。
执行
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
good!xmllint成功的报出了错误!

4. 关于--schema的输出
在讲输出之前先看下面一个场景,假如你想通过php执行xmllint然后拿到返回结果,你的代码通常应该是这个样子
valid.php
<?php$command = "xmllint --noout --schema person.xsd person.xml";exec($command, $output, $retval);//出错时返回值不为0if ($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,错误信息顺利拿到!

附小问题
--noout参数关闭的是什么输出呢:)


原创粉丝点击