Jump to content

xsl:output method="xml"


sireesha g

Recommended Posts

Hi all,i have an xml where i am applying the xslt with output method="xml" but when we see the output in the browser i am getting the output as text.If any one knows what the reason please share your views. This is my xml file name.xml

<?xml version="1.0"?><?xml-stylesheet href="firstname.xsl" type="text/xsl"?><beatles> <beatle link="http://www.paulmccartney.com">  <name>   <firstname>Paul</firstname>   <lastname>McCartney</lastname>  </name> </beatle> <beatle link="http://www.johnlennon.com">  <name>   <firstname>John</firstname>   <lastname>Lennon</lastname>  </name> </beatle> <beatle link="http://www.georgeharrison.com">  <name>   <firstname>George</firstname>   <lastname>Harrison</lastname>  </name> </beatle> <beatle link="http://www.ringostarr.com">  <name>   <firstname>Ringo</firstname>   <lastname>Starr</lastname>  </name> </beatle> <beatle link="http://www.webucator.com" real="no">  <name>   <firstname>Nat</firstname>   <lastname>Dunn</lastname>  </name> </beatle></beatles>

This is my xsl file firstname.xsl

	<?xml version="1.0" encoding="UTF-8"?>	<xsl:stylesheet version="1.0" 	    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">	 <xsl:output method="xml" indent="yes" version="1.0"/>	 <xsl:template match="name">	  <Matches>	   <Match>We found a name!</Match>	   <Name><xsl:value-of select="."/></Name>	  </Matches>	 </xsl:template>	</xsl:stylesheet>

Output in Browser:We found a first name and it's Paul McCartney We found a first name and it's John Lennon We found a first name and it's George Harrison We found a first name and it's Ringo Starr We found a first name and it's Nat Dunn

Link to comment
Share on other sites

Well browsers in general, once you apply XSLT, expect you to use a format they know to render. So it makes sense to transform XML to HTML or (with some browsers that support SVG or MathML) to transform XML to SVG or to MathML. But when you transform XML to some arbitrary XML like in your sample the browser does nothing more than render the text nodes in the result document. You could apply an CSS stylesheet for better rendering, if that is what you are after. But if you want to the browser to render something then it is better to transform XML to HTML, that has much better support and power than styling abitrary XML with CSS.

Link to comment
Share on other sites

Well browsers in general, once you apply XSLT, expect you to use a format they know to render. So it makes sense to transform XML to HTML or (with some browsers that support SVG or MathML) to transform XML to SVG or to MathML. But when you transform XML to some arbitrary XML like in your sample the browser does nothing more than render the text nodes in the result document. You could apply an CSS stylesheet for better rendering, if that is what you are after. But if you want to the browser to render something then it is better to transform XML to HTML, that has much better support and power than styling abitrary XML with CSS.
Hi martin,I expect my output to be shown the browser in this xml format,but i am getting in text format as i mentioned in the previous post.please do clarify in brief as i am new to xslt.
<?xml version="1.0" encoding="UTF-8"?><Matches> <Match>We found a name!</Match> <Name>PaulMcCartney</Name></Matches>

Link to comment
Share on other sites

Sorry, your choosen "XML format" is not something I recognize as a defined format and that probably no browser recognizes as a defined format so all it does is render the text nodes in that document. You would need to apply a CSS stylesheet to define how that format is to be rendered.If you expect the browser to render the source of your result XML the same way some browsers do when you load an XML document not associated with any stylesheet, then sorry, I don't know of any browsers doing that as the expectation is that you use XSLT to transform arbitrary XML into some format like XHTML or SVG or MathML your targetted browsers know to handle.So all you can do is define sheet.css with e.g.

Root, Matches, Match, Name { display: block; }Root { margin: 0.2 em; }

and then put

<xsl:template match="/">  <xsl:processing-instruction name="xml-stylesheet">type="text/xsl" href="sheet.css"</xsl:processing-instruction>  <Root>	<xsl:apply-templates/>  </Root></xsl:template>

in your stylesheet to make sure the result document uses that CSS. The CSS sample is just an example, you might want different settings for "display" for some elements and you might want additional definitions of course.

Link to comment
Share on other sites

Hi,Actually i need to know then what is the purpose of declaring xsl:output method="xml" .According to me the browser should be able to display the xml format.If we apply style sheet it will be done, but what is the purpose??there should be some use for declaring output to xml.Is the browser cant render it as an xml??Please correct me if i am wrong.

Link to comment
Share on other sites

First of all XSLT is not restricted to the browser. It is possible to use XSLT without any browser by simply running an XSLT processor from the command line or inside of an XML editor where the transformation output is then usually a file. And the output method defines how any result tree the XSLT processor creates is serialized to a file. For details see http://www.w3.org/TR/xslt#output. Thus if you want to understand how the different output methods work then I suggest you use an XSLT processor outside of any browser, where the transformation result is written to a file, then you write three stylesheet that only differ in the output method and apply them to the same input, then you will see the difference between the three serialization formats respectively output methods.As for your "According to me the browser should be able to display the xml format", I have already pointed out twice that browser can display some XML formats thus for instance if you use a Mozilla browser like Firefox or SeaMonkey you can transform XML to XML where the XML is XHTML or SVG or MathML or a mix of these and Mozilla nicely renders them.You however seem to want to output some arbitrary XML format and you seem to expect a certain way how the browser should display that but you have not even explained how your self-made format is supposed to be rendered. I have also said twice that you can use CSS in the result document to define the rendering of your arbitrary XML.Furthermore I have explained that browsers don't display the source tree of an XML document if is the result of the client-side transformation. You might expect that but I don't know of any browser doing that. To explain that further, there is a great difference between loading some document with e.g.

<root>  <foo>bar</foo></root>

directly in a browser window in Mozilla or IE or to have such a document as the result of a cllient-side XSLT transformation. In the first case Mozilla and IE display the source tree of the XML, in the latter case not. So it might be that you expect the proper display of your result XML is the source tree you would get when you load such a document directly in the browser window but for reasons explained that is not what happens.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...