2013年8月7日 星期三

ONVIF -- 如何產生正確的 TopicSet

針對 ONVIF 13.06 test tool 的測試案例 ACCESSCONTROL-12-1-1,進行測試時,
在 Step 4 Validate topics XML representation. 會發生錯誤。

問題是 Test tool 認為 GetEventPropertiesResponse 的 xml 不合法,
錯誤訊息為 "tns1:Configuration/tns1:VideoEncoderConfiguration must be respresennted by a non-qualified element"

查詢event.wsdl內的GetEventPropertiesResponse,可得知問題與 TopicSet 的設定有關,
以下便針對 GetEventPropertiesResponse 內如何產生 TopicSet 作一討論


1. GetEventPropertiesResponse 定義在 event.wsdl 內,這邊有兩處需要注意
a. elementFormDefault="qualified"
<xs:schema targetNamespace="http://www.onvif.org/ver10/events/wsdl"
xmlns:tt="http://www.onvif.org/ver10/schema"
xmlns:wstop="http://docs.oasis-open.org/wsn/t-1"
xmlns:wsnt="http://docs.oasis-open.org/wsn/b-2"
elementFormDefault="qualified"
version="2.2.1">
b.TopicSet 需參考 wstop 的定義
<xs:element ref="wstop:TopicSet">
<xs:annotation>
<xs:documentation>Set of topics supported.</xs:documentation>
</xs:annotation>
</xs:element>

2. elementFormDefault="qualified" 的意思
參考 http://www.w3schools.com/schema/el_schema.asp 的定義,
"qualified"便是要在element前面加上namespace,"unqualified"就是不用加namespace。
如果沒有特別定義的話,就是當成"unqualified"

原文摘錄如下:
Default is "unqualified". "unqualified" indicates that elements from the target namespace are not required to be qualified with the namespace prefix. "qualified" indicates that elements from the target namespace must be qualified with the namespace prefix
參考 http://www.cs.fsu.edu/~engelen/soapfaq.html,舉例如下:
When an element form is qualified by default, elements in XML instances of the schema are required to be namespace qualified. Without going into too much detail, the XML basically looks like:
<x:foo xmlns:x="urn:foobar">
<x:bar>bar is qualified</x:bar>
</x:foo>
or:
<foo xmlns="urn:foobar">
<bar>bar is qualified</bar>
</foo> 
When the element form is unqualified (as in SOAP RPC encoding), we would have:
<x:foo xmlns:x="urn:foobar">
<bar>bar is unqualified</bar>
</x:foo>
     
3."wstop:TopicSet" 需參考 http://docs.oasis-open.org/wsn/t-1.xsd
摘錄部份 Schema 如下:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:wstop="http://docs.oasis-open.org/wsn/t-1"
targetNamespace="http://docs.oasis-open.org/wsn/t-1"
elementFormDefault="qualified" attributeFormDefault="unqualified">

<xsd:complexType name="TopicSetType">
<xsd:complexContent>
<xsd:extension base="wstop:ExtensibleDocumented">
<xsd:sequence>
<xsd:any namespace="##other" minOccurs="0" maxOccurs="unbounded" processContents="lax"/>
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
<xsd:element name="TopicSet" type="wstop:TopicSetType"/>
<xsd:attribute name="topic" type="xsd:boolean" default="false"/>

由此schame,我們可以知道幾件事情
  1. 由 elementFormDefault="qualified",可知此 schema 內定義的 element 都要帶 namespace
  2. TopicSet 的內容,上述的 <xsd:complexContent></xsd:complexContent> 是可由我們自行定義的字串,此部分是否需要帶namespace,也是可自行定義。(ONVIF組織定義)
  3. 由 processContents="lax" 可知,產生的XML沒有嚴格要求是否會檢查 namespace
註:processContents 各項設定的意義如下:
  • strict - the XML processor must obtain the schema for the required namespaces and validate the elements (this is default)
  • lax - same as strict but; if the schema cannot be obtained, no errors will occur
  • skip - The XML processor does not attempt to validate any elements from the specified namespaces

