开发者

XSLT: Transforming into non-xml content?

开发者 https://www.devze.com 2022-12-29 07:31 出处:网络
Is it possible to use XSLT to transform XML into something other than XML? e.g. i want the final non-xml content:

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'll notice this document is not xml (or even html), but it does have what we would call <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 contrasts with xhtml (the reformulation of HTML using xml) where all elements are required to have 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>


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">
    <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')


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号