Bonjour,
j'ai un probleme avec XPath lorsque mon DOM chargé a l'aide de Java Xerces a un namespace.
Dans le cas ou je charge un DOM avec le contenu suivant :
et que j'applique un XPath avec //input, la j'arrive a recuprer le node input.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3 <html> <input type="text" /> </html>
Par contre si j'ai un namespace comme ceci :
J'ai vu qu'il y avait l'objet NamespaceContext sur l'instance XPath, mais cet objet marche si on met on a un namespace comme ceci xmlns:html="http://www.w3.org/1999/xhtml" et si la requete xpath est du type //html:input.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3 <html xmlns="http://www.w3.org/1999/xhtml" > <input type="text" /> </html>
J'aimerais bien eviter de faire ca et je me demandais si il etait possible de faire fonctionner Xpath avec un namspace. Merci de votre aide.
Angelo
Voici mon code en Java :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 // 1. Chargement du contenu du fichier HTML dans un DOM /* <html xmlns="http://www.w3.org/1999/xhtml" > <input type="text" /> </html> */ InputStream source = XPathTest.class.getResourceAsStream("form.html"); org.apache.xerces.parsers.DOMParser parser = new org.apache.xerces.parsers.DOMParser(); parser.setFeature("http://xml.org/sax/features/namespaces", true); parser.parse(new InputSource(source)); source.close(); Document document = parser.getDocument(); // 2. Recuperation d'une factory XPath XPathFactory xpathFactory = new org.apache.xpath.jaxp.XPathFactoryImpl(); XPath xpath = xpathFactory.newXPath(); // 3. Compilation de l'expression XPath //input XPathExpression pathExpression = xpath.compile("//input"); // Recuperation d'element input Node node = (Node)pathExpression.evaluate(document, XPathConstants.NODE); // => Ici le node est null si il y a xmlns="http://www.w3.org/1999/xhtml // Le cas marche si je ne mets pas de namespace. /* <html> <input type="text" /> </html> */ System.out.println(node);
Partager