xml 如何在 XSL 中找出属性是否存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4918837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 13:34:08  来源:igfitidea点击:

how to find out if an attribute exists or not in XSL

xmlxslt

提问by Jawed

how to find out if an attribute exists or not in XSL.

如何找出 XSL 中是否存在属性。

回答by Dimitre Novatchev

Just use:

只需使用

<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>

Do note: In a well-written XSLT code the number of conditional instructions (such as <xsl:choose>, <xsl:when>, <xsl:otherwise>, <xsl:if>, ... etc.) is close to zero. In this solution it is0.

请注意:在编写良好的 XSLT 代码中,条件指令(例如<xsl:choose>, <xsl:when>, <xsl:otherwise>, <xsl:if>, ... 等)的数量接近于零。在这个解决方案中它0。

回答by Arsen Mkrtchyan

<xsl:choose>
   <xsl:when test="element/@attribute">
     do one thing
   </xsl:when>
   <xsl:otherwise>
     do something else
   </xsl:otherwise>
</xsl:choose>

回答by Oleg Motorin

<xsl:value-of select="element[not(@attribute)]"/>

if need select some element without attribute

如果需要选择一些没有属性的元素