Gruber
Gruber

Reputation: 551

Insert external file content as HTML through XSLT

i'm a little stucked with some XSLT issue.

I have some simple xml-files and the following stylesheet:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:param name="linked_content"/>

<xsl:template match="/">
    <html>
        <head>
            <title>Chapter summary</title>
        </head>
        <body BGCOLOR="white">
            <xsl:value-of select="$linked_content"></xsl:value-of>
        </body>
    </html>             
</xsl:template>

</xsl:stylesheet>

The linked_content comes from a simple text file (e.g. summary.txt):

<p>
Consider this as a simple summary ^^
</p>

<h3>Part One</h3>

Now my question: How can i insert the HTML-Code from the text file as HTML code into the resulting html file. I know, the code above wont work, since i only get &gt, &lt in the resulting inserted text.

I'm not stuck to submit the content through a parameter. If their's a way to read the textfile from within the stylesheet, that would great!

Anyone an idea?

EDIT: Still stuck here. I tried a workaround reading the text file in java and setting the content as a parameter to the stylesheet. Sadly the

signs are being translated in the process to &lt and &gt ... thus, the html code is screwed. Is there a chance to force the stylesheet not to transform them?

Thx!

Upvotes: 6

Views: 7161

Answers (3)

Michael Kay
Michael Kay

Reputation: 163458

This kind of problem is messy. You can either treat it as an HTML-to-HTML transformation in which the XSLT processor sees the HTML only as text, or as an HTML-to-HTML transformation in which the XSLT processor sees the HTML as XML. In the first case, you're not going to be able to do much transformation of the content; in the second case, you're exposed to the input not being valid. Either way, it's not the pure XML-to-XML use case for which XSLT was designed, so you're going to need extensions.

The first approach - treating it as text - can be achieved using disable-output-escaping, provided (a) your processor supports it, and (b) you use a processing pipeline in which transformation is immediately followed by serialization.

The second approach - treating it as XML - can be achieved using extensions such as saxon:parse-html() which takes HTML input and converts it to a tree representation of some "equivalent" XML.

Upvotes: 0

Jeff
Jeff

Reputation: 887

Are your html files well-formed?

You can try using 'copy-of' in your xsl.

<p><xsl:copy-of select="document('yourHtmlDoc.html')"/></p>

If you need specific items out of your html file, you can even set a path, given that your html is well-formed.

<xsl:copy-of select="document('yourHtmlDoc.html')/tagsNeeded"/>

Upvotes: 3

d-live
d-live

Reputation: 8036

You can use unparsed-text() function which can read external file in. Or if you know that the external file is a valid xml, you can use document() function as well. Both XSLT 2.0 functions however - i think.

Upvotes: 2

Related Questions