Writing to KHTMLPart and stylesheets

View: New views
8 Messages — Rating Filter:   Alert me  

Writing to KHTMLPart and stylesheets

by Bugzilla from lemma@confuego.org :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

I'm currently trying to create a small plugin for applying XSL
transformations to XML documents loaded in Konqueror (using libxml and
libxslt as QtXmlPatterns' XSLT isn't fully compliant with eg. docbook).

Most of what I'm doing is already working but I'm having trouble writing the
output back to the KHTMLPart if its document type doesn't match the
currently loaded document.

I've been using this small example:
-----------------------
part->begin();
part->write("<html><head><meta http-equiv=\"Content-Type\""
   "content=\"text/html; charset=UTF-8\"></head><body><p align=\"center\""
   "style=\"font-family:Tahoma; font-size:64px; color:red\">hello"
   "world!</p></body></html>");
part->end();
------------------------

If there's currently no document loaded or if the currently loaded document
is a html document (no matter if it has an <?xml?> header or not), the
output is displayed properly.
However if the current document is an xml document of unknown type it
displays a parsing error (as the new content is not proper xml because meta
misses the ending tag).

Is there any way I could work around this?

Apart from that I've also been failing to get the attributes of an embedded
<?xml-stylesheet?> with type "text/xsl" using part->document()-
>styleSheets(). It seems this only gives me css stylesheets. Is this
assumption correct and the behaviour intended?

Thanks and kind regards,
Michael



Re: Writing to KHTMLPart and stylesheets

by Maksim Orlovich-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Tuesday 04 August 2009 20:01:24 Michael Leupold wrote:
> Hi,
>
> I'm currently trying to create a small plugin for applying XSL
> transformations to XML documents loaded in Konqueror (using libxml and
> libxslt as QtXmlPatterns' XSLT isn't fully compliant with eg. docbook).

Very cool.

> If there's currently no document loaded or if the currently loaded document
> is a html document (no matter if it has an <?xml?> header or not), the
> output is displayed properly.
> However if the current document is an xml document of unknown type it
> displays a parsing error (as the new content is not proper xml because meta
> misses the ending tag).
>
> Is there any way I could work around this?

You can set the mimetype you want via KParts' arguments:
KParts::OpenUrlArguments args = part->arguments();
args.setMimeType("foo/bar");
part->setArguments(args);


> Apart from that I've also been failing to get the attributes of an embedded
> <?xml-stylesheet?> with type "text/xsl" using part->document()-
>
> >styleSheets(). It seems this only gives me css stylesheets. Is this
>
> assumption correct and the behaviour intended?

Yeah. Look for a PROCESSING_INSTRUCTION_NODE child of the document, and go
from there. (Hmm, maybe we should export traverseNextChild/traversePrevChild
in public API)?

Thanks,
Maks

Re: Writing to KHTMLPart and stylesheets

by Bugzilla from lemma@confuego.org :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Maksim Orlovich wrote:

> On Tuesday 04 August 2009 20:01:24 Michael Leupold wrote:
>> If there's currently no document loaded or if the currently loaded
>> document is a html document (no matter if it has an <?xml?> header or
>> not), the output is displayed properly.
>> However if the current document is an xml document of unknown type it
>> displays a parsing error (as the new content is not proper xml because
>> meta misses the ending tag).
>>
>> Is there any way I could work around this?
>
> You can set the mimetype you want via KParts' arguments:
> KParts::OpenUrlArguments args = part->arguments();
> args.setMimeType("foo/bar");
> part->setArguments(args);

Awesome. Works fine.

>> Apart from that I've also been failing to get the attributes of an
>> embedded <?xml-stylesheet?> with type "text/xsl" using part->document()-
>>
>> >styleSheets(). It seems this only gives me css stylesheets. Is this
>>
>> assumption correct and the behaviour intended?
>
> Yeah. Look for a PROCESSING_INSTRUCTION_NODE child of the document, and go
> from there. (Hmm, maybe we should export
> traverseNextChild/traversePrevChild in public API)?

Alright, I'll try that tomorrow. For the moment I was using libxml's XPath
but the code turned out pretty weird because I put the contents back into a
QString for parsing.

Regards,
Michael



Re: Writing to KHTMLPart and stylesheets

by Bugzilla from lemma@confuego.org :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Michael Leupold wrote:

> Maksim Orlovich wrote:
>> On Tuesday 04 August 2009 20:01:24 Michael Leupold wrote:
>>> If there's currently no document loaded or if the currently loaded
>>> document is a html document (no matter if it has an <?xml?> header or
>>> not), the output is displayed properly.
>>> However if the current document is an xml document of unknown type it
>>> displays a parsing error (as the new content is not proper xml because
>>> meta misses the ending tag).
>>>
>>> Is there any way I could work around this?
>>
>> You can set the mimetype you want via KParts' arguments:
>> KParts::OpenUrlArguments args = part->arguments();
>> args.setMimeType("foo/bar");
>> part->setArguments(args);
>
> Awesome. Works fine.

