通过 XSLT 将外部文件内容插入为 HTML
我对一些 XSLT 问题有点困惑。
我有一些简单的 xml 文件和以下样式表:
<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>
linked_content 来自一个简单的文本文件(例如summary.txt):
<p>
Consider this as a simple summary ^^
</p>
<h3>Part One</h3>
现在我的问题:如何从文本文件插入 HTML 代码作为 HTML 代码放入生成的 html 文件中。我知道,上面的代码不起作用,因为我只在结果插入的文本中得到 >, < 。
我不再坚持通过参数提交内容。如果它们是一种从样式表中读取文本文件的方法,那就太好了!
有人有想法吗?
编辑: 还卡在这里。我尝试了一种解决方法,在 java 中读取文本文件并将内容设置为样式表的参数。可悲的是
- <
- >
符号在此过程中被转换为 < 和 > ...因此,html 代码被搞砸了。是否有机会强制样式表不转换它们?
谢谢!
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 >, < 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 < and > ... thus, the html code is screwed. Is there a chance to force the stylesheet not to transform them?
Thx!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你的 html 文件格式正确吗?
您可以尝试在 xsl 中使用“copy-of”。
如果您需要 html 文件中的特定项目,您甚至可以设置一个路径,前提是您的 html 格式良好。
Are your html files well-formed?
You can try using 'copy-of' in your xsl.
If you need specific items out of your html file, you can even set a path, given that your html is well-formed.
您可以使用 unparsed-text() 函数来读取外部文件。
或者,如果您知道外部文件是有效的 xml,您也可以使用 document() 函数。
然而,我认为这两个 XSLT 2.0 都可以工作。
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.
这种问题很混乱。您可以将其视为 HTML 到 HTML 转换(其中 XSLT 处理器仅将 HTML 视为文本),或者将其视为 HTML 到 HTML 转换(其中 XSLT 处理器将 HTML 视为 XML)。在第一种情况下,您将无法对内容进行太多转换;在第二种情况下,您会遇到输入无效的情况。无论哪种方式,它都不是 XSLT 设计的纯粹 XML 到 XML 用例,因此您将需要扩展。
第一种方法 - 将其视为文本 - 可以使用禁用输出转义来实现,前提是(a)您的处理器支持它,并且(b)您使用一个处理管道,其中转换后立即进行序列化。
第二种方法 - 将其视为 XML - 可以使用诸如 saxon:parse-html() 之类的扩展来实现,它接受 HTML 输入并将其转换为某些“等效”XML 的树表示形式。
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.