1

I'm making different selection on this XML, but for most of them i'm not sure that i done the simplest solution, and for 2 of them i'm not able to provide a working solution.

<?xml version="1.0" ?>
<!DOCTYPE list[
<!ELEMENT list(man|woman)*>
<!ELEMENT man(sons,daughters)>
<!ATTLIST man name CDATA #REQUIRED>
<!ELEMENT man(sons,daughters)>
<!ATTLIST woman name CDATA #REQUIRED>
<!ELEMENT sons(man)*>
<!ELEMENT daughters(woman)*>

<list>
    <man name="Jean-Bernard">
        <sons>
            <man name="Marc-Antoine"/>
        </sons>
        <daughters>
            <woman name="Marie-Jeanne">
                <sons/>
                <daughters>
                    <woman name="Anne-Sophie"/>
                </daughters>
            </woman>
            <woman name="Rose-Marie">
                <daughters>
                    <woman name="Marie-Cécile"/>
                </daughters>
            </woman>
        </daughters>
    </man>
    <woman name="Marie-Jeanne">
        <sons>
            <man name="Ghislain-Auguste">
                <sons/>
            </man>  
        </sons>
        <daughters>
            <woman name="Rita-Lise">
            <sons>
                <man name="Alain-Luc"/>
                <man name="Jules-Edouard"/>
            </sons>
            </woman>
        </daughters>
    </woman>
</list>

What i have done :

  • Selecting all women : //woman
  • Selecting all women with the name "Marie Jeanne" : //woman[@name='Marie-Jeanne']
  • Selecting all woman with a name which contains "Marie" : //woman[contains(@name,Marie)]

What i have done but this is not exact or i think it can be better :

  • Selecting all women or men who have at least 2 children : //*[sum(count(sons/man), count(daughters/woman)) >= 2]
  • Selecting all women or men who are grandparents : i'm totally stuck for this one i can't see how i can select with so much unknown. In my mind this is like that : //*/*/*/* . . . i don't understand how to start.
Julien Breuil
  • 165
  • 2
  • 15

2 Answers2

3

Selecting all women or men who have at least 2 children:

//*[(self::man or self::woman) and count(*/*) > 1]

Selecting all women or men who are grandparents

//*[(self::man or self::woman) and */*/*/*]
Kirill Polishchuk
  • 51,053
  • 10
  • 118
  • 119
1

To make it easier to select all women or men who are grandparents you could use the "self" declarative XPath to select multiple tags:

//*[count(*[self::sons or self::daughters]/*[self::man or self::woman]/*[self::sons or self::daughters]/*[self::man or self::woman]) >= 1]
Community
  • 1
  • 1
Justin Bicknell
  • 4,716
  • 16
  • 25