xslt输出的问题

时间:2010-11-21 09:04:16

标签: xml xslt

在.xml文件中,我喜欢这个:

<function>true</function>

在模式ile中,我将其定义为布尔值。所以现在,它正常工作。但是对于XSLT文件,即.xsl,

2 个答案:

答案 0 :(得分:3)

您可以使用xsl:choose

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

答案 1 :(得分: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>

最后,使用hack(在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>