-1

For my homework i got asked in XPATH a question that i can't response. Here is the question "Provide an XPath expression which, from a element, returns the number of people after (in document order) that whose postal code is the current node."

<addressbook xmlns="https://esi-bru.be/WEBR4"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="https://esi-bru.be/WEBR4 addressbook.xsd">
    <persons>
        <person id='p1'>
            <identity>
                <lastname>Loiselle</lastname>
                <firstnames>
                    <firstname>Raymond</firstname>
                    <firstname>Claude</firstname>
                </firstnames>
                <sex>MALE</sex>
                <birth>1991-10-15</birth>
                <death>NULL</death>
            </identity>
            <addresses>
                <postal>
                    <country_id>BE</country_id>
                    <postalcode>4850</postalcode>
                    <city>Plombières</city>
                    <street>Rue Basse</street>
                    <number>113</number>
                </postal>
                <emails>
                    <email use='PROFESSIONAL'>natoque.penatibus@loremipsumsodales.org</email>
                    <email use='PRIVATE'>id@lacusMaurisnon.net</email>
                </emails>
                <phones>
                    <phone use='PRIVATE' type='LANDLINE'>042082616</phone>
                    <phone use='PRIVATE' type='CELLPHONE'>0497806590</phone>
                    <phone use='PROFESSIONAL' type='LANDLINE'>022966411</phone>
                    <phone use='PROFESSIONAL' type='IPPHONE'>082648320</phone>
                </phones>
            </addresses>
            <relations>
                <relation>
                    <person_id>p3</person_id>
                    <level>1</level>
                </relation>
                <relation>
                    <person_id>p7</person_id>
                    <level>-1</level>
                </relation>
                <relation>
                    <person_id>p12</person_id>
                    <level>0</level>
                </relation>
                <relation>
                    <person_id>p5</person_id>
                    <level>3</level>
                </relation>
            </relations>
        </person>
        <person id='p2'>
            <identity>
                <lastname>Frueh</lastname>
                <firstnames>
                    <firstname>Andreas</firstname>
                </firstnames>
                <sex>MALE</sex>
                <birth>1984-04-15</birth>
                <death>NULL</death>
            </identity>
            <addresses>
                <postal>
                    <country_id>DE</country_id>
                    <postalcode>4850</postalcode>
                    <city>Vettelschoß</city>
                    <street>Wallstrasse</street>
                    <number>21</number>
                </postal>
                <emails>
                    <email use='PRIVATE'>Aenean.eget@etlibero.net</email>
                    <email use='PROFESSIONAL'>Proin.nisl.sem@vitaevelitegestas.edu</email>
                </emails>
                <phones>
                    <phone use='PRIVATE' type='LANDLINE'>03327839582</phone>
                    <phone use='PRIVATE' type='CELLPHONE'>017436797167</phone>
                    <phone use='PROFESSIONAL' type='LANDLINE'>07056419296</phone>
                    <phone use='PROFESSIONAL' type='IPPHONE'>09747178748</phone>
                </phones>
            </addresses>
            <relations>
                <relation>
                    <person_id>p14</person_id>
                    <level>4</level>
                </relation>
                <relation>
                    <person_id>p1</person_id>
                    <level>2</level>
                </relation>
                <relation>
                    <person_id>p3</person_id>
                    <level>-1</level>
                </relation>
                <relation>
                    <person_id>p10</person_id>
                    <level>1</level>
                </relation>
                <relation>
                    <person_id>p11</person_id>
                    <level>1</level>
                </relation>
                <relation>
                    <person_id>p6</person_id>
                    <level>2</level>
                </relation>
                <relation>
                    <person_id>p5</person_id>
                    <level>2</level>
                </relation>
            </relations>
        </person>
        <person id='p3'>
            <identity>
                <lastname>Diederich</lastname>
                <firstnames>
                    <firstname>Juliane</firstname>
                </firstnames>
                <sex>FEMALE</sex>
                <birth>1963-12-23</birth>
                <death>2005-05-02</death>
            </identity>
            <addresses>
                <postal>
                    <country_id>DE</country_id>
                    <postalcode>56414</postalcode>
                    <city>Oberahr</city>
                    <street>Stuttgarter Platz</street>
                    <number>75</number>
                </postal>
                <emails>
                    <email use='PRIVATE'>Aenean.eget@etlibero.net</email>
                </emails>
                <phones/>
            </addresses>
            <relations>
                <relation>
                    <person_id>p12</person_id>
                    <level>4</level>
                </relation>
                <relation>
                    <person_id>p11</person_id>
                    <level>4</level>
                </relation>
                <relation>
                    <person_id>p1</person_id>
                    <level>4</level>
                </relation>
            </relations>
        </person>
    </persons>
</addressbook>

I started a request but i don't fully understand how . works and what would be my current node. how could i compare with all other nodes from the same type in the order without taking the one that have a position() < the postalcode that i'm comparing to the other.

request:

//postalcode/(count(./text() = //person/addresses/postal/postalcode/text()))

I do not necessarily ask for a correction of the request but more to be able to better understand certain concepts to be able to answer it.

Sorry for my bad english btw..

Machavity
  • 30,841
  • 27
  • 92
  • 100
  • 3
    Please don't make more work for other people by vandalizing your posts. By posting on the Stack Exchange network, you've granted a non-revocable right, under the [CC BY-SA 4.0 license](//creativecommons.org/licenses/by-sa/4.0/), for Stack Exchange to distribute that content (i.e. regardless of your future choices). By Stack Exchange policy, the non-vandalized version of the post is the one which is distributed. Thus, any vandalism will be reverted. If you want to know more about deleting a post please see: [How does deleting work?](//meta.stackexchange.com/q/5221) – Machavity May 27 '20 at 22:52

2 Answers2

1

Here are some key principles to understand to solve your homework problem:

  1. Your XML has a default namespace. See How does XPath deal with XML namespaces?

  2. Predicates should be placed in [ ], not ( ).

  3. The following:: or following-sibling:: axis will help you limit the selection to points forward in document order from the targeted element. Example: Grab following siblings in XPath?

kjhughes
  • 106,133
  • 27
  • 181
  • 240
0

If your desired output is a number, the first part of your expression should begin with count.

Wildcard * and [position number] should be useful too.

The pattern of your XPath will look like :

count(path to first element[predicate][position number]/axis::something[predicate])
E.Wiest
  • 5,425
  • 2
  • 7
  • 12