XSLT: conversion to non-xml content?

Can XSLT be used to transform XML into something other than XML?

eg. I want the final content without xml:

<Content> <image url="file1.png"> <image url="file2.png"> ... <image url="filen.png"> <EndContent> 

You noticed that this document is not xml (or even html), but it has <elements> .

Is it possible using XSLT to generate non-xml output?


Another example of non-xml output might be:

 <HTML> <BODY> <IMG src="file1.png"><BR> <IMG src="file2.png"><BR> ... <IMG src="filen.png"><BR> </BODY> </HTML> 

You will notice that this document is HTML , because it is forbidden to have an end tag in the HTML IMG and BR tags. This is consistent with xhtml , a reformulation of HTML using xml, where all elements are required from a closing tag (because in xml each tag must be closed).


Another example of non-xml output might be:

 INSERT INTO Documents (Filename) VALUES ('file1.png') INSERT INTO Documents (Filename) VALUES ('file2.png') ... INSERT INTO Documents (Filename) VALUES ('file3.png') 

I can compile any source XML, but there can be one example:

Xml source:

 <DocumentStore> <Document type="image">file1.png</Document> <Document type="image">file2.png</Document> <Document type="image">filen.png</Document> </DocumentStore> 

Or perhaps:

 <Profiles> <User avatar="file1.png" /> <User avatar="file2.png" /> <User avatar="filen.png" /> </Profiles> 
+6
html xslt
source share
3 answers

You can use <xsl:output> to specify the output format, which should not be xml, see this man page .

However, if you output html, no modern browser should complain, even if you add closing tags, therefore, using your example above, I believe that all browsers should be fine: -

 <HTML> <BODY> <IMG src="file1.png"></IMG><BR></BR> <IMG src="file2.png"></IMG><BR></BR> ... <IMG src="filen.png"></IMG><BR></BR> </BODY> </HTML> 

Therefore, Iโ€™m not too sure why you donโ€™t want to put a closing tag if I donโ€™t miss something.


Update: Added sample output without xml

Given this style sheet: -

 <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text" /> <xsl:template match="/filenames"> <xsl:for-each select="filename"> INSERT INTO Documents (Filename) VALUES ('<xsl:value-of select="." />') </xsl:for-each> </xsl:template> </xsl:stylesheet> 

and this xml input: -

 <?xml version="1.0" encoding="UTF-8"?> <filenames> <filename>file1.png</filename> <filename>file2.png</filename> <filename>file3.png</filename> </filenames> 

You get the output as follows: -

 INSERT INTO Documents (Filename) VALUES ('file1.png') INSERT INTO Documents (Filename) VALUES ('file2.png') INSERT INTO Documents (Filename) VALUES ('file3.png') 
+8
source share

No matter how you create your IMG tags,

 <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html"/> <xsl:template match="/"> <HTML> <BODY> <xsl:element name="IMG"> <xsl:attribute name="src">file1.png</xsl:attribute> </xsl:element> <IMG src="file2.png"></IMG> <IMG src="filen.png"/> </BODY> </HTML> </xsl:template> </xsl:stylesheet> 

The html output method will cause the IMG tags to not be closed.

 <HTML> <BODY><IMG src="file1.png"><IMG src="file2.png"><IMG src="filen.png"></BODY> </HTML> 
+4
source share

Yes, you can using the xsl: output element in your stylesheet.

+1
source share

All Articles