XPath find if node exists

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

Na przykład, jeśli musiałem upewnić się, że strona internetowa ma poprawną podstawową strukturę, taką jak / html / body I / html / head / title

Author: Michael Eakins, 2009-04-20

6 answers

<xsl:if test="xpath-expression">...</xsl:if>

Więc na przykład

<xsl:if test="/html/body">body node exists</xsl:if>
<xsl:if test="not(/html/body)">body node missing</xsl:if>
 329
Author: Patrick McDonald,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-04-20 11:33:51

Spróbuj użyć następującego wyrażenia: boolean(path-to-node)

 72
Author: annesley,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-09-06 11:32:10

Patrick jest poprawny, zarówno w użyciu xsl:if, jak i w składni do sprawdzania istnienia węzła. Jednak, jak sugeruje odpowiedź Patryka, nie ma odpowiednika xsl if-then-else, więc jeśli szukasz czegoś bardziej podobnego do if-then-else, zwykle lepiej używać xsl:choose i xsl:otherwise. Tak więc przykładowa składnia Patricka zadziała, ale jest to alternatywa:

<xsl:choose>
 <xsl:when test="/html/body">body node exists</xsl:when>
 <xsl:otherwise>body node missing</xsl:otherwise>
</xsl:choose>
 50
Author: gkrogers,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-01-30 11:27:34

Może być lepiej użyć wyboru, nie trzeba wpisywać (lub ewentualnie wpisywać błędnie) wyrażeń więcej niż raz, i pozwala śledzić dodatkowe różne zachowania.

Bardzo często używam count(/html/body) = 0, ponieważ konkretna liczba węzłów jest bardziej interesująca niż zbiór. Na przykład... gdy nieoczekiwanie jest więcej niż 1 węzeł, który pasuje do twojego wyrażenia.

<xsl:choose>
    <xsl:when test="/html/body">
         <!-- Found the node(s) -->
    </xsl:when>
    <!-- more xsl:when here, if needed -->
    <xsl:otherwise>
         <!-- No node exists -->
    </xsl:otherwise>
</xsl:choose>
 13
Author: davenpcj,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-05-01 17:03:18

Pracuję w Ruby i używając Nokogiri pobieram element i sprawdzam czy wynik jest zerowy.

require 'nokogiri'

url = "http://somthing.com/resource"

resp = Nokogiri::XML(open(url))

first_name = resp.xpath("/movies/actors/actor[1]/first-name")

puts "first-name not found" if first_name.nil?
 4
Author: bcolfer,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-02-16 23:30:33

Odmiana podczas używania xpath w Javie przy użyciu count ():

int numberofbodies = Integer.parseInt((String) xPath.evaluate("count(/html/body)", doc));
if( numberofbodies==0) {
    // body node missing
}
 3
Author: user324898,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-04-25 12:10:39