xpath find if node exists

EddyR picture EddyR · Apr 20, 2009 · Viewed 333k times · Source

Using a xpath query how do you find if a node (tag) exists at all?

For example if I needed to make sure a website page has the correct basic structure like /html/body and /html/head/title

Answer

Patrick McDonald picture Patrick McDonald · Apr 20, 2009
<xsl:if test="xpath-expression">...</xsl:if>

so for example

<xsl:if test="/html/body">body node exists</xsl:if>
<xsl:if test="not(/html/body)">body node missing</xsl:if>