04
Feb

xPath Notes

XPath notes:
====================

Source:  http://rstudio-pubs-static.s3.amazonaws.com/180219_050df438cb0f4bae9af4ba9c24ecd0f6.html


#1. Attribute selection
========================

Source:    	<document name="getit Übungsaufgaben" />
Challenge:  	Output the content of the attribute “name”.

Answer:		//@name


#2. Conditional element selection
===================================

Source:
  <document xmlns:xlink="http://www.w3.org/1999/xlink">
    <linkList name="A">
      <document xlink:href="15024" />
      <document xlink:href="15028" />
    </linkList>
    <linkList name="B">
      <document xlink:href="15030" />
      <document xlink:href="15032" />
    </linkList>
  </document>

Challenge:	Select all document elements below the node “linkList” with the name A.


Answer:		//linkList[@name='A']/*
		(//linkList[@name='A']/*)[1]  => first document



#3. Filtering by attribute value
==================================

Source:
<jobs>
<job priority="critical" name="Müll rausbringen" />
    <job priority="low" name="Möbel säubern" />
    <job priority="low" name="Teppich reinigen" />
    <job priority="medium" name="Fenster putzen" />
    <job priority="high" name="Pflanzen gießen" />
</jobs>

Challenge:	Select all jobs with a priority corresponding to the value “critical” or “high”.

Answer:		//job[@priority='critical' or @priority='high']


#4. Filtering by numeric values
=================================

Source:
  <persons>
    <person firstName="Hans" lastName="Mustermann" age="28" />
    <person firstName="Herbert" lastName="Möllemann" age="33" />
    <person firstName="Peter" lastName="Meier" age="37" />
    <person firstName="Ulrike" lastName="Albrecht" age="45" />
  </persons>

Challenge:	Select all persons aged less than 35 years.

Answer:		//person[@age<35]



#5. Limiting number
====================

Source:
  <persons>
    <person firstName="Hans" lastName="Mustermann" age="28" />
    <person firstName="Herbert" lastName="Möllemann" age="33" />
    <person firstName="Peter" lastName="Meier" age="37" />
    <person firstName="Ulrike" lastName="Albrecht" age="45" />
  </persons>

Challenge:	Select the first three person elements

Answer:		//person[position() < 4]




#6. Filtering by character conditions
======================================

Source:
  <persons>
    <person firstName="Hans" lastName="Mustermann" age="28"></person>
    <person firstName="Herbert" lastName="Möllemann" age="33"></person>
    <person firstName="Peter" lastName="Meier" age="37"></person>
    <person firstName="Ulrike" lastName="Albrecht" age="45"></person>
  </persons>

Challenge:	Select all persons whose first name begins with the letter H.

Answer:		//person[starts-with(@firstname, 'H')]




#7. Filtering by text length
=============================

Source:
        <persons>
          <person firstName="Hans" lastName="Mustermann" age="28" />
          <person firstName="Herbert" lastName="Möllemann" age="33" />
          <person firstName="Peter" lastName="Meier" age="37" />
          <person firstName="Ulrike" lastName="Albrecht" age="45" />
          <person firstName="Uwe" lastName="Peters" age="34" />
        </persons>

Challenge:	Select all person elements with an attribute “firstName” a maximum of 5 characters long.

Answer:		//person[string-length(@firstname) < 6]




#8. Selecting the following siblings
=====================================

Source:
  <products>
    <product id="1" name="Teekanne" price="25.00" category="1"></product>
    <product id="2" name="Bleistift" price="0.29" category="2"></product>
    <product id="3" name="Lautsprecher" price="19.00" category="2"></product>
    <product id="4" name="Tasse" price="1.99" category="1"></product>
    <product id="5" name="Apfelsaft" price="1.49" category="1"></product>
    <product id="6" name="CD-Rohling" price="0.89" category="2"></product>
    <category id="1" name="Sortiment 2005"></product>
    <category id="2" name="Sortiment 2006"></product>
  </products>

Challenge:	Select the following product-siblings relatively from node where name=Lautsprecher

Answer:		//product[@name="Lautsprecher"]/following-sibling::product




#9. Selecting following siblings with filtering
=================================================

Source:
  <products>
    <product id="1" name="Teekanne" price="25.00" category="1" />
    <product id="2" name="Bleistift" price="0.29" category="2" />
    <product id="3" name="Lautsprecher" price="19.00" category="2" />
    <product id="4" name="Tasse" price="1.99" category="1" />
    <product id="5" name="Apfelsaft" price="1.49" category="1" />
    <product id="6" name="CD-Rohling" price="0.89" category="2" />
    <category id="1" name="Sortiment 2005" />
    <category id="2" name="Sortiment 2006" />
  </products>

Challenge:	Select the following product siblings, which contain the value 1 as a category, relatively from node where name=Lautsprecher

Answer:		//product[@name="Lautsprecher"]/following-sibling::product[@category="1"]



#10. Selecting all links to Google within a document
======================================================

Source:
        <html xmlns="http://www.w3.org/1999/xhtml">
          <head>
          </head>
          <body>
            <div>
              <ul>
                <li><a href="http://www.google.de/">http://www.google.de/Google</a>
                  <ul>
                    <li><a href="http://earth.google.de/">Google Earth</a></li>
                    <li><a href="http://picasa.google.de/intl/de/">Picasa</a></li>
                  </ul>
                </li> 
                <li><a href="http://www.heise.de/">Heise</a></li> 
                <li><a href="http://www.yahoo.de/">Yahoo</a></li> 
              </ul>
            </div>
          </body>
        </html>

Challenge:	Collect all links in the document which refer to a Google service.	

Answer:		//a[contains(@href, "google" )]


#11. Selecting elements by name
=================================

Source:
    <document>
      <item_0001>Erstes Element</item_0001>
      <item_0002>Zweites Element</item_0002>
      <item_0003>Drittes Element</item_0003>
      <item_0004>Viertes Element</item_0004>
      <other_0001>Erstes Element</other_0001>
      <other_0002>Zweites Element</other_0002>
    </document>

Challenge:	Select all elements with the name beginning with »item«.

Answer:		//*[contains(local-name(), "item")]



#12. Counting elements selected by certain criteria
====================================================

Source:
  <collection>
    <artist>
      <name>Robbie Williams</name>
      <cds>
        <cd>Rudebox</cd>
        <cd>Swing when you're winning</cd>
      </cds>
    </artist>
    <band>
      <name>Juli</name>
      <cds>
        <cd>Ein neuer Tag</cd>
      </cds>
    </band>
    <band>
      <name>Silbermond</name>
      <cds>
        <cd>Verschwende deine Zeit</cd>
      </cds>
    </band>
    <artist>
      <name>Michael Jackson</name>
      <cds>
        <cd>Bad</cd>
        <cd>Thriller</cd>
      </cds>
    </artist>
  </collection>

Challenge:	Select the name of the artist that goes with the CD.

Answer:		//cd/ancestor::artist/name[text()]



#13. First and last element
============================

Source:
<collection>
  <artist>
    <name>Robbie Williams</name>
    <cds>
      <cd>Rudebox</cd>
      <cd>Swing when you're winning</cd>
    </cds>
  </artist>
  <band>
    <name>Juli</name>
    <cds>
      <cd>Ein neuer Tag</cd>
    </cds>
  </band>
  <band>
    <name>Silbermond</name>
    <cds>
      <cd>Verschwende deine Zeit</cd>
    </cds>
  </band>
  <artist>
    <name>Michael Jackson</name>
    <cds>
      <cd>Bad</cd>
      <cd>Thriller</cd>
    </cds>
  </artist>
</collection>

Challenge:	Output the name of the first-listed CD by the last artist in the list

Answer:		//artist[last()]//cd[position() = 1]