Xmllint
De Linuxmemo.
Analyseur de fichiers XML
Sommaire |
Valider la syntaxe
xmllint --valid file.xml
Correction de syntaxe
xmllint --recover file.xml > filesanserreur.xml
Auto indente
xmllint --format file.xml
Mode interactif
xmllint --shell file.xml help
Xpath
xmllint --xpath "XPATH_EXPRESSION" file.xml
Language Xpath
Voir XPath
Différents types de requêtes XPath :
Xpath absolu :
Suit un chemin fixe du nœud racine à un élément (Précis mais fragile, car toute modification du DOM peut le détruire).
/ /html/body/div/div/div/main/div/div/div/select
Xpath relatif :
Recherche dynamique des éléments n'importe où dans le DOM (Plus flexible et stable pour les tests automatisés).
// //div[@class='sort']/select
Interroger un élément pour le localisateur XPath=
Axes :
Il existe 13 axes différents pour interroger un élément. Les axes permettent de localiser les éléments en fonction de leurs propres attributs, des éléments proches ou des relations hiérarchiques au sein du DOM.
ancestor ancestor is used when we want to check all the parent nodes from the context node up to root.
//select/ancestor::div
ancestor-or-self ancestor-or-self is used when we want to select all the parent nodes upto root including the context node
//select/ancestor-or-self::div
attribute attribute is used when we want to query for an element using its attributes like id, class, value etc..
//div[@class='sort']/select
child child is used when we want to query for child element with parent reference
//div[@class='sort']/child::select
descendant descendant is used when we want to query for all the child element and its children of a context node
//div[@class='sort']/descendant::*
descendant-or-self descendant-or-self is used when we to query for all the child elements and its children including context node
//div[@class='sort']/descendant-or-self::*
following following is used when we want to query for all the elements after the current context node(except the context node’s descendant)
//div[@class='sort']/following::*
following-sibling following-sibling is used when we want to query for the sibling element which belongs to same parent as context node
//div[@class='sort']/following-sibling::*
parent parent is used when we want to query for the immediate parent of the current context node. This uses two dots “..”
//div[@class='sort']/..
preceding preceding is used when we want to query for elements that are preceding to the current context node
//div[@class='sort']/preceding::*
preceding-sibling preceding-sibling is used when we want to query for the sibling element preceding to the current context node
//div[@class='sort']/preceding-sibling::*
self self is used when we first query for a set of elements and then refine further for the current context node. This axis uses a single dot “.”
//div[@class='sort']/.
Fonctions :
Les fonctions XPath permettent de vérifier l'existence des éléments et de les localiser en fonction de leurs attributs, de leur position ou de leur contenu textuel, améliorant ainsi la sélection dynamique des éléments.
boolean() This function accepts locator as parameter and it return true if the passed locator is found else returns false
boolean(//div[@class='sort']/select)
count() This function accepts locator as parameter and returns the count of elements found
count(//div[@class='sort']/select/*)
id() This function accepts id attribute of the element and returns the element if found
id('__next')
position() This function is used when we need to identify an element based on its position
//select/option[position()=2]