54
Xpath Xpath XPath is a language for finding information in an XML documen

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

Embed Size (px)

Citation preview

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

XpathXpath

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

Page 3: Xpath 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

Page 4: Xpath 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

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

IntroductionIntroduction

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

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

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.

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

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.

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

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.

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

ExempleExemple

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

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

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

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

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

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

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

Page 13: Xpath 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

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

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'

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

| 'ancestor'| 'ancestor'

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

| 'ancestor'| 'ancestor'

1

2

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

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

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

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

2

3

1

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

| 'child'| 'child'

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

| 'child'| 'child'

1 2

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

| 'descendant'| 'descendant'

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

| 'descendant'| 'descendant'

1 2

53 4

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

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

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

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

1

2 3

64 5

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

| 'following'| 'following'

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

| 'following'| 'following'

1

32

4

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

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

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

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

1 2

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

| 'parent'| 'parent'

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

| 'parent'| 'parent'

1

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

| 'preceding'| 'preceding'

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

| 'preceding'| 'preceding'

3

12

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

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

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

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

1

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

| 'self'| 'self'

1

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

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   ::=   '@'?

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

ExempleExemple

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

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

@ spécifie l'axe attribut

Page 38: Xpath 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

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

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

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

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.

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

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::*

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

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::*

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

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::*

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

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::*

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

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::*

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

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::*

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

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::*

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

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]

Page 49: Xpath 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

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

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

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

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>

Page 52: Xpath 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

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

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>

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

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]