Il miglior tutorial Rubino XML, XSLT e XPath Tutorial Nel 2024, in questo tutorial puoi imparare Che cosa è XML?,parser XML e la struttura API,Ruby per analizzare e creare XML,DOM parser,parser SAX,XPath e Ruby,XSLT e Ruby,maggiori informazioni,
Si riferisce a Extensible Markup Language XML (eXtensible Markup Language).
Extensible Markup Language, un sottoinsieme dello Standard Generalized Markup Language, un metodo per la marcatura di un documento elettronico di avere un linguaggio di marcatura strutturato.
Può essere usato per codificare i dati, definire i tipi di dati, è una tecnologia che consente agli utenti di definire il proprio linguaggio di markup fonte lingua. E 'ideale per la trasmissione Web, fornendo un approccio unificato per descrivere e scambiare indipendente da applicazioni o fornitori di dati strutturati.
Per ulteriori informazioni, si prega di consultare il nostro tutorial di XML
XML parser SAX e DOM sono principalmente di due tipi.
RUBY parsing di documenti XML può usare questa libreria REXML biblioteca.
biblioteca REXML è rubino toolkit XML è di usare il linguaggio puro Ruby, seguire le norme XML1.0.
Nelle versioni Ruby1.8 e successive, la biblioteca conterrà RUBY REXML.
biblioteca Percorso REXML è: rexml / documento
Tutti i metodi e le classi sono confezionati in un modulo REXML.
REXML parser ha i seguenti vantaggi rispetto ad altri parser:
Quanto segue è un esempio di codice XML, salvarlo come movies.xml:
<collection shelf="New Arrivals"> <movie title="Enemy Behind"> <type>War, Thriller</type> <format>DVD</format> <year>2003</year> <rating>PG</rating> <stars>10</stars> <description>Talk about a US-Japan war</description> </movie> <movie title="Transformers"> <type>Anime, Science Fiction</type> <format>DVD</format> <year>1989</year> <rating>R</rating> <stars>8</stars> <description>A schientific fiction</description> </movie> <movie title="Trigun"> <type>Anime, Action</type> <format>DVD</format> <episodes>4</episodes> <rating>PG</rating> <stars>10</stars> <description>Vash the Stampede!</description> </movie> <movie title="Ishtar"> <type>Comedy</type> <format>VHS</format> <rating>PG</rating> <stars>2</stars> <description>Viewable boredom</description> </movie> </collection>
Cominciamo ad analizzare i dati XML In primo luogo abbiamo introdotto biblioteca rexml / documento, di solito può essere nella top-level namespace REXML introdotto:
#!/usr/bin/ruby -w require 'rexml/document' include REXML xmlfile = File.new("movies.xml") xmldoc = Document.new(xmlfile) # 获取 root 元素 root = xmldoc.root puts "Root element : " + root.attributes["shelf"] # 以下将输出电影标题 xmldoc.elements.each("collection/movie"){ |e| puts "Movie Title : " + e.attributes["title"] } # 以下将输出所有电影类型 xmldoc.elements.each("collection/movie/type") { |e| puts "Movie Type : " + e.text } # 以下将输出所有电影描述 xmldoc.elements.each("collection/movie/description") { |e| puts "Movie Description : " + e.text }
L'output sopra esempio è:
Root element : New Arrivals Movie Title : Enemy Behind Movie Title : Transformers Movie Title : Trigun Movie Title : Ishtar Movie Type : War, Thriller Movie Type : Anime, Science Fiction Movie Type : Anime, Action Movie Type : Comedy Movie Description : Talk about a US-Japan war Movie Description : A schientific fiction Movie Description : Vash the Stampede! Movie Description : Viewable boredom SAX-like Parsing:
Trasformazione lo stesso file di dati: movies.xml, SAX parsing non è raccomandato come un piccolo file, il seguente è un semplice esempio:
#!/usr/bin/ruby -w require 'rexml/document' require 'rexml/streamlistener' include REXML class MyListener include REXML::StreamListener def tag_start(*args) puts "tag_start: #{args.map {|x| x.inspect}.join(', ')}" end def text(data) return if data =~ /^\w*$/ # whitespace only abbrev = data[0it40] + (data.length > 40 ? "it." : "") puts " text : #{abbrev.inspect}" end end list = MyListener.new xmlfile = File.new("movies.xml") Document.parse_stream(xmlfile, list)
Sopra output è:
tag_start: "collection", {"shelf"=>"New Arrivals"} tag_start: "movie", {"title"=>"Enemy Behind"} tag_start: "type", {} text : "War, Thriller" tag_start: "format", {} tag_start: "year", {} tag_start: "rating", {} tag_start: "stars", {} tag_start: "description", {} text : "Talk about a US-Japan war" tag_start: "movie", {"title"=>"Transformers"} tag_start: "type", {} text : "Anime, Science Fiction" tag_start: "format", {} tag_start: "year", {} tag_start: "rating", {} tag_start: "stars", {} tag_start: "description", {} text : "A schientific fiction" tag_start: "movie", {"title"=>"Trigun"} tag_start: "type", {} text : "Anime, Action" tag_start: "format", {} tag_start: "episodes", {} tag_start: "rating", {} tag_start: "stars", {} tag_start: "description", {} text : "Vash the Stampede!" tag_start: "movie", {"title"=>"Ishtar"} tag_start: "type", {} tag_start: "format", {} tag_start: "rating", {} tag_start: "stars", {} tag_start: "description", {} text : "Viewable boredom"
Possiamo usare XPath per visualizzare XML, XPath per trovare informazioni è un documento in linguaggio XML (Vedere: XPath Tutorial ).
XPath è l'XML Path Language, è un metodo utilizzato per determinare il codice XML (un sottoinsieme del Markup Language Standard Generalized) lingua del documento una parte della posizione. XPath a base di albero XML, e offre la possibilità di cercare nei nodi della struttura di dati nella struttura.
Ruby XPath supporto XPath per classe REXML, che si basa sulla struttura di Analysis (Document Object Model).
#!/usr/bin/ruby -w require 'rexml/document' include REXML xmlfile = File.new("movies.xml") xmldoc = Document.new(xmlfile) # 第一个电影的信息 movie = XPath.first(xmldoc, "//movie") p movie # 打印所有电影类型 XPath.each(xmldoc, "//type") { |e| puts e.text } # 获取所有电影格式的类型,返回数组 names = XPath.match(xmldoc, "//format").map {|x| x.text } p names
L'output sopra esempio è:
<movie title='Enemy Behind'> it. </> War, Thriller Anime, Science Fiction Anime, Action Comedy ["DVD", "DVD", "DVD", "VHS"]
Ruby ha due parser XSLT, una breve descrizione è il seguente:
Questo parser è scritto e mantenuto dalla giustizia Masayoshi Takahash. Questo è scritto principalmente per il sistema operativo Linux, sono necessari i seguenti librerie:
È possibile Ruby-Sablotron trovare queste librerie.
XSLT4R bisogno operazione XMLScan, compresi filmati XSLT4R, che è un modulo rubino 100%. Questi moduli possono utilizzare il metodo di installazione standard di Ruby (cioè install.rb Ruby) di installazione.
sintassi XSLT4R è la seguente:
ruby xslt.rb stylesheet.xsl document.xml [arguments]
Se si desidera utilizzare XSLT4R nella propria applicazione, è possibile introdurre parametri XSLT e di input necessari. Esempi sono i seguenti:
require "xslt" stylesheet = File.readlines("stylesheet.xsl").to_s xml_doc = File.readlines("document.xml").to_s arguments = { 'image_dir' => '/itit' } sheet = XSLT::Stylesheet.new( stylesheet, arguments ) # output to StdOut sheet.apply( xml_doc ) # output to 'str' str = "" sheet.output = [ str ] sheet.apply( xml_doc )