Is args.setMimeType(""); part->setArguments(args); legal? It seems this will
work for xslt generating html as well as xhtml. If it isn't I'll have to
find a yet to determine the proper mimetype yet.

>>> Apart from that I've also been failing to get the attributes of an
>>> embedded <?xml-stylesheet?> with type "text/xsl" using part->document()-
>>>
>>> >styleSheets(). It seems this only gives me css stylesheets. Is this
>>>
>>> assumption correct and the behaviour intended?
>>
>> Yeah. Look for a PROCESSING_INSTRUCTION_NODE child of the document, and
>> go from there. (Hmm, maybe we should export
>> traverseNextChild/traversePrevChild in public API)?
>
> Alright, I'll try that tomorrow. For the moment I was using libxml's XPath
> but the code turned out pretty weird because I put the contents back into
> a QString for parsing.

Works as well and turned out to be simpler than the XPath stuff.

Regards,
Michael



Re: Writing to KHTMLPart and stylesheets

by Maksim Orlovich-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


> Is args.setMimeType(""); part->setArguments(args); legal?

Not really, I think. It just happens to be treated as HTML --- but that's an
implementation detail.

> It seems this
> will work for xslt generating html as well as xhtml. If it isn't I'll have
> to find a yet to determine the proper mimetype yet.

I don't see how XSLT can possibly produce something other than XML?
And it will certainly not parse XML right (not that the XML parser itself is
perfect --- there is some nonsense stuff there that needs removing...)


> >> Yeah. Look for a PROCESSING_INSTRUCTION_NODE child of the document, and
> >> go from there. (Hmm, maybe we should export
> >> traverseNextChild/traversePrevChild in public API)?
> >
> > Alright, I'll try that tomorrow. For the moment I was using libxml's
> > XPath but the code turned out pretty weird because I put the contents
> > back into a QString for parsing.
>
> Works as well and turned out to be simpler than the XPath stuff.

Good to hear.

Thanks,
Maks

Re: Writing to KHTMLPart and stylesheets

by Bugzilla from lemma@confuego.org :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Maksim Orlovich wrote:

>> Is args.setMimeType(""); part->setArguments(args); legal?
>
> Not really, I think. It just happens to be treated as HTML --- but that's
> an implementation detail.
>
>> It seems this
>> will work for xslt generating html as well as xhtml. If it isn't I'll
>> have to find a yet to determine the proper mimetype yet.
>
> I don't see how XSLT can possibly produce something other than XML?
> And it will certainly not parse XML right (not that the XML parser itself
> is perfect --- there is some nonsense stuff there that needs removing...)

It can using <xsl:output method="html"/>. I guess I could just scout the
stylesheet for that and set the mime-type accordingly.

Regards,
Michael



Re: Writing to KHTMLPart and stylesheets

by Maksim Orlovich-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Wednesday 05 August 2009 09:01:54 Michael Leupold wrote:

> Maksim Orlovich wrote:
> >> Is args.setMimeType(""); part->setArguments(args); legal?
> >
> > Not really, I think. It just happens to be treated as HTML --- but that's
> > an implementation detail.
> >
> >> It seems this
> >> will work for xslt generating html as well as xhtml. If it isn't I'll
> >> have to find a yet to determine the proper mimetype yet.
> >
> > I don't see how XSLT can possibly produce something other than XML?
> > And it will certainly not parse XML right (not that the XML parser itself
> > is perfect --- there is some nonsense stuff there that needs removing...)
>
> It can using <xsl:output method="html"/>. I guess I could just scout the
> stylesheet for that and set the mime-type accordingly.

Is there no accessor for that in libxslt? That's unfortunate.

I wonder what it does with namespaces and the like in that case?
I am not a big fan of serialization methods that can't reproduce the original
(which reminds me there are some .innerHTML() fixes I should do).

Thanks,
Maks

Re: Writing to KHTMLPart and stylesheets

by Bugzilla from lemma@confuego.org :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Maksim Orlovich wrote:

> On Wednesday 05 August 2009 09:01:54 Michael Leupold wrote:
>> Maksim Orlovich wrote:
>> >> Is args.setMimeType(""); part->setArguments(args); legal?
>> >
>> > Not really, I think. It just happens to be treated as HTML --- but
>> > that's an implementation detail.
>> >
>> >> It seems this
>> >> will work for xslt generating html as well as xhtml. If it isn't I'll
>> >> have to find a yet to determine the proper mimetype yet.
>> >
>> > I don't see how XSLT can possibly produce something other than XML?
>> > And it will certainly not parse XML right (not that the XML parser
>> > itself is perfect --- there is some nonsense stuff there that needs
>> > removing...)
>>
>> It can using <xsl:output method="html"/>. I guess I could just scout the
>> stylesheet for that and set the mime-type accordingly.
>
> Is there no accessor for that in libxslt? That's unfortunate.

There might be. The problem is that that IMO libxml and libxslt api docs are
really poor to search through. I'll keep searching.

Regards,
Michael