2017-06-08 19 views
1

Webサービスから取得したWSDLファイル内に<wsdl:types></wsdl:types>のネストされたXSDに対して検証する必要があるXMLファイルがあります。wsdl:types内でネストされたスキーマに対するXMLドキュメントの検証

<wsdl:types></wsdl:types>の中には、いくつかのものがあります。<xs:schema>です。

Element '{http://schemas.xmlsoap.org/soap/envelope/}Envelope': No 
matching global declaration available for the validation root. 

これまでのところ、私は<xs:schema>秒(すべてを抽出している:私は、XMLファイルを読み込むと、私はプログラムを実行したときのXSDは、しかし、私は次のエラーを取得していたと照合して検証するためにルビーの宝石nokogiriを使用していますそれらのうち4つ)をコピーし、schema.xsdファイルにコピーしました。

コード

require 'rubygems' 
require 'nokogiri' 

def validate(document_path, schema_path) 
    schema = Nokogiri::XML::Schema(File.read(schema_path)) 
    document = Nokogiri::XML(File.read(document_path)) 
    schema.validate(document) 
end 


validate('data.xml', 'schema.xsd').each do |error| 
    puts error.message 
end 

そこで、基本的に私schema.xsdは、私は自分自身にとは考えていないされ、そこに複数の<xs:schema>秒を持っている私はschemaをインスタンス化するときnokogiriがエラーをスローしていなかったため、問題ですオブジェクト。

schema.xsd

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://schemas.microsoft.com/2003/10/Serialization/" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://schemas.microsoft.com/2003/10/Serialization/"> 
    <xs:element name="anyType" nillable="true" type="xs:anyType"/> 
    <xs:element name="anyURI" nillable="true" type="xs:anyURI"/> 
    <!-- data in here --> 
</xs:schema> 

<!-- three more xs:schema tags removed for brevity --> 

data.xmlに

<?xml version='1.0' ?> 
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"> 
    <env:Header /> 
    <env:Body> 
    <CreatePerson xmlns="https://person.example.com/"> 
     <oMessageType xmlns:epa="http://schemas.datacontract.org/2004/07/whatever" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:array="http://schemas.microsoft.com/2003/10/Serialization/Arrays"> 
     <person:bodyField> 
      <!-- data in here --> 
     </person:bodyField> 
     <!-- more data in here --> 
     </oMessageType> 
    </CreatePerson> 
    </env:Body> 
</env:Envelope> 

答えて

1

はい、WSDLないあなたは、プログラミングによって手動で部分的または自動のスキーマを抽出する必要があるので、XSDのscheam。

サンプルコードは次のとおりです。リファクタリングしてコードで使用する必要があります。

str = <<EOF 
    <definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://www.examples.com/wsdl/HelloService.wsdl" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="HelloService" targetNamespace="http://www.examples.com/wsdl/HelloService.wsdl"> 
    <types> 
     <schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://schemas.microsoft.com/2003/10/Serialization/" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://schemas.microsoft.com/2003/10/Serialization/"> 
      <element name="anyType" nillable="true" type="anyType"/> 
      <element name="anyURI" nillable="true" type="anyURI"/> 
      <!-- data in here --> 
     </schema> 
    </types> 
    <message name="SayHelloRequest"> 
     <part name="firstName" type="xsd:string"/> 
    </message> 
    <message name="SayHelloResponse"> 
     <part name="greeting" type="xsd:string"/> 
    </message> 
    <portType name="Hello_PortType"> 
     <operation name="sayHello"> 
      <input message="tns:SayHelloRequest"/> 
      <output message="tns:SayHelloResponse"/> 
     </operation> 
    </portType> 
    <binding name="Hello_Binding" type="tns:Hello_PortType"> 
     <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/> 
     <operation name="sayHello"> 
      <soap:operation soapAction="sayHello"/> 
      <input> 
       <soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:examples:helloservice"/> 
      </input> 
      <output> 
       <soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:examples:helloservice"/> 
      </output> 
     </operation> 
    </binding> 
    <service name="Hello_Service"> 
     <documentation>WSDL File for HelloService</documentation> 
     <port name="Hello_Port" binding="tns:Hello_Binding"> 
      <soap:address location="http://www.examples.com/SayHello/"/> 
     </port> 
    </service> 
</definitions> 

EOF 

require 'rubygems' 
require 'nokogiri' 

doc = Nokogiri::XML(str) 

doc.root.children.each do |child| 
    if child.node_name == 'types' 
     types = child 
     # p types.inner_html 
     xsd_doc = Nokogiri::XML(types.inner_html) 

     # p xsd_doc.root 

     xsd = Nokogiri::XML::Schema.from_document xsd_doc.root 
    end 
end 
+0

私は先に行って、あなたがここに持っているかのように私のコードをリファクタリングし、私はまだ同じエラーメッセージました:https://pastebin.com/4Kutuqdh – hyde

+0

@hyde主なアイデアは、埋め込まれたスキーマ要素を検索し、新しい文書を作成したら、新しい文書に対してxsdを作成することができます。それでおしまい。私のサンプルコードは作業コピーですが、プロジェクトの提案では、このコードは美しいものではありません。テストのために、wsdl文字列をコピーして 'str'を置き換えることができます。 –

+0

これが、あなたのコードが行うことです - WSDLからスキーマを抽出します。ただし、抽出されたスキーマを使用してデータを検証しようとすると、同じエラーが発生します。要素 '{http://schemas.xmlsoap.org/soap/envelope/}Envelope':いいえ 検証ルート。 – hyde

関連する問題