我想得到第一个非空值为2并将其置于文本输入的"值"属性.所以,我这样做:
<input type="text">
<xsl:attribute name="value">
<xsl:choose>
<xsl:when test="some/@attr != ''">
<xsl:value-of select="some/@attr" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="some/another/@attr" /> <!-- always non-empty, so get it -->
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
</input>
Run Code Online (Sandbox Code Playgroud)
问题是:有没有办法用较少的代码行来实现它?..也许,就像那样:<input type="text" value="some/@attr or some/another/@attr" />或者其他什么东西?比如说Perl:my $val = 0 || 5;
在此先感谢您的帮助
UPD XSLT 1.0
如果你使用
<xsl:attribute name="value">
<xsl:value-of select="some/@attr[. != ''] | some/another/@attr"/>
</xsl:attribute>
Run Code Online (Sandbox Code Playgroud)
然后使用XSLT 1.0 value-of语义some/@attr[. != ''] | some/another/@attr输出所选第一个节点的字符串值.因此,如果some/@attr[. != '']选择一个节点它应该输出,否则some/another/@attr(我认为在文档顺序中some/@attr被认为是先前的some/child-element/@attr).