problem with xslt output
In the .xml file, I have like this:
<function>true</function>
In the schema ile, I have 开发者_如何学Cdefined it as a boolean. So now, It is working correctly. BUT for XSLT file i.e .xsl,
You can use xsl:choose
:
<td>
<xsl:choose>
<xsl:when test="function = 'true'">@</xsl:when>
<xsl:otherwise> </xsl:otherwise>
</xsl:choose>
</td>
This can be done very simply, not at all requiring conditional XSLT instructions, and completely in the spirit of the XSLT (push-style):
This transformation:
<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>
when applied on the following XML document:
<function>true</function>
produces the wanted, correct result:
<td>@</td>
When the same transformation is applied on the following XML document:
<function>false</function>
again the correct, wanted result is produced:
<td> </td>
Finally, using a hack (in XSLT 2.0/XPath 2.0 this isn't necessary), we can use just a single template:
<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>
精彩评论