tags:

views:

83

answers:

3

Is it possible to use XSLT to transform XML into something other than XML?

e.g. i want the final non-xml content:

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

You're notice this document is not xml (or even html), but it does have <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'll notice this document is HTML, because in HTML IMG and BR tags are forbidden from having a closing tag. This constrasts with xhtml, the reformulation of HTML using xml, where all elements are required from having a closing tag (because in xml every 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 make up any source xml i like, but one example might be:

Source xml:

<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>
+1  A: 

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

GSerg
+4  A: 

You can use <xsl:output> to specify the output format, which doesn't have to be xml, see this reference page.

However, if you are outputting html, no modern browser should complain even if you do put the closing tags, so using your example above, i believe all browser should be ok with :-

<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>

So not too sure why you don't want to put the closing tag, unless i'm missing something.


Update: Added example of non xml output

Given this stylesheet:-

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
    <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 input xml:-

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

You get output like this:-

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

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

INSERT INTO Documents (Filename) VALUES ('file3.png')
Alan Chan
@Alan Chan: It's not so much generating invalid HTML that browsers might be able to deal with; it's generating valid HTML so that parsers can handle it.
Ian Boyd
@Ian: ok i see. Well, if you use <xsl:output method="html"> to specify that the output is html, and then use <xsl:element> to create the html tags ... e.g. <xsl:element name="IMG"><xsl:attribute name="src">file1.png</xsl:attribute></xsl:element> then the xslt processor should produce the <IMG>, <BR>, etc., tags without the closing tag as required for html.
Alan Chan
+4  A: 

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"&gt;
<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 output method "html" 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>
Darrel Miller
When i reproduce this, the closing tags are included. At the same time, i sometimes might want to include optional closing tags, and sometimes i might not want to. XSLT needs to be able to transform xml into my **desired** output format.
Ian Boyd