我正在进行一些xslt转换,但我刚刚发现,根据外部因素,当前父级和子级之间可能有一个额外的节点,也可能没有。因此,现在我必须更改我的xslt代码才能处理以下两种情况:
方案1:
<parent>
<child/>
<child/>
<parent>
方案2:
<parent>
<nuisance>
<child/>
<child/>
</nuisance>
<parent>
在某些情况下,我
test="parent/child"
或使用此格式访问父级/节点。我需要类似
test="parent/magic(* or none)/child"
的东西我知道可以解决此问题的唯一方法是使用:
<xsl:choose>
<xsl:when test="parent/child">
<!-- select="parent/child"-->
</xsl:when>
<xsl:otherwise>
<!-- select="parent/*/child"-->
</xsl:otherwise>
</xsl:choose>
但这将使我的代码的大小增加三倍,并且将需要大量的体力劳动...
帮助非常感谢!
最佳答案
为什么不简单地选择两者的并集呢?
<xsl:apply-templates select="parent/child|parent/*/child"/>
在两种情况下都将选择正确的节点。
关于xslt - XSLT:测试节点是否存在,无论它是当前节点的子代还是子代,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5968809/