4. 參考 ONVIF-Core-Specification-v230.pdf 9.7 Topic Structure
a. ONVIF 所定義的所有 root topics 如下
<wstop:TopicNamespace name="ONVIF" targetNamespace="http://www.onvif.org/ver10/topics">
<wstop:Topic name="Device"/>
<wstop:Topic name="VideoSource"/>
<wstop:Topic name="VideoEncoder"/>
<wstop:Topic name="VideoAnalytics"/>
<wstop:Topic name="RuleEngine"/>
<wstop:Topic name="PTZController"/>
<wstop:Topic name="AudioSource"/>
<wstop:Topic name="AudioEncoder"/>
<wstop:Topic name="UserAlarm"/>
<wstop:Topic name="MediaControl"/>
<wstop:Topic name="RecordingConfig"/>
<wstop:Topic name="RecordingHistory"/>
<wstop:Topic name="VideoOutput"/>
<wstop:Topic name="AudioOutput"/>
<wstop:Topic name="VideoDecoder"/>
<wstop:Topic name="AudioDecoder"/>
<wstop:Topic name=”Receiver”/>
<wstop:Topic name=”Monitoring”/>
</wstop:TopicNamespace>

b. 摘錄 TopicSet Example 如下
      <wstop:TopicSet xmlns=””>
         <tns1:RuleEngine>
            <LineDetector>
               <Crossed wstop:topic="true">
                  <tt:MessageDescription>
                  <tt:Source>
                     <tt:SimpleItemDescription Name="VideoSourceConfigurationToken"
                                               Type="tt:ReferenceToken"/>
                     <tt:SimpleItemDescription Name="VideoAnalyticsConfigurationToken"
                                               Type="tt:ReferenceToken"/>
                     <tt:SimpleItemDescription Name="Rule" Type="xs:string"/>
                  </tt:Source>
                  <tt:Data>
                     <tt:SimpleItemDescription Name="ObjectId" Type="xs:integer"/>
                  </tt:Data>
               </tt:MessageDescription>
               </Crossed>
            </LineDetector>
         </tns1:RuleEngine>
      </wstop:TopicSet>
NOTE xmlns="" is included in the example to make sure that there is no default namespace in scope for any of the descendents of the TopicSet element, see the [WS-Topics] specification for more information. 
c. 觀察上述的 TopicSet Example 可得知 <LineDetector> 並沒有帶 namespace,所以 TopicSet 的內容應該屬於 unqualified element因此當我們自行定義相關 topic 的 WSDL 時,可以設定 elementFormDefault="unqualified"

5. 實驗結果如下
請注意下列兩個 XML 的紅色粗體字 
Orignal XML: Step4 Validate topics XML representation fail
<tns1:MediaConfiguration>
<tns1:Profile wstop:topic="true">
<tt:MessageDescription IsProperty="true">
<tt:Source>
<tt:SimpleItemDescription Name="ProfileToken" Type="tt:ReferenceToken">
</tt:SimpleItemDescription>
</tt:Source>
<tt:Data>
<tt:ElementItemDescription Name="Config" Type="tt:Profile">
</tt:ElementItemDescription>
</tt:Data>
</tt:MessageDescription>
</tns1:Profile>
</tns1:MediaConfiguration>
New XML: Step4 Validate topics XML representation success
<tns1:MediaConfiguration>
<Profile wstop:topic="true">
<tt:MessageDescription IsProperty="true">
<tt:Source>
<tt:SimpleItemDescription Name="ProfileToken" Type="tt:ReferenceToken">
</tt:SimpleItemDescription>
</tt:Source>
<tt:Data>
<tt:ElementItemDescription Name="Config" Type="tt:Profile">
</tt:ElementItemDescription>
</tt:Data>
</tt:MessageDescription>
</Profile>
</tns1:MediaConfiguration>

6. ACCESSCONTROL-12-1-1 Step 4 的錯誤原因
原先定義的 topic.wsdl 內定義 elementFormDefault="qualified"
只要修改為 elementFormDefault="unqualified" 即可

注意ONVIF 版本的區別:
  • ONVIF-Core-Specification-v230.pdf 9.7 Topic Structure 使用 "unqualified" 的方式
  • ONVIF-Core-Specification-v221.pdf 9.7 Topic Structure 使用 "qualified" 的方式