Xpath XPath is a language for finding information in an XML document

Preview:

Citation preview

XpathXpath

XPath is a language for finding information in an XML document.

Naviguer dans les documents XML avec XPathNaviguer dans les documents XML avec XPath

• Introduction• Les axes• Les filtres• Les prédicats• Les fonctions prédéfinies• Syntaxe abrégée

Naviguer dans les documents XML avec XPathNaviguer dans les documents XML avec XPath

• Introduction• Les axes• Les filtres• Les prédicats• Les fonctions prédéfinies• Syntaxe abrégée

IntroductionIntroduction

Chaque document XML est une instance d'un arbre XML. XPath est un langage général d'adressage dans cet arbre XML.

RemarquesRemarques

Le nœud racine est unique et obligatoire

Il ne faut pas le confondre avec l'élément document, souvent appelé racine du document.

Cette distinction est justifiée par le fait que quatre types de nœuds différents peuvent être fils du nœud racine.

• le nœud déclaration de domaine

• les instructions de traitement

• les commentaires

• l'élément document qui est unique

Si les nœuds sont ordonnés, les attributs ne le sont pas, ainsi nous ne pourrons pas recherche le nième attributs.

LocationPathLocationPath

http://www.w3.org/TR/xpath#NT-LocationPath

[1] LocationPath ::= RelativeLocationPath| AbsoluteLocationPath

[2] AbsoluteLocationPath ::= '/' RelativeLocationPath?| AbbreviatedAbsoluteLocationPath

[3] RelativeLocationPath ::= Step| RelativeLocationPath '/' Step| AbbreviatedRelativeLocationPath

Un RelativeLocationPath sert à sélectionner un ensemble de noeuds basés sur leur position par rapport au noeud contextuel.

Un AbsoluteLocationPath sert à sélectionner un ensemble de noeuds basées sur leur position dans le document par rapport au noeud racine.

RécursivitéRécursivité

http://www.w3.org/TR/xpath#NT-LocationPath

[1] LocationPath ::= RelativeLocationPath| AbsoluteLocationPath

[2] AbsoluteLocationPath ::= '/' RelativeLocationPath?| AbbreviatedAbsoluteLocationPath

[3] RelativeLocationPath ::= Step| RelativeLocationPath '/' Step| AbbreviatedRelativeLocationPath

Un RelativeLocationPath sert à sélectionner un ensemble de noeuds basés sur leur position par rapport au noeud contextuel.

Un AbsoluteLocationPath sert à sélectionner un ensemble de noeuds basées sur leur position dans le document par rapport au noeud racine.

ExempleExemple

/child::doc /child::chapter[position()=5]/ child::section[ position()=2] selects the second section of the fifth chapter of the doc document element

ExempleExemple

child::para [position()=2][attribute::pe="warning"] selects the second para child of the context node if that child has a type attribute with value warning

Naviguer dans les documents XML avec XPathNaviguer dans les documents XML avec XPath

• Le langage d’adressage utilise des chemins pour désigner un ensemble d’objets

• Une étape de localisation se fait en 3 temps : un axe, est un chemin à travers l'arbre du document, débutant au nœud origine et suivant une relation particulière entre les nœuds.

un filtre, qui spécifie le type du noeud obtenus par l'étape de localisation

0 ou n prédicats, qui sont des expressions arbitraires pour raffiner l'ensemble des noeuds

ExemplesExemples

child::para[ position()=5][attribute::type="warning"] selects the fifth para child of the context node if that child has a type attribute with value warning

• Une étape de localisation se fait en 3 temps : • un axe, est un chemin à travers l'arbre du document,

débutant au nœud origine et suivant une relation particulière entre les nœuds.

• un filtre, qui spécifie le type du noeud obtenus par l'étape de localisation

• 0 ou n prédicats, qui sont des expressions arbitraires pour raffiner l'ensemble des noeuds

Naviguer dans les documents XML avec XPathNaviguer dans les documents XML avec XPath

• Introduction• Les axes• Les filtres• Les prédicats• Les fonctions prédéfinies• Syntaxe abrégée

