如何在XSL中查明属性是否存在

Jaw*_*wed 23 xml xslt

如何在XSL中查明属性是否存在.

Dim*_*hev 27

只需使用:

<xsl:template match="someElement/@someAttrName">
  <!-- Whatever specific work when someElement has @someAttrName -->
</xsl:template>

<xsl:template match="someElement[not(@someAttrName)]">
  <!-- Whatever specific work when someElement has no @someAttrName -->
</xsl:template>
Run Code Online (Sandbox Code Playgroud)

别注:在一个精心编写的XSLT代码的条件指令数(如<xsl:choose>,<xsl:when>,<xsl:otherwise>,<xsl:if>,...等)接近于零.在这个解决方案中它 0.


Ars*_*yan 19

<xsl:choose>
   <xsl:when test="element/@attribute">
     do one thing
   </xsl:when>
   <xsl:otherwise>
     do something else
   </xsl:otherwise>
</xsl:choose>
Run Code Online (Sandbox Code Playgroud)

  • 正确,假设上下文节点位于相关元素的父级.(我倾向于怀疑如果海报在这方面有困难,那么他们还没有掌握上下文在XSLT中的工作方式.) (2认同)