4

いくつかのjavascript関数を保持する別のファイルが必要です。XSLTがアプリケーションを処理するときに、このファイルのすべてのコンテンツをHTMLに出力するようにします。ライブラリを参照したくありませんが、代わりにhtml内にすべての関数があります。

私は知って<xsl:include>いますが、内部や<body>タグに何も含めることはできません。

これは可能ですか?

4

2 に答える 2

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 は、Javascript コードなどの非 XML プレーン テキスト ファイルも読み取ることができます)。

[編集] unparsed-text を使用した 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>
于 2012-06-22T12:28:10.650 に答える
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>
于 2012-06-22T12:38:37.743 に答える