newbie xsl:processing-instruction question

M

Martin Welch

I'm in the (very) early stages of learning xml/xsl and I've been
experimenting happily with transforming xml to xhtml.

I then decided to try a transform of xml to xml:

----------------- pi.xml -----------------
<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet version="1.0" type="text/xsl" href="pi.xslt"?>

<article>
<content>Hello World</content>
</article>
------------------------------------------

----------------- pi.xslt ----------------
<?xml version='1.0' encoding="utf-8" ?>

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:eek:utput method="xml" encoding="utf-8" omit-xml-declaration="no"
version="1.0" />

<xsl:template match="article">
<xsl:processing-instruction name="xml-stylesheet">
<xsl:text>href='page.xslt' type='text/xsl'</xsl:text>
</xsl:processing-instruction>

<result>
</result>
</xsl:template>
</xsl:stylesheet>
------------------------------------------

What I'd hoped for would be

------------------------------------------
<?xml version='1.0' encoding="utf-8" ?>
<?xml-stylesheet version="1.0" type="text/xsl" href="page.xslt"?>

<result />
------------------------------------------

but I only get

------------------------------------------
<result />
------------------------------------------

Now I don't know if this is significant but I'm using firefox 2.x on
WinXP to open pi.xml and looking at the results using WebDeveloper 1.1.4
which gives me an option to view generated source.

I've searched the newsgroups for two days now with no result.

Could anyone give me a clue where I might be going wrong? Can anyone
suggest a better (free) tool to help 'debug' my feeble attempts?

Thanks

Martin
 
M

Martin Honnen

Martin said:
Now I don't know if this is significant but I'm using firefox 2.x on
WinXP to open pi.xml and looking at the results using WebDeveloper 1.1.4
which gives me an option to view generated source.

I've searched the newsgroups for two days now with no result.

Could anyone give me a clue where I might be going wrong? Can anyone
suggest a better (free) tool to help 'debug' my feeble attempts?

I have tried your sample with Firefox 2.0, to check the result document
I have typed
javascript: alert(new XMLSerializer().serializeToString(document)); void 0
in the location bar (as one line) and hit enter.
Rest assured that the processing instruction is part of the result
document so the XSLT transformation is working fine in Firefox.
You can also simply use Tools -> DOM Inspector to see the nodes in the
result document, it also shows the processing instruction node.

No idea about WebDeveloper, check whether they have a mailing list or
forum to ask why it does not show that processing instruction.

And generally, unless you are specifically developing for Firefox, I
think learning XSLT and transforming XML to XML is better done with a
command line XSLT processor such as (for instance) Saxon 6
<URL:http://saxon.sourceforge.net/saxon6.5.5/index.html> as that way you
see the result document in the console window or in a file editor. A
browser tries to render the XML and that way you see only the text node
content and not the XML.
 
M

Martin Welch

Wow,

Thanks for the swift, comprehensive reply.

I tried your javascript trick (?) and I got:

----
<?xml-stylesheet version="1.0" type="text/xsl" href="page.xslt"?>

<result />
----

However I don't get:

<?xml version='1.0' encoding="utf-8" ?>

I assume that's FireFox 'trying to render the xml' :)

I'm downloading saxon now...

Having solved that mystery I now find myself with another one which
perhaps would be better as a separate thread. I'll mention it here just
in case it's another obvious case of ignorance..:

I'd hoped that the output of the transform would also be transformed
into html by FireFox (which is the purpose of page.xslt):

------------------
<?xml version='1.0' encoding="utf-8" ?>
<?xml-stylesheet version="1.0" type="text/xsl" href="page.xslt"?>

<result />
------------------

Can browsers perform this two stage transform?

Thanks again for your time,

Martin
 
M

Martin Honnen

Martin said:
I tried your javascript trick (?) and I got:

----
<?xml-stylesheet version="1.0" type="text/xsl" href="page.xslt"?>

<result />
----

However I don't get:

<?xml version='1.0' encoding="utf-8" ?>

That is the XML declaration and it is not part of the DOM object model
so you can't see that by serializing the DOM document object, whether
that DOM document is the result of an XSLT transformation or just a
static XML document.


I'd hoped that the output of the transform would also be transformed
into html by FireFox (which is the purpose of page.xslt):

------------------
<?xml version='1.0' encoding="utf-8" ?>
<?xml-stylesheet version="1.0" type="text/xsl" href="page.xslt"?>

<result />

Neither Firefox nor IE nor Opera do as far as I know, they take the
processing instruction in the first XML document and apply that
transformation and render the result. Opera and Firefox can at least
render it as XML while IE even treats the result of an XSLT
transformation as HTML, whatever the output method is.

If you want to chain transformations in the browser you have to script
them, Mozilla's XSLT processor API exposed to JavaScript is described
here:
<URL:http://developer.mozilla.org/en/docs/Using_the_Mozilla_JavaScript_interface_to_XSL_Transformations>
Opera 9 also implements that API, as will Safari 3 as far as I know.
IE uses MSXML for XML parsing and XSLT transformations, it is also
scriptable but has a different API.
 
M

Martin Welch

Ah that's that, then.

Ok, I'll look to doing the first transform server side for the time
being while I look into the feasability of client side chain
transformations.

Thanks again for your help.

Martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top