国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

xmllint命令處理xml與html的例子

2024-05-04 21:48:11
字體:
來源:轉載
供稿:網友

xmllint是一個很方便的處理及驗證xml、處理html的工具,linux下只要安裝libxml2就可以使用這個命令,首先看下其結合--html、--xpath參數處理html時的例子.

例子:curl http://www.survivalescaperooms.com /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),只獲取文本部分的內容,上面的腳本語句執行后的結果如下:

  1. [您的查詢]:8.8.8.8 
  2. 本站主數據: 
  3. 美國 
  4. 本站輔數據:Google Public DNS提供:hypo 
  5. 美國 Google免費的Google Public DNS提供:zwstar參考數據一:美國 
  6. 參考數據二:美國 

下面再結合示例看下其他主要參數的用法.

1、--format

此參數用于格式化xml,使其具有良好的可讀性,假設有xml(person.xml)內容如下:

<person><name>ball</name><age>30</age<sex>male</sex></person>

執行如下操作后其輸出為更易讀的xml格式:

  1. #xmllint --format person.xml 
  2.     <?xml version="1.0"?> 
  3.     <person> 
  4.       <name>ball</name> 
  5.       <age>30</age> 
  6.       <sex>male</sex> 
  7.     </person>  

2、--noblanks

與--format相反,有時為了節省傳輸量,我們希望去掉xml中的空白,這時我們可以使用--noblanks命令.

假設xml(person.xml)內容如下:

  1. <?xml version="1.0"?> 
  2.     <person> 
  3.       <name>ball</name> 
  4.       <age>30</age> 
  5.       <sex>male</sex> 
  6.     </person>  

執行該參數操作后,其輸出結果為:

  1. #xmllint --noblanks person.xml 
  2.     <?xml version="1.0"?> 
  3.     <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

  1. <?xml version="1.0"?> 
  2.     <person> 
  3.       <name>ball</name> 
  4.       <age>30</age> 
  5.       <sex>male</sex> 
  6.     </person> 

person.xsd

  1. <?xml version="1.0"?> 
  2.     <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
  3.       <xs:element name="name" type="xs:string"/> 
  4.       <xs:element name="age" type="xs:integer"/> 
  5.       <xs:element name="sex"> 
  6.         <xs:simpleType> 
  7.           <xs:restriction base="xs:string"> 
  8.             <xs:enumeration value="male"/> 
  9.             <xs:enumeration value="female"/> 
  10.           </xs:restriction> 
  11.         </xs:simpleType> 
  12.       </xs:element> 
  13.       <xs:element name="person"> 
  14.         <xs:complexType> 
  15.           <xs:all> 
  16.             <xs:element ref="name"/> 
  17.             <xs:element ref="age"/> 
  18.             <xs:element ref="sex"/> 
  19.           </xs:all> 
  20.         </xs:complexType> 
  21.       </xs:element> 
  22.     </xs:schema> 

按如下命令執行后的結果是:

  1. #xmllint --schema person.xsd person.xml 
  2.     <?xml version="1.0"?> 
  3.     <person> 
  4.       <name>ball</name> 
  5.       <age>30</age> 
  6.       <sex>male</sex> 
  7.     </person> 
  8.     person.xml validates   

注:默認情況下,驗證后會輸出驗證的文件內容,可以使用 --noout選項去掉此輸出,這樣我們可以只得到最后的驗證結果.

#xmllint --noout --schema person.xsd person.xml

person.xml validates

下面我們改動person.xml,使這份文件age字段和sex都是不符合xsd定義的.

  1. #xmllint --noout --schema person.xsd person.xml 
  2. person.xml:4: element age: Schemas validity error : Element 'age': 'not age' is not a valid value of the atomic type 'xs:integer'. 
  3. person.xml:5: element sex: Schemas validity error : Element 'sex': [facet 'enumeration'] The value 'test' is not an element of the set {'male', 'female'}. 
  4. person.xml:5: element sex: Schemas validity error : Element 'sex': 'test' is not a valid value of the local atomic type. 
  5. person.xml fails to validate  

可以看到xmllint成功的報出了錯誤.

4、關于--schema的輸出

在講輸出之前先看下面一個場景,假如你想通過php執行xmllint然后拿到返回結果,你的代碼通常應該是這個樣子valid.php,實例代碼如下:

  1. $command = "xmllint --noout --schema person.xsd person.xml"
  2. exec($command$output$retval); 
  3. //出錯時返回值不為0 
  4. if ($retval != 0){ 
  5.         var_dump($output); 
  6. }//開源代碼Vevb.com 
  7. else
  8.     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,其內容如下:

  1. <?xml version="1.0"?> 
  2. <purchaseOrder orderDate="1999-10-20"> 
  3.     <shipTo country="US"> 
  4.         <name>Alice Smith</name> 
  5.         <street>123 Maple Street</street> 
  6.         <city>Mill Valley</city> 
  7.         <state>CA</state> 
  8.         <zip>90952</zip> 
  9.     </shipTo> 
  10.     <billTo country="US"> 
  11.         <name>Robert Smith</name> 
  12.         <street>8 Oak Avenue</street> 
  13.         <city>Old Town</city> 
  14.         <state>PA</state> 
  15.         <zip>95819</zip> 
  16.     </billTo> 
  17.     <comment>Hurry, my lawn is going wild!</comment> 
  18.     <items> 
  19.         <item partNum="872-AA"> 
  20.             <productName>Lawnmower</productName> 
  21.             <quantity>1</quantity> 
  22.             <USPrice>148.95</USPrice> 
  23.             <comment>Confirm this is electric</comment> 
  24.         </item> 
  25.         <item partNum="926-AA"> 
  26.             <productName>Baby Monitor</productName> 
  27.             <quantity>1</quantity> 
  28.             <USPrice>39.98</USPrice> 
  29.             <shipDate>1999-05-21</shipDate> 
  30.         </item> 
  31.     </items> 
  32. </purchaseOrder> 

然后為 po.xml 寫的 schema 文件,取名為 po.xsd,內容如下:

  1. <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
  2.  <xsd:annotation> 
  3.   <xsd:documentation xml:lang="en"> 
  4.    Purchase order schema for Example.com. 
  5.    Copyright 2000 Example.com. All rights reserved. 
  6.   </xsd:documentation> 
  7.  </xsd:annotation> 
  8.  <xsd:element name="purchaseOrder" type="PurchaseOrderType"/> 
  9.  <xsd:element name="comment" type="xsd:string"/> 
  10.  <xsd:complexType name="PurchaseOrderType"> 
  11.   <xsd:sequence> 
  12.    <xsd:element name="shipTo" type="USAddress"/> 
  13.    <xsd:element name="billTo" type="USAddress"/> 
  14.    <xsd:element ref="comment" minOccurs="0"/> 
  15.    <xsd:element name="items"  type="Items"/> 
  16.   </xsd:sequence> 
  17.   <xsd:attribute name="orderDate" type="xsd:date"/> 
  18.  </xsd:complexType> 
  19.  <xsd:complexType name="USAddress"> 
  20.   <xsd:sequence> 
  21.    <xsd:element name="name"   type="xsd:string"/> 
  22.    <xsd:element name="street" type="xsd:string"/> 
  23.    <xsd:element name="city"   type="xsd:string"/> 
  24.    <xsd:element name="state"  type="xsd:string"/> 
  25.    <xsd:element name="zip"    type="xsd:decimal"/> 
  26.   </xsd:sequence> 
  27.   <xsd:attribute name="country" type="xsd:NMTOKEN" 
  28.      fixed="US"/> 
  29.  </xsd:complexType> 
  30.  <xsd:complexType name="Items"> 
  31.   <xsd:sequence> 
  32.    <xsd:element name="item" minOccurs="0" maxOccurs="unbounded"> 
  33.     <xsd:complexType> 
  34.      <xsd:sequence> 
  35.       <xsd:element name="productName" type="xsd:string"/> 
  36.       <xsd:element name="quantity"> 
  37.        <xsd:simpleType> 
  38.         <xsd:restriction base="xsd:positiveInteger"> 
  39.          <xsd:maxExclusive value="100"/> 
  40.         </xsd:restriction> 
  41.        </xsd:simpleType> 
  42.       </xsd:element> 
  43.       <xsd:element name="USPrice"  type="xsd:decimal"/> 
  44.       <xsd:element ref="comment"   minOccurs="0"/> 
  45.       <xsd:element name="shipDate" type="xsd:date" minOccurs="0"/> 
  46.      </xsd:sequence> 
  47.      <xsd:attribute name="partNum" type="SKU" use="required"/> 
  48.     </xsd:complexType> 
  49.    </xsd:element> 
  50.   </xsd:sequence> 
  51.  </xsd:complexType> 
  52.  <!-- Stock Keeping Unit, a code for identifying products --> 
  53.  <xsd:simpleType name="SKU"> 
  54.   <xsd:restriction base="xsd:string"> 
  55.    <xsd:pattern value="d{3}-[A-Z]{2}"/> 
  56.   </xsd:restriction> 
  57.  </xsd:simpleType> 
  58. </xsd:schema> 

使用 xmllint 對 po.xml 文件進行校驗:

$xmllint -schema po.xsd po.xml如果無出錯信息,就說明校驗通過了.

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 精河县| 巴彦县| 当阳市| 永兴县| 台中市| 高雄县| 龙门县| 南靖县| 甘肃省| 兴宁市| 五指山市| 丰宁| 乌拉特中旗| 吉林市| 安福县| 磴口县| 手游| 隆子县| 定边县| 武汉市| 连平县| 怀远县| 兰州市| 新河县| 峨眉山市| 绵竹市| 沙雅县| 太白县| 阿合奇县| 肥西县| 法库县| 乐清市| 喀什市| 东乡县| 牡丹江市| 绥滨县| 汶川县| 视频| 宁津县| 望奎县| 钟祥市|