2010-11-21 141 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