如何在此架构模板中为body元素创建xsi:type属性?

时间:2011-07-20 12:01:06

标签: xml xsd schema xsitype

这是实际的XML。

<?xml version="1.0" encoding="UTF-8"?>
<root:tmApiRoot
xmlns:root="http://www.telmap.com/xmlapi/v7/apiroot" 
xmlns="http://www.telmap.com/xmlapi/v7/general" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
    <root:header>
        <root:bodyVersion>0</root:bodyVersion>
        <root:apiFamily>GENERAL</root:apiFamily>
    </root:header>
    <root:body xsi:type="tmLoginRequest">
        <appName>your application name</appName>
        <userName>someuser</userName>
        <password>somepassword</password>
        <auditString>Something to be written in the audit log</auditString>
        <languages>
            <primary>en</primary>
            <secondary>he</secondary>
        </languages>
        <requestedSessionId>-1006202250</requestedSessionId>
    </root:body>
</root:tmApiRoot>

以下是schema1.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://www.telmap.com/xmlapi/v7/root"
    xmlns:root="http://www.telmap.com/xmlapi/v7/root"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    elementFormDefault="qualified">

    <xs:import namespace="" schemaLocation="schema2.xsd"/>

    <!-- Root Element  -->
    <xs:complexType name="tmApiRootType">
        <xs:sequence>
            <xs:element type="root:tmApiRootRequestType" name="tmApiRoot"/>
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="tmApiRootRequestType">
        <xs:sequence>
            <xs:element type="root:headerType" name="header"/>
            <xs:element type="root:bodyType" name="body"/>
        </xs:sequence>
    </xs:complexType>
    <!-- Root Element  -->

    <!-- Header Element  -->
    <xs:complexType name="headerType">
        <xs:sequence>
            <xs:element type="xs:long" name="bodyVersion"/>
            <xs:element type="xs:string" name="apiFamily"/>
        </xs:sequence>
    </xs:complexType>
    <!-- Header Element  -->

    <!-- Body Element  -->
    <xs:complexType name="bodyType" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
            <xs:sequence>
                <xs:element ref="appName" maxOccurs="1" minOccurs="0" />
                <xs:element ref="userName" maxOccurs="1" minOccurs="0" />
                <xs:element ref="password" maxOccurs="1" minOccurs="0" />
                <xs:element ref="auditString" maxOccurs="1" minOccurs="0" />
                <xs:element ref="sessionId" maxOccurs="1" minOccurs="0" />
                <xs:element ref="city" maxOccurs="1" minOccurs="0" />
                <xs:element ref="street" maxOccurs="1" minOccurs="0" />
                <xs:element ref="jctStreet" maxOccurs="1" minOccurs="0" />
                <xs:element ref="postcode" maxOccurs="1" minOccurs="0" />
                <xs:element ref="singleLineAddress" maxOccurs="1" minOccurs="0" />
                <xs:element ref="numResults" maxOccurs="1" minOccurs="0" /> 
                <xs:element ref="offset" maxOccurs="1" minOccurs="0" /> 

                <xs:element ref="countryCode" maxOccurs="1" minOccurs="0" />
                <xs:element ref="languages" maxOccurs="1" minOccurs="0" />
                <xs:element ref="geoPoint" maxOccurs="1" minOccurs="0" />
                <xs:element ref="requestedSessionId" maxOccurs="1" minOccurs="0" />         
            </xs:sequence>

            <xs:attribute ref="xsi:type" use="required" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
    </xs:complexType>
    <!-- Body Element  -->

</xs:schema>

除了一件小事之外,已经为上面的xml编写了xml架构。问题是如何解析xsi:type的属性<root xsi:type="tmLoginRequest" />

0 个答案:

没有答案