curl http://www.ki4.cn /ip/?q=8.8.8.8 2>/dev/null | xmllint --html --xpath "//ul[@id='csstb']" - 2>/dev/null | sed -e 's/<[^>]*>//g'
上例中主假如经由过程在123cha上查询的IP地点的归属状况后,经由过程提取效果(ul#csstb),只猎取文本部份的内容。上面的剧本语句实行后的效果以下:
[您的查询]:8.8.8.8
本站主数据:
美国
本站辅数据:Google Public DNS供应:hypo
美国 Google免费的Google Public DNS供应:zwstar参考数据一:美国
参考数据二:美国
下面再连系示例看下其他主要参数的用法。
1、 --format
此参数用于花样化xml,使其具有优越的可读性。
假定有xml(person.xml)内容以下:
<person><name>ball</name><age>30</age<sex>male</sex></person>
实行以下操纵后其输出为更易读的xml花样:
#xmllint --format person.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 version="1.0"?> <person><name>ball</name><age>30</age><sex>male</sex></person>
3、--schema
运用scheam考证xml文件的正确性(XML Schema 是基于 XML 的 DTD 替代者)
假定有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,使这份文件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
能够看到xmllint胜利的报出了毛病!
4、 关于--schema的输出
在讲输出之前先看下面一个场景,假如你想经由过程php实行xmllint然后拿到返回效果,你的代码一般应该是这个模样valid.php
<?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,毛病信息顺遂拿到!
例子
起首竖立一份 xml 文档,命名为 po.xml,其内容以下:
<?xml version="1.0"?> <purchaseOrder orderDate="1999-10-20"> <shipTo country="US"> <name>Alice Smith</name> <street>123 Maple Street</street> <city>Mill Valley</city> <state>CA</state> <zip>90952</zip> </shipTo> <billTo country="US"> <name>Robert Smith</name> <street>8 Oak Avenue</street> <city>Old Town</city> <state>PA</state> <zip>95819</zip> </billTo> <comment>Hurry, my lawn is going wild!</comment> <items> <item partNum="872-AA"> <productName>Lawnmower</productName> <quantity>1</quantity> <USPrice>148.95</USPrice> <comment>Confirm this is electric</comment> </item> <item partNum="926-AA"> <productName>Baby Monitor</productName> <quantity>1</quantity> <USPrice>39.98</USPrice> <shipDate>1999-05-21</shipDate> </item> </items>
</purchaseOrder>然后为 po.xml 写的 schema 文件,取名为 po.xsd,内容以下:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:annotation> <xsd:documentation xml:lang="en"> Purchase order schema for Example.com. Copyright 2000 Example.com. All rights reserved. </xsd:documentation> </xsd:annotation> <xsd:element name="purchaseOrder" type="PurchaseOrderType"/> <xsd:element name="comment" type="xsd:string"/> <xsd:complexType name="PurchaseOrderType"> <xsd:sequence> <xsd:element name="shipTo" type="USAddress"/> <xsd:element name="billTo" type="USAddress"/> <xsd:element ref="comment" minOccurs="0"/> <xsd:element name="items" type="Items"/> </xsd:sequence> <xsd:attribute name="orderDate" type="xsd:date"/> </xsd:complexType> <xsd:complexType name="USAddress"> <xsd:sequence> <xsd:element name="name" type="xsd:string"/> <xsd:element name="street" type="xsd:string"/> <xsd:element name="city" type="xsd:string"/> <xsd:element name="state" type="xsd:string"/> <xsd:element name="zip" type="xsd:decimal"/> </xsd:sequence> <xsd:attribute name="country" type="xsd:NMTOKEN" fixed="US"/>www.111cn.net </xsd:complexType> <xsd:complexType name="Items"> <xsd:sequence> <xsd:element name="item" minOccurs="0" maxOccurs="unbounded"> <xsd:complexType> <xsd:sequence> <xsd:element name="productName" type="xsd:string"/> <xsd:element name="quantity"> <xsd:simpleType> <xsd:restriction base="xsd:positiveInteger"> <xsd:maxExclusive value="100"/> </xsd:restriction> </xsd:simpleType> </xsd:element> <xsd:element name="USPrice" type="xsd:decimal"/> <xsd:element ref="comment" minOccurs="0"/> <xsd:element name="shipDate" type="xsd:date" minOccurs="0"/> </xsd:sequence> <xsd:attribute name="partNum" type="SKU" use="required"/> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> <!-- Stock Keeping Unit, a code for identifying products --> <xsd:simpleType name="SKU"> <xsd:restriction base="xsd:string"> <xsd:pattern value="d{3}-[A-Z]{2}"/> </xsd:restriction> </xsd:simpleType>
</xsd:schema>运用 xmllint 对 po.xml 文件举行校验:
$ xmllint -schema po.xsd po.xml假如无失足信息,就申明校验经由过程了。
以上就是应用xmllint敕令处置惩罚xml的细致内容,更多请关注ki4网别的相干文章!