AxisNameAxisName

Un AxisName est utilisé dans un Step pour identifier un chemin à suivre depuis un nœud donné vers les autres nœuds liés.

http://www.w3.org/TR/xpath#axes

AxisName ::= | 'ancestor'

| 'ancestor-or-self'| 'attribute'| 'child'| 'descendant'| 'descendant-or-self'| 'following'| 'following-sibling'| 'namespace'| 'parent'| 'preceding'| 'preceding-sibling'| 'self'

| 'ancestor'| 'ancestor'

| 'ancestor'| 'ancestor'

1

2

'ancestor-or-self''ancestor-or-self'

'ancestor-or-self''ancestor-or-self'

2

3

1

| 'child'| 'child'

| 'child'| 'child'

1 2

| 'descendant'| 'descendant'

| 'descendant'| 'descendant'

1 2

53 4

| 'descendant-or-self'| 'descendant-or-self'

| 'descendant-or-self'| 'descendant-or-self'

1

2 3

64 5

| 'following'| 'following'

| 'following'| 'following'

1

32

4

| 'following-sibling'| 'following-sibling'

| 'following-sibling'| 'following-sibling'

1 2

| 'parent'| 'parent'

| 'parent'| 'parent'

1

| 'preceding'| 'preceding'

| 'preceding'| 'preceding'

3

12

| 'preceding-sibling'| 'preceding-sibling'

| 'preceding-sibling'| 'preceding-sibling'

1

| 'self'| 'self'

1

AxisSpecifierAxisSpecifier

Un AxisSpecifier est soit un nom d'axe, soit une abréviation pour un nom d'axe. Il définit une direction de navigation à travers le document en définissant une liste ordonnée de nœuds qui peuvent être visités.

http://www.w3.org/TR/xpath#NT-AxisSpecifier

AxisSpecifier ::= AxisName '::' | AbbreviatedAxisSpecifier

http://www.w3.org/TR/xpath#NT-AbbreviatedAxisSpecifier

[13]   AbbreviatedAxisSpecifier   ::=   '@'?

ExempleExemple

ancestor::spécifie l'axe ancêtre

Preceding-sibling::Spécifie l'axe enfant précédent

@ spécifie l'axe attribut

Naviguer dans les documents XML avec XPathNaviguer dans les documents XML avec XPath

• Introduction• Les axes• Les filtres• Les prédicats• Les fonctions prédéfinies• Syntaxe abrégée

PredicatePredicate

Un prédicat est une expression qualificative utilisée pour sélectionner un sous ensemble des noeuds d'une ensemble de noeuds ou de Step.

http://www.w3.org/TR/xpath#predicates

[8]   Predicate   ::=   '[' PredicateExpr ']' # noter les []

[9]   PredicateExpr   ::=   Expr

ExempleExemple

Para[ 1] ou Para[ position()=1]Le premier élément enfant <para> du noeud contextuel

para[ last()]le dernier élément enfant du noeud contextuel.

Naviguer dans les documents XML avec XPath (3)Naviguer dans les documents XML avec XPath (3)

Les axes: self, child, parent,

descendant, descendant-or-self,

ancestor, ancestor-or-self,

preceding, preceding-sibling,

following, following-sibling, attribute

<AAA>           <XXX>                <DDD>                     <BBB/>                     <BBB/>                     <EEE/>                     <FFF/>                </DDD>           </XXX>           <CCC>                <DDD>                     <BBB/>                     <BBB id=“b”/>                     <GGG/>                     <FFF/>                </DDD>           </CCC>           <CCC>                <BBB name=“bbb”>                     <BBB id=“bbb”>                          <BBB/>                     </BBB>                </BBB>           </CCC> </AAA>

//CCC/self::*

Naviguer dans les documents XML avec XPath (3)Naviguer dans les documents XML avec XPath (3)

Les axes: self, child, parent,

descendant, descendant-or-self,

ancestor, ancestor-or-self,

preceding, preceding-sibling,

following, following-sibling, attribute

<AAA>           <XXX>                <DDD>                     <BBB/>                     <BBB/>                     <EEE/>                     <FFF/>                </DDD>           </XXX>           <CCC>                <DDD>                     <BBB/>                     <BBB id=“b”/>                     <GGG/>                     <FFF/>                </DDD>           </CCC>           <CCC>                <BBB name=“bbb”>                     <BBB id=“bbb”>                          <BBB/>                     </BBB>                </BBB>           </CCC> </AAA>

//CCC/child::*

Naviguer dans les documents XML avec XPath (3)Naviguer dans les documents XML avec XPath (3)

Les axes: self, child, parent,

descendant, descendant-or-self,

ancestor, ancestor-or-self,

preceding, preceding-sibling,

following, following-sibling, attribute

<AAA>           <XXX>                <DDD>                     <BBB/>                     <BBB/>                     <EEE/>                     <FFF/>                </DDD>           </XXX>           <CCC>                <DDD>                     <BBB/>                     <BBB id=“b”/>                     <GGG/>                     <FFF/>                </DDD>           </CCC>           <CCC>                <BBB name=“bbb”>                     <BBB id=“bbb”>                          <BBB/>                     </BBB>                </BBB>           </CCC> </AAA>

//CCC/parent::*

Naviguer dans les documents XML avec XPath (3)Naviguer dans les documents XML avec XPath (3)

Les axes: self, child, parent,

descendant, descendant-or-self,

ancestor, ancestor-or-self,

preceding, preceding-sibling,

following, following-sibling, attribute

<AAA>           <XXX>                <DDD>                     <BBB/>                     <BBB/>                     <EEE/>                     <FFF/>                </DDD>           </XXX>           <CCC>                <DDD>                     <BBB/>                     <BBB id=“b”/>                     <GGG/>                     <FFF/>                </DDD>           </CCC>           <CCC>                <BBB name=“bbb”>                     <BBB id=“bbb”>                          <BBB/>                     </BBB>                </BBB>           </CCC> </AAA>

//DDD/descendant::*

Naviguer dans les documents XML avec XPath (3)Naviguer dans les documents XML avec XPath (3)

Les axes: self, child, parent,

descendant, descendant-or-self,

ancestor, ancestor-or-self,

preceding, preceding-sibling,

following, following-sibling, attribute

<AAA>           <XXX>                <DDD>                     <BBB/>                     <BBB/>                     <EEE/>                     <FFF/>                </DDD>           </XXX>           <CCC>                <DDD>                     <BBB/>                     <BBB id=“b”/>                     <GGG/>                     <FFF/>                </DDD>           </CCC>           <CCC>                <BBB name=“bbb”>                     <BBB id=“bbb”>                          <BBB/>                     </BBB>                </BBB>           </CCC> </AAA>

//DDD/ancestor::*

Naviguer dans les documents XML avec XPath (3)Naviguer dans les documents XML avec XPath (3)

Les axes: self, child, parent,

descendant, descendant-or-self,

ancestor, ancestor-or-self,

preceding, preceding-sibling,

following, following-sibling, attribute

<AAA>           <XXX>                <DDD>                     <BBB/>                     <BBB/>                     <EEE/>                     <FFF/>                </DDD>           </XXX>           <CCC>                <DDD>                     <BBB/>                     <BBB id=“b”/>                     <GGG/>                     <FFF/>                </DDD>           </CCC>           <CCC>                <BBB name=“bbb”>                     <BBB id=“bbb”>                          <BBB/>                     </BBB>                </BBB>           </CCC> </AAA>

//GGG/preceding::*

Naviguer dans les documents XML avec XPath (3)Naviguer dans les documents XML avec XPath (3)

Les axes: self, child, parent,

descendant, descendant-or-self,

ancestor, ancestor-or-self,

preceding, preceding-sibling,

following, following-sibling, attribute

<AAA>           <XXX>                <DDD>                     <BBB/>                     <BBB/>                     <EEE/>                     <FFF/>                </DDD>           </XXX>           <CCC>                <DDD>                     <BBB/>                     <BBB id=“b”/>                     <GGG/>                     <FFF/>                </DDD>           </CCC>           <CCC>                <BBB name=“bbb”>                     <BBB id=“bbb”>                          <BBB/>                     </BBB>                </BBB>           </CCC> </AAA>

//GGG/preceding-sibling::*

Naviguer dans les documents XML avec XPath (3)Naviguer dans les documents XML avec XPath (3)

Les axes: self, child, parent,

descendant, descendant-or-self,

ancestor, ancestor-or-self,

preceding, preceding-sibling,

following, following-sibling, attribute

<AAA>           <XXX>                <DDD>                     <BBB/>                     <BBB/>                     <EEE/>                     <FFF/>                </DDD>           </XXX>           <CCC>                <DDD>                     <BBB/>                     <BBB id=“b”/>                     <GGG/>                     <FFF/>                </DDD>           </CCC>           <CCC>                <BBB name=“bbb”>                     <BBB id=“bbb”>                          <BBB/>                     </BBB>                </BBB>           </CCC> </AAA>

//*[attribute::id]

Naviguer dans les documents XML avec XPathNaviguer dans les documents XML avec XPath

• Introduction• Les axes• Les filtres• Les prédicats• Les fonctions prédéfinies• Syntaxe abrégée

FilterExprFilterExpr

FilterExpr est utilisé pour appliquer un ou plusieurs Predicates à un ensemble de noeuds, en sélectionnant un sous ensemble de ces noeuds satisfaisant certaines conditions.

http://www.w3.org/TR/xpath#NT-FilterExpr

[20]   FilterExpr   ::=   PrimaryExpr| FilterExpr Predicate

Naviguer dans les documents XML avec XPath (3)Naviguer dans les documents XML avec XPath (3)

Les axes: self, child, parent,

descendant, descendant-or-self,

ancestor, ancestor-or-self,

preceding, preceding-sibling,

following, following-sibling, attribute

Les filtres: /, *, element

Les prédicats: [expression], les opérations: +, -, *, div, mod, >, >=, <, <=, =, !=

Syntaxe abrégée: //, ., .., @

/AAA/*/DDD/*<AAA>           <XXX>                <DDD>                     <BBB/>                     <BBB/>                     <EEE/>                     <FFF/>                </DDD>           </XXX>           <CCC>                <DDD>                     <BBB/>                     <BBB id=“b”/>                     <GGG/>                     <FFF/>                </DDD>           </CCC>           <CCC>                <BBB name=“bbb”>                     <BBB id=“bbb”>                          <BBB/>                     </BBB>                </BBB>           </CCC> </AAA>

Naviguer dans les documents XML avec XPathNaviguer dans les documents XML avec XPath

• Introduction• Les axes• Les filtres• Les prédicats• Les fonctions prédéfinies• Syntaxe abrégée

Naviguer dans les documents XML avec XPath (3)Naviguer dans les documents XML avec XPath (3)

Les axes: self, child, parent,

descendant, descendant-or-self,

ancestor, ancestor-or-self,

preceding, preceding-sibling,

following, following-sibling, attribute

Les filtres: /, *, element

Les prédicats: [expression], les opérations: +, -, *, div, mod, >, >=, <, <=, =, !=

Syntaxe abrégée: //, ., .., @

//BBB[attribute::id=‘bbb’]<AAA>           <XXX>                <DDD>                     <BBB/>                     <BBB/>                     <EEE/>                     <FFF/>                </DDD>           </XXX>           <CCC>                <DDD>                     <BBB/>                     <BBB id=“b”/>                     <GGG/>                     <FFF/>                </DDD>           </CCC>           <CCC>                <BBB name=“bbb”>                     <BBB id=“bbb”>                          <BBB/>                     </BBB>                </BBB>           </CCC> </AAA>

Syntaxe abrégéeSyntaxe abrégée

./toto self::toto

toto child::toto (et non toto)

../toto parent::toto

@titi attribute::titi

//toto /descendant-or-self::node()/child::toto

.//toto descendant-or-self::node()/child::toto

toto[2] child::toto[position() = 2]

Recommended