0

我在这个线程上找到了文件上传的代码

我尝试修改它以使用 Nokogiri 解析临时文件并根据 xsd 模式对其进行验证,但它总是返回 false。

我还将github上的整个项目上传到了这个网站

我的.xml

<?xml version="1.0" encoding="ISO-8859-1"?>

 <shiporder orderid="889923"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:noNamespaceSchemaLocation="my.xsd">
  <orderperson>John Smith</orderperson>
 <shipto>
   <name>Ola Nordmann</name>
   <address>Langgt 23</address>
   <city>4000 Stavanger</city>
   <country>Norway</country>
 </shipto>
 <item>
   <title>Empire Burlesque</title>
   <note>Special Edition</note>
   <quantity>1</quantity>
   <price>10.10</price>
 </item>
 <item>
   <title>Hide your heart</title>
   <quantity>1</quantity>
   <price>9.90</price>
 </item>
</shiporder>

我的.xsd

<?xml version="1.0" encoding="ISO-8859-1" ?>
 <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

 <xs:simpleType name="stringtype">
   <xs:restriction base="xs:string"/>
 </xs:simpleType>

 <xs:simpleType name="inttype">
   <xs:restriction base="xs:positiveInteger"/>
 </xs:simpleType>

 <xs:simpleType name="dectype">
   <xs:restriction base="xs:decimal"/>
 </xs:simpleType>

 <xs:simpleType name="orderidtype">
   <xs:restriction base="xs:string">
     <xs:pattern value="[0-9]{6}"/>
   </xs:restriction>
 </xs:simpleType>

 <xs:complexType name="shiptotype">
   <xs:sequence>
     <xs:element name="name" type="stringtype"/>
     <xs:element name="address" type="stringtype"/>
     <xs:element name="city" type="stringtype"/>
     <xs:element name="country" type="stringtype"/>
   </xs:sequence>
 </xs:complexType>

 <xs:complexType name="itemtype">
   <xs:sequence>
     <xs:element name="title" type="stringtype"/>
     <xs:element name="note" type="stringtype" minOccurs="0"/>
     <xs:element name="quantity" type="inttype"/>
     <xs:element name="price" type="dectype"/>
   </xs:sequence>
 </xs:complexType>

 <xs:complexType name="shipordertype">
   <xs:sequence>
     <xs:element name="orderperson" type="stringtype"/>
     <xs:element name="shipto" type="shiptotype"/>
     <xs:element name="item" maxOccurs="unbounded" type="itemtype"/>
   </xs:sequence>
   <xs:attribute name="orderid" type="orderidtype" use="required"/>
 </xs:complexType>

<xs:element name="shiporder" type="shipordertype"/>

</xs:schema>

配置.ru

$LOAD_PATH.unshift(File.dirname(__FILE__))
require 'UserUpload'

run UserUpload

用户上传.rb

#encoding utf-8

require 'sinatra/base'
require 'haml'
require 'nokogiri'

class UserUpload < Sinatra::Base

  helpers do
    def open_schema
      @myschema = Nokogiri::XML::Schema(File.open('my.xsd'))
    end

    def check_file
     @test = @myschema.valid? (@mytmp)
     @atest = @myschema.validate (@mytmp)
    end

  end

  get '/upload' do
    haml :upload, :layout => false
  end

  post '/upload' do
    unless params[:file] &&
           (tmpfile = params[:file][:tempfile]) &&
           (name = params[:file][:filename])
      @error = "No file selected"
      return haml(:upload)
    end
    STDERR.puts "Uploading file, original name #{name.inspect}"
    while blk = tmpfile.read(65536)
      # read tempfile with nokogiri and validate against xsd schema
      @mytmp = Nokogiri::XML(tmpfile)
      open_schema
      check_file
      STDERR.puts blk.inspect
    end

    "#{@mytmp}" # returns <?xml version="1.0"?>
    #{}"@test = #{@test} @atest = #{@atest}" # @test = false
  end

end

尝试在命令行上针对 xsd 架构验证 xml 文件没有问题。

xmlreader.rb(ruby 脚本,非 sinatra)

require 'nokogiri'

doc = Nokogiri::XML(File.open('my.xml'))
schema = Nokogiri::XML::Schema(File.open('my.xsd'))
y =  schema.valid? (doc) #returns false if invalid and true if valid
x = schema.validate (doc) #returns a listing of errors
puts "valid? #{y} errors: #{x}"
4

1 回答 1

0

我找到了一个解决方案:

post '/upload' do
  unless params[:file] &&
       (tmpfile = params[:file][:tempfile]) &&
       (name = params[:file][:filename])
    @error = "No file selected"
    return haml(:upload)
  end
  STDERR.puts "Uploading file, original name #{name.inspect}"
  @mytmp = Nokogiri::XML(tmpfile.read)
  open_schema
  check_file
end
于 2011-07-18T11:47:00.947 回答