2017-07-12 27 views
0

私は、Webページを生成するために、いくつかのXMLファイルでXSLTを使用しています。 <widget name="example" />ウィジェットタグを自分のXMLに追加して、htmlの対応する場所にウィジェットを挿入したいと考えています。このウィジェットは、HTMLファイルやその他の情報を定義するXMLファイルによっても定義されています。ドキュメントからすべてのスクリプトタグをXSLTのヘッドタグに移動

これはかなりよくなりますが、今のウィジェットは、一の以上のスクリプトを必要とする、私は代わりに、すべてのスクリプトタグを散乱、HTMLのheadタグにこのスクリプトを追加したい

<scripts> 
    <script>jquery<script> 
    <script>animation</script> 
</scripts> 

のようにそれが定義されています場所の上に。これは彼らが scriptsタグを残し

<xsl:template match="/"> 
    <xsl:variable name="document"> 
     <html> 
      <head> 
       <title><xsl:value-of select="page/title" /></title> 
       <xsl:apply-templates select="/page/scripts" mode="content" /> 
       <xsl:apply-templates select="/page/sheets" mode="content" /> 
      </head> 
      <body> 
       <div class="header"> 
        <h1><xsl:value-of select="page/title" /></h1> 
       </div> 
       <xsl:apply-templates select="$menu/menu" mode="menu" /> 
       <div class="content"> 
        <xsl:apply-templates select="page/content/*" mode="content" /> 
       </div> 
      </body> 
     </html> 
    </xsl:variable> 
    <xsl:apply-templates select="exsl:node-set($document)" mode="collect-resources" /> 
</xsl:template> 

:のように私は、私のページを作成し、変数にそれを置く:私はこのために使う現在のアプローチは、次のようです。 、ヘッダーにスクリプトを置くために、すべてのスクリプトタグを収集するための1時間:文書全体を2回横断

<xsl:template match="head" mode="collect-resources"> 
    <xsl:for-each select="..//scripts"> 
     <xsl:apply-templates select="current()/*" mode="scripts" /> <!-- Convert the scripts defined in this scripts tag into proper HTML script tags. --> 
    </xsl:for-each> 
</xsl:template> 

<xsl:template match="scripts" mode="collect-resources" /> <!-- Remove all scripts tags. --> 

<xsl:template match="node()" mode="collect-resources"> 
    <xsl:copy> 
     <xsl:copy-of select="@*"/> 
     <xsl:apply-templates select="./node()" mode="collect-resources"/> 
    </xsl:copy> 
</xsl:template> 

:この後、私は次のテンプレートにつながる

<xsl:apply-templates select="exsl:node-set($document)" mode="collect-resources" /> 

を呼び出しますすべてのscriptsタグを削除する2回目です。

これは、大きなドキュメントでは非常に遅くなり、少し汚れています。これを行う方法はありますか?

付録I -

<widget> 
    <scripts> 
     <script>/widgets/example/main</script> 
    </scripts> 
    <content> 
     <div class="example-widget" /> 
    </content> 
</widget> 

widget.xml付録II - template.xsl

<xsl:template match="widget" mode="content"> 
    <xsl:variable name="path">../widgets/<xsl:value-of select="@name" />/widget.xml</xsl:variable> <!-- The path to the widget xml --> 
    <xsl:variable name="widget" select="document($path)"/> 
    <xsl:element name="div"> 
     <xsl:attribute name="class">widget</xsl:attribute> 
     <xsl:apply-templates select="$widget/widget/scripts" mode="content" /> <!-- Copy the widget script information. --> 
     <xsl:apply-templates select="$widget/widget/content/*" mode="content" /> <!-- Copy the widget contents. --> 
    </xsl:element> 
</xsl:template> 

<xsl:template match="node()" mode="content"> <!-- Copy anything that is not a widget. --> 
    <xsl:copy> 
     <xsl:copy-of select="@*" /> 
     <xsl:apply-templates select="./node()" mode="content" /> 
    </xsl:copy> 
</xsl:template> 
+1

あなたが処理する 'widget'要素はありますが、コードには表示されないので、初めて実際にそれらの要素をどこで処理するのか、' script'要素がどこに終わるのか分かりにくいです。 –

+0

これは実装の詳細に関するものではありません。私は完全なコードベースであなたを溺れさせるかもしれませんが、それはその質問にはあまり関係ありません。ウィジェットHTMLの残りの部分で 'script'要素が' body'要素の中でHTMLのいくつかのレベルまで深くなっているとあなたは私を信じることができます。 –

+0

しかし、関連するウィジェットコードを質問に追加します。 –

答えて

1

あなたの

 <head> 
      <title><xsl:value-of select="page/title" /></title> 
      <xsl:apply-templates select="/page/scripts" mode="content" /> 
      <xsl:apply-templates select="/page/sheets" mode="content" /> 
     </head> 

の内側にあなたはあなたがどんなwidgetを処理したいようですsとそれらのリンクされたスクリプトを直接

 <head> 
      <title><xsl:value-of select="page/title" /></title> 
      <xsl:apply-templates select="/page/scripts" mode="content" /> 
      <xsl:apply-templates select="//widget" mode="script"/> 
      <xsl:apply-templates select="/page/sheets" mode="content" /> 
     </head> 

<xsl:template match="widget" mode="script"> 
    <xsl:variable name="path">../widgets/<xsl:value-of select="@name" />/widget.xml</xsl:variable> <!-- The path to the widget xml --> 
    <xsl:variable name="widget" select="document($path)"/> 
    <xsl:apply-templates select="$widget/widget/scripts" mode="scripts" /> 
</xsl:template> 

や他のテンプレートにあなたがしないで出力するスクリプトは

<xsl:template match="widget" mode="content"> 
    <xsl:variable name="path">../widgets/<xsl:value-of select="@name" />/widget.xml</xsl:variable> <!-- The path to the widget xml --> 
    <xsl:variable name="widget" select="document($path)"/> 
    <xsl:element name="div"> 
     <xsl:attribute name="class">widget</xsl:attribute> 
     <xsl:apply-templates select="$widget/widget/content/*" mode="content" /> <!-- Copy the widget contents. --> 
    </xsl:element> 
</xsl:template> 

あなたはそれがより良い実行するかどうかを測定する必要がありますでしょう。

+0

ありがとう!私が望むようにきちんとしているわけではありませんが、ずっと良くなりました。どうもありがとうございました! –

関連する問題