5
A
回答
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
屬性改造前被調用:
解析XSL樣式表作爲XML文件
評估條件決定哪個樣式表應該被導入。
將
<xsl:import>
聲明的href
屬性的值設置爲動態確定的待導入樣式表的URI。使用剛剛修改的內存中xsl樣式表調用轉換。
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
相關問題
- 1. 如何在富文件中有條件地設置樣式:dataTable
- 2. 條件適用於XSLT 1.0樣式,但不能用於XSLT 2.0樣式表
- 3. 如何有條件地加入表格?
- 4. 如何在Python中有條件地導入?
- 5. 如何在Perl中有條件地導入包?
- 6. XSLT樣式表
- 7. Webpack/ES6:如何導入樣式表
- 8. XSLT樣式表?不知道如何寫?
- 9. 如何使用XSLT樣式錶行
- 10. XSLT樣式表複式
- 11. 有條件地導入框架
- 12. 有條件地填寫導軌形式
- 13. 如何導入/包含在封閉樣式表中?
- 14. 有條件導入
- 15. XSLT有條件地設置屬性值
- 16. 有條件加載樣式表
- 17. 有條件的樣式表軌跡3
- 18. 如何在Angular 2中基於組件有條件添加樣式表?
- 19. 問題將css樣式有條件地應用於twitter-bootstrap表
- 20. 如何在xslt中添加或條件?
- 21. 如何導入AngularJS組件的樣式表?
- 22. Xslt:如何通過類選擇器在xslt樣式表中選擇xml元素?
- 23. XSLT:合併樣式表
- 24. xslt列表標記樣式
- 25. XSLT樣式表切換器
- 26. xslt樣式表確認
- 27. 如何使用XSLT有條件地複製XML元素?
- 28. 在QT樣式表中導入另一個css文件
- 29. 有條件地導入文本文件中的行
- 30. 在ASP.NET MVC3中使用帶有XML的XSLT樣式表
+1有用答案我一直在想的東西!並確保其他人有點好奇 – Treemonkey 2010-12-17 16:07:35
+1好答案。認爲'xsl:import'或'xsl:include'與其他語言的指令如'import'或'#include'不同的是沒有意義的。另外,因爲'xsl:import/@ href'是一個URI,所以這個資源也可以動態構建(幾乎和'@ use-when'一樣) – 2010-12-17 16:51:48