I had a slight need to convert books in FB format to xhtml.
Well, of course I
went to
www.gribuser.ru/xml/fictionbook/2.0/xsl/export where I took the file
FB2_2_xhtml.xsl (at the same time captured
FB2_2_txt.xsl, you
never need to get a book in text form)
I tried to convert the book, and noticed a little problem, the pictures were gone. :(
In principle, the drawings are not the main thing in the book, but still a little offensive. Therefore, we will find out what the problem is.
According to the FB2 standard, embedded images are described in binary tags. And they are referenced by an anchor in the link, that is,
< img src ='#cover.jpg' />
At first the idea arose to simply pull out the pictures, and put them in the folder with the converted file. It turned out something like
<?php
$ path = ' book ' ; //
$ doc = new DO MDocument ( ) ;
$ doc -> load ( $ xml_filename ) ;
$ nodes = $ doc -> getElementsByTagName ( ' binary ' ) ;
foreach ( $ nodes as $ node )
{
$ value = base64_decode ( $ node -> nodeValue ) ;
$ fname = $ node -> attributes -> getNamedItem ( ' id ' ) -> nodeValue ;
$ fh = fopen ( $ path . ' / ' . $ fname , ' w+ ' ) ;
fwrite ( $ fh , $ value ) ;
fclose ( $ fh ) ;
}
Then we do xslt file conversion taken from the off site and enjoy life, pictures appear. That's just the volume, if you make a small library, then you need to store txt files, fb2 files, xhtml, and even the pictures are expensive. In principle, you can store one fb2'shku and receive a file by request, the delay is small, a couple of seconds and you can wait (this is on a netbook, on the desktop only the entry is performed). And then I remembered that there is such a thing as
Data Uri , so why not use it and not embed the drawings directly into the final xhtml? Of course, the size will grow, but after all we get xhtml only to see when it is necessary, besides, if you get such an xsl file, you can read fb2 normally in the browser.
And so I started digging into the FB2_2_xhtml.xsl file.
Immediately see the reason why the pictures disappeared
<xsl :template match = " fb:image " >
<div align = " center " >
<img border = " 1 " >
<xsl :choose >
<xsl :when test = " starts-with(@xlink:href,'#') " >
<xsl :attribute name = " src " > <xsl : value -of select = " substring-after(@xlink:href,'#') " / > </xsl :attribute >
</xsl :when >
<xsl :otherwise >
<xsl :attribute name = " src " > <xsl : value -of select = " @xlink:href " / > </xsl :attribute >
</xsl :otherwise >
</xsl :choose >
</img >
</div >
</xsl :template >
')
in this case, just cut # before the picture. And it is assumed that the pictures will lie nearby.
After some shamanism, the following code came out
<xsl :when test = " starts-with(@xlink:href,'#') " >
<xsl :attribute name = " src " >
<xsl : text > data: </xsl : text >
<xsl :variable name = " href " select = " substring-after(@xlink:href,'#') " / >
<set variable = " href " expression = " substring-after(@xlink:href,'#') " / >
<xsl : value -of select = " //fb:binary[@id=$href]/@content-type " disable-output-escaping = " yes " / >
<xsl : text > ;base64, </xsl : text >
<xsl : value -of select = " //fb:binary[@id=$href] " disable-output-escaping = " yes " / >
</xsl :attribute >
</xsl :when >
Why that simple construction
<xsl :variable name = " href " select = " substring-after(@xlink:href,'#') " / >
I did not want to set the variable, it was not seen in the xpath expression, after additional definition
<set variable = " href " expression = " substring-after(@xlink:href,'#') " / >
everything worked as it should.
Also made minor changes regarding the withdrawal of the book title above
<h4 align = " center " >
<xsl : value -of select = " fb:description/fb:title-info/fb:book-title " / >
</h4 >
And also the cover
<xsl : for -each select = " fb:description/fb:title-info/fb:coverpage/fb:image " >
<xsl :call-template name = " image " / >
</xsl : for -each >
having previously corrected
<xsl:template match="fb:image">
on
<xsl:template match="fb:image" name="image">
It may be worthwhile to move the content to the end, but in principle, everything suits me.
The resulting file can be taken here
bit.ly/1JOIvzPS while I was looking for a suitable solution in nete I came across
rusec.livejournal.com/11740.html :)
Well, regarding the fb2 reader under Linux I can not quote a message that came across on the
loreFound a great reader for fictonbook:
"Zcat book.fb2.zip | xsltproc FB2_2_txt.xsl - | less -s"
;)
juick.com/demyan/70753
PPS I hope this is useful to someone. :)