XSLT节点可用性检查
我想检查变量是否有任何节点或属性XSLT节点可用性检查,xslt,Xslt,我想检查变量是否有任何节点或属性 XSL: <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="/"> <xsl:variable name="testvar"> <test><name first="Isaac" last="Sivakumar" middle="G
XSL:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="/">
<xsl:variable name="testvar">
<test><name first="Isaac" last="Sivakumar" middle="G"></name></test>
</xsl:variable>
<xsl:choose>
<xsl:when test="normalize-space($testvar)">
<xsl:value-of select="$testvar"></xsl:value-of>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="'NO XML DATA AVAILABLE'"></xsl:value-of>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
XSL:
当我试着运行上面的代码时,我得到的结果是“没有XML数据可用”。我需要检查变量是否有任何节点/任何属性,而不管它是否有数据
你能帮我修一下吗 在XSLT 1.0中,变量的值类型为“result tree fragment”,首先需要使用扩展函数将其转换为节点集,以便能够寻址节点,例如
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common" version="1.0">
<xsl:template match="/">
<xsl:variable name="testvar">
<test><name first="Isaac" last="Sivakumar" middle="G"></name></test>
</xsl:variable>
<xsl:choose>
<xsl:when test="exsl:node-set($testvar)/node()">
<xsl:copy-of select="$testvar"></xsl:value-of>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="'NO XML DATA AVAILABLE'"></xsl:value-of>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
给定XSLT 2.0,我只需检查($testvar instance of xs:string和$testvar='')是否为“无XML数据可用”或$testvar传递空变量时出错。错误描述:XPTY0019:“/”的第一个操作数的必需项类型为node();提供的值的项类型为xs:string,该错误消息引用了错误代码,并且类型
xs:string
听起来非常像是使用XSLT 2.0处理器,因此我不明白为什么将问题标记为XSLT-1.0。请告诉我们您使用的是哪种XSLT处理器,并告诉我们在出现错误时如何定义变量。看起来我忽略了XSLT1.0和2.0中的
都会导致一个空字符串,因此在这种情况下,测试更加复杂。然而,在XSLT2.0中,我们现在可以很容易地检查if($testvar-instance of xs:string),然后。。。否则…
。谢谢:)我可以处理对象类型。谢谢:)
<xsl:choose>
<xsl:when test="exsl:object-type($testvar) = 'string' and $testvar = ''">
<xsl:value-of select="'NO XML DATA AVAILABLE'"/>
</xsl:when>
<xsl:when test="exsl:object-type($testvar) = 'node-set'">
<xsl:copy-of select="$testvar"/>
</xsl:when>
</xsl:choose>