如何在使用XSLT中包含文件的所有内容?

时间:2012-06-22 12:15:18

标签: xslt

我想要一个包含一些javascript函数的单独文件。当XSLT处理我的应用程序时,我希望它将此文件的所有内容输出到HTML。我不想引用一个库,而是在我的html中包含所有函数。

我知道<xsl:include>但我不能在内部添加任何内容或<body>代码。

这可能吗?

2 个答案:

答案 0 :(得分:3)

假设您要包含的文件(例如scripts.xml)是XML,例如有像

这样的内容
<script type="text/javascript">
function foo() { ... }
function bar() { ... }
...
</script>

然后在XSLT中你可以简单地使用

<xsl:template match="/">
  <html>
    <body>
      <xsl:copy-of select="document('scripts.xml')/script"/>
    </body>
  </html>
</xsl:template>

如果这没有用,那么你需要更详细地解释你有哪种文件,你使用的是哪个XSLT版本(XSLT 2.0也可以读取非XML纯文本文件,如Javascript代码)。

[编辑] 这是一个使用未解析文本的XSLT 2.0示例(需要像Saxon或AltovaXML这样的XSLT 2.0处理器):

<xsl:template match="/">
  <html>
    <body>
      <script type="text/javascript">
         <xsl:value-of select="unparsed-text('file.js')"/>
      </script>

    </body>
  </html>
</xsl:template>

答案 1 :(得分:1)

使用unparsed-text()功能阅读文本文件的内容。

<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/">
     <html>
      <body>
        <xsl:sequence select="unparsed-text('YourFile.js')"/>
      </body>
     </html>
 </xsl:template>
</xsl:stylesheet>