2010-11-21 89 views
1

在.xml文件,我有這樣的:問題XSLT輸出

<function>true</function> 

在架構ILE,我已經將它定義爲一個布爾值。所以現在,它工作正常。但對於XSLT文件即的.xsl,

+0

你可以添加一個示例xsl來描述你想要做什麼嗎? – wimh 2010-11-21 10:59:10

+0

plz檢查更新的部分 – user507087 2010-11-21 11:04:32

+0

好問題,+1。查看我的答案,獲取不使用任何XSLT條件指令的完整簡短解決方案。還等一個更短的黑客,來... :) – 2010-11-21 17:31:19

回答

3

您可以使用xsl:choose

<td> 
    <xsl:choose> 
    <xsl:when test="function = 'true'">@</xsl:when> 
    <xsl:otherwise>&#32;</xsl:otherwise> 
    </xsl:choose> 
</td> 
+0

它不工作...&32是什麼?/它是拋出一個錯誤? – user507087 2010-11-21 12:40:05

+0

@ user507087 - 抱歉,它應該是空間的實體(' ')。我錯過了#。 – Oded 2010-11-21 12:42:27

+0

該空間實體將從樣式表中移除。 – 2010-11-22 23:40:21

0

這可以非常簡單地完成,而不是在所有需要的條件XSLT指令,並完全在XSLT的精神(推式):

這種轉變:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<xsl:template match="node()|@*"> 
    <xsl:copy> 
     <xsl:apply-templates select="node()|@*"/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="function/text()[.='true']">@</xsl:template> 
<xsl:template match="function/text()[not(.='true')]"> 
    <xsl:text> </xsl:text> 
</xsl:template> 

<xsl:template match="function"> 
    <td><xsl:apply-templates/></td> 
</xsl:template> 
</xsl:stylesheet> 

當在下面的XML文檔施加:

<function>true</function> 

產生想要的,正確的結果

<td>@</td> 

當在下面的XML文檔被施加相同的變換:

<function>false</function> 

再次正確的,希望的結果是產生

<td> </td> 

最後,使用黑客(在XSLT 2.0/XPath 2.0中這不是必要的),我們可以只使用一個模板:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<xsl:template match="node()|@*"> 
    <xsl:copy> 
     <xsl:apply-templates select="node()|@*"/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="function"> 
    <td> 
    <xsl:value-of select= 
    "concat(substring('@', 1 div (.='true')), 
      substring(' ', 1 div not(.='true')) 
     ) 
    "/> 
    </td> 
</xsl:template> 
</xsl:stylesheet> 
+0

我認爲使用條件比不必寫兩次條件要好。 – svick 2010-11-21 17:42:38

+0

@svick:你認爲更好的選擇有更低(可能接近於零)composabitiy。此外,它具有更大的複雜性,並且更容易出錯。 – 2010-11-21 18:00:39