2010-12-17 58 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溶液是動態地改變0​​聲明的href屬性改造前被調用:

  1. 解析XSL樣式表作爲XML文件

  2. 評估條件決定哪個樣式表應該被導入。

  3. <xsl:import>聲明的href屬性的值設置爲動態確定的待導入樣式表的URI。

  4. 使用剛剛修改的內存中xsl樣式表調用轉換。

+1

+1有用答案我一直在想的東西!並確保其他人有點好奇 – Treemonkey 2010-12-17 16:07:35

+1

+1好答案。認爲'xsl:import'或'xsl:include'與其他語言的指令如'import'或'#include'不同的是沒有意義的。另外,因爲'xsl:import/@ href'是一個URI,所以這個資源也可以動態構建(幾乎和'@ use-when'一樣) – 2010-12-17 16:51:48

2

我知道這個帖子很舊,但我想分享我的意見。

每個顯示屏可以使用一個模板而不是兩個。值顯示將隨着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! – 2015-12-23 16:46:31