利用xmllint指令處理xml

PHPz
發布: 2017-04-02 11:10:56
原創
4371 人瀏覽過

範例

curl http://www.php.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)內容如下:


ball30male  
執行以下操作後其輸出為更易讀的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 &#39;age&#39;: &#39;not age&#39; is not a valid value of the atomic type &#39;xs:integer&#39;.
person.xml:5: element sex: Schemas validity error : Element &#39;sex&#39;: [facet &#39;enumeration&#39;] The value &#39;test&#39; is not an element of the set {&#39;male&#39;, &#39;female&#39;}.
person.xml:5: element sex: Schemas validity error : Element &#39;sex&#39;: &#39;test&#39; 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>
登入後複製

然後為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>
登入後複製

使用xmllint 對po.xml 檔案進行校驗:

#$ xmllint   -schema po.xsd po.xml如果無出錯訊息,就表示校驗通過了。

以上是利用xmllint指令處理xml的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!