2010-12-17 13 views

答えて

14

こんにちはすべて、いくつかの 条件を確認した後、 スタイルシートをインポートする方法はありますか?

同様に、変数$ a = "1"の値が の場合、1.xslをインポートするか、または 2.xslをインポートします。

いいえ、<xsl:import>ディレクティブは、コンパイル時があります。

XSLT 2.0では、制限付き条件付きコンパイルのためにuse-when属性を使用できます。

は、例えばの場合: - 特に定義された有効範囲内の変数が存在しないことを意味する

<xsl:import href="module-A.xsl" 
    use-when="system-property('xsl:vendor')='vendor-A'"/> 

use-when属性の制限は、属性が評価されたときに動的なコンテキストが存在しないことです。

非XSLTソリューションは、変換が呼び出される前に、動的に<xsl:import>宣言のhref属性を変更することです:

  1. は、XSLスタイルシートを解析し、XMLファイルとして

  2. を評価しますインポートするスタイルシートを決定する条件。

  3. <xsl:import>宣言のhref属性の値を、動的に決定されたインポートするスタイルシートのURIに設定します。

  4. 変更されたばかりのメモリ内のxslスタイルシートを使用して変換を呼び出します。

+1

+1便利な答え私が考えてきたもの!といくつかの点で好奇心で他の人を確認する – Treemonkey

+1

+1良い答え。 'xsl:import'や' xsl:include'が 'import'や' #include'のような他の言語の命令とは違っていると考えるのは意味がありません。また、 'xsl:import/@ href'がURIであるため、リソースも動的に構築できます(' @ use-when'とほぼ同じ制限)。 –

2

私はこの投稿が古いと知っていますが、私は意見を分かちたいと思います。

各ディスプレイは、2つではなく1つのテンプレートを使用できます。値の表示は、VBアプリケーションで変更されます。

breakfast_menu.xml:このファイルで

<?xml version="1.0" encoding="UTF-8"?> 
<?xml-stylesheet type="text/xsl" href="conditionDisplay.xsl" ?> 
<data> 
    <breakfast_menu> 
     <food> 
      <name>Belgian Waffles</name> 
      <price>$5.95</price> 
      <description>Two of our famous Belgian Waffles with plenty of real maple syrup</description> 
      <calories>650</calories> 
     </food> 

     <food> 
      <name>Strawberry Belgian Waffles</name> 
      <price>$7.95</price> 
      <description>Light Belgian waffles covered with strawberries and whipped cream</description> 
      <calories>900</calories> 
     </food> 


     <food> 
      <name>Homestyle Breakfast</name> 
      <price>$6.95</price> 
      <description>Two eggs, bacon or sausage, toast, and our ever-popular hash browns</description> 
      <calories>950</calories> 
     </food> 
    </breakfast_menu> 
    <display>1</display> 
</data> 

は、私は私のディスプレイを輸入し、条件に私は私が必要なものをテンプレートに伝えます。

conditionDisplay.xsl

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" > 
    <xsl:import href="display1.xsl"/> 
    <xsl:import href="display2.xsl"/> 
    <xsl:template match="/"> 
     <xsl:variable name="display"><xsl:value-of select= "data/display"/></xsl:variable> 
     <xsl:choose> 
      <xsl:when test="$display='1'"> 
       <xsl:call-template name="display1" /> 
      </xsl:when> 
      <xsl:otherwise> 
       <xsl:call-template name="display2 /> 
      </xsl:otherwise> 
     </xsl:choose> 
    </xsl:template> 
</xsl:stylesheet> 

display1.xsl

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template name="display1"> 
     <html xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
      <body style="font-family:Arial;font-size:12pt;background-color:#EEEEEE"> 
       <xsl:for-each select="data/breakfast_menu/food"> 
        <div style="background-color:teal;color:white;padding:4px"> 
         <span style="font-weight:bold"><xsl:value-of select="name"/> - </span> 
         <xsl:value-of select="price"/> 
        </div> 
        <div style="margin-left:20px;margin-bottom:1em;font-size:10pt"> 
         <p> 
          <xsl:value-of select="description"/> 
          <span style="font-style:italic"> (<xsl:value-of select="calories"/> calories per serving)</span> 
         </p> 
        </div> 
       </xsl:for-each> 
      </body> 
     </html> 
    </xsl:template> 
</xsl:stylesheet> 

display2.xsl

<?xml version="1.0" encoding="UTF-8"?>futur 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template name="display2"> 
     <html xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
      <body style="font-family:Arial;font-size:12pt;background-color:#222222"> 
       <xsl:for-each select="data/breakfast_menu/food"> 
        <div style="background-color:teal;color:white;padding:4px"> 
         <span style="font-weight:bold"><xsl:value-of select="name"/> - </span> 
         <xsl:value-of select="price"/> 
        </div> 
        <div style="margin-left:20px;margin-bottom:1em;font-size:10pt"> 
         <p> 
          <xsl:value-of select="description"/> 
          <span style="font-style:italic"> (<xsl:value-of select="calories"/> calories per serving)</span> 
         </p> 
        </div> 
       </xsl:for-each> 
      </body> 
     </html> 
    </xsl:template> 
</xsl:stylesheet> 

私は本当に私のひどい英語をお詫び申し上げます。それは次の投稿には良いでしょう。私はそれが最良の解決策ではないと思うので、誰かを助けることを願っています。

+0

私はモードと同様のことをしました。良い最初の答え。 +1ようこそstackoverflowへ! –

関連する問題