Render xml + xslt

Takie pytanko jak podpiac xslt pod blok

respond_to do |format| format.xml { render :xml => @costam } end
Da to sie jakos zrobic?

Pierwsze co mi przychodzi do głowy: jeśli w modelu zdefiniujesz metodę

def to_xml ... end
to będzie ona wywołana przy:

render :xml => @costam

…i możesz sobie takiego xmla wygenerować jaki Ci się podoba (np z linkiem do xml-stylesheeta). Ale to łatwe nie będzie. Na pewno chcesz używać XSLT? Możesz napisać dlaczego taka decyzja? (Może uda mi się wybić Ci ją z głowy ;))

projekt zaliczeniowy z przedmiotu xml

MUSI wykorzystywac XML, XSLT, DOM, i co tam sie jeszcze da. no ale bez XSLT nie da rady.

Ja mam 2 pomysly z sieci:

proc = Proc.new { |options| options[:builder].instruct!(:xml-stylesheet, type=>‘text/xsl’, :href=>‘something.xsl’) }
@foo.to_xml :procs => [proc]


Drugi bardziej hard ;]

xml = @costam.to_xml
xhtml = xsltproc schemat.xsl #{xml}
render :text = > xhtml

chyba pierwszy bedzie ok, na arzie nie dziala :confused: nie wiem czemu ale nei dodaje sie linika ze stylami.

cos mi sie udalo zrobic ale mam problem, moze ktos bedzie wiedzial:

mam tak:

def index
@books = Book.find(:all, :order => [‘author,part,title,subtitle’])
proc = Proc.new { |options| options[:builder].instruct!(:‘xml-stylesheet’, :type=>‘text/xsl’, :href=>‘schemat.xsl’) }

respond_to do |format|
  #format.html # index.html.erb
  format.xml  {
    page_xml = @books.to_xml :procs => [proc]
    render :text => page_xml
    #render :xml => @books 
  }
end

end

a w wyniku dostaje:

<?xml version="1.0" encoding="UTF-8"?> Andrzej Sapkowski 2009-05-29T06:48:55Z 6 3 Chrzest ognia
<title>saga o Wiedźminie</title>
<updated-at type="datetime">2009-05-29T06:48:55Z</updated-at>
<where></where>
<?xml-stylesheet href="schemat.xsl" type="text/xsl"?>
Artur Baniewicz
<created-at type="datetime">2009-05-29T06:57:59Z</created-at>
<id type="integer">15</id>
<part type="integer">1</part>
<subtitle>Smoczy pazur</subtitle>
<title>saga o czarokrążcy</title>
<updated-at type="datetime">2009-05-29T06:57:59Z</updated-at>

<where></where>
<?xml-stylesheet href="schemat.xsl" type="text/xsl"?>
Artur Baniewicz 2009-05-29T06:57:34Z 14
<part type="integer">2</part>
<subtitle>Pogrzeb czarownicy</subtitle>
<title>saga o czarokrążcy</title>
<updated-at type="datetime">2009-05-29T06:57:34Z</updated-at>
<where></where>
<?xml-stylesheet href="schemat.xsl" type="text/xsl"?>
....

czyli linika z <?xml-stylesheet href="schemat.xsl" type="text/xsl"?> w kolko sie powtarza a powinna byc raz tylko na samej gorze strony pod znacznikiem <?xml ... ?>

Trochę lepiej ale też nie doskonale:

[code=ruby] def index
@books = Book.all

respond_to do |format|
  format.html # index.html.erb
  format.xml  { render :xml => @books.to_xml { |xml| xml.instruct! "xml-stylesheet", :type => "text/xsl", :href => "/xsl/style.xsl" } }
end

end[/code]
xml-stylesheet jest nadal w złym miejscu (wewnątrz elementu books), ale tylko 1 raz. Firefox się nabiera, może na zaliczeniu też się nabiorą :stuck_out_tongue_winking_eye:

dzieki ;] zawsze cos i jakis punkt wyjscia jest ;]

Ech ogolnie mialo byc w javie ale wymusilem by technologia byla dowolna wkoncu o xml chodzi nie o jave i teraz mam :stuck_out_tongue:

ech niesttey moj domowy firefox sie nie nabiera :confused: (v. 3.0.10)

ale pokombinuje jka to przeniesc jedna linike wyzej :confused:

siedze tez nad innym podejsciem:

def index
@books = Book.find(:all, :order => [‘author,part,title,subtitle’])

respond_to do |format|
  format.xml  {
    f = File.open('/home/gotar/Programowanie/Ruby/Book/public/index.xml', 'w')
    f.write (@books.to_xml)
    f.close
    html = %x(xsltproc /home/gotar/Programowanie/Ruby/Book/public/xslt/schemat.xsl /home/gotar/Programowanie/Ruby/Book/public/index.xml > /home/gotar/Programowanie/Ruby/Book/public/index.html)
    render :file => '/home/gotar/Programowanie/Ruby/Book/public/index.html'
  }
end

end

problem jest tylko ze przy dodaniu nowego recordu np, nie odswieza sie zawartosc, cos z redirectem powrotnym jest nie tak ;/

def create
@book = Book.new(params[:book])

respond_to do |format|
  if @book.save
    #flash[:notice] = 'Book was successfully created.'
    format.html { redirect_to :action => "index"}
    format.xml  { render :xml => @book, :status => :created, :location => @book }
  else
    format.html { render :action => "new" }
    format.xml  { render :xml => @book.errors, :status => :unprocessable_entity }
  end
end

end

moze jakis cache jest? jakos da sie wymusi odczyt z pliku ponowny, a nie z pamieci?

ok poradzilem sobie pomijajac plik, nie skasuje bo moze komus sie przyda:

f = File.open(’/home/gotar/Programowanie/Ruby/Book/public/index.xml’, ‘w’)
f.write (@books.to_xml)
f.close
html = %x(xsltproc /home/gotar/Programowanie/Ruby/Book/public/xslt/schemat.xsl /home/gotar/Programowanie/Ruby/Book/public/index.xml)
render :text => html