Querying XML Datastores with XPath – 9 – HackerRank Solution

In this post, we will solve Querying XML Datastores with XPath – 9 HackerRank Solution. This problem (Querying XML Datastores with XPath – 9) is a part of HackerRank Databases series.

Task

XPath is a very valuable tool, often used for querying XML databases. XPath queries (or close variants) are also used in the process of Web Scraping while retreiving data from structured XHTML-compliant web pages, specially those with tabulated data.

A quick XPath tutorial is available here.

Provided XML Fragment and Task Description

Assume that you have been provided the fragment of XML, shown below. Your task is to write the XPath selector for listing the title of the second movie for which the marked shelf = “B”.

The Ruby code for handling input, output and document construction has already been provided. You only need to fill up the required blanks as indicated in the template code provided. This challenge is agnostic to language-specific knowledge and you do not require to know ruby – as only the XPath selector needs to be completed by you.

<collection shelf="Classics">
<movie title="The Enemy" shelf="A">
   <type>War, Thriller</type>
   <format>DVD</format>
   <year>2001</year>
   <rating>PG</rating>
   <popularity>10</popularity>
   <description>Talk about a war</description>
</movie>
<movie title="Transformers" shelf="B">
   <type>Science Fiction</type>
   <format>DVD</format>
   <year>1980</year>
   <rating>R</rating>
   <popularity>7</popularity>
   <description>Science Fiction</description>
</movie>
   <movie title="Trigun" shelf="B">
   <type>Action</type>
   <format>DVD</format>
   <episodes>4</episodes>
   <rating>PG</rating>
   <popularity>10</popularity>
   <description>Quite a bit of action!</description>
</movie>
<movie title="Ishtar" shelf="A">
   <type>Comedy</type>
   <format>VHS</format>
   <rating>PG</rating>
   <popularity>2</popularity>
   <description>Boring</description>
</movie>
</collection>

Expected Output

Trigun  

Your code will also be run against a hidden test case, which is similar in schema and format to the XML fragment shown above – only some of the values in the fields will be changed. This is to prevent the submission of hard-coded answers.

Solution – Querying XML Datastores with XPath – 9 – HackerRank Solution

# Standard ruby library for XML parsing
require 'rexml/document'
include REXML

# Enter your code here. Read input from STDIN. Print output to STDOUT
xmlText = "" 

# Read the input XML Fragment
while line = gets()
   xmlText += line
end

doc = Document.new xmlText
# Write the XPath selector for listing the title of the second movie for which the marked shelf = "B".
# Fill in the blanks to complete the required XPath selector query
puts doc.elements.each("collection/movie[@shelf = 'B'][2]/@title")

Note: This problem (Querying XML Datastores with XPath – 9) is generated by HackerRank but the solution is provided by CodingBroz. This tutorial is only for Educational and Learning purpose.

Leave a Comment

Your email address will not be published. Required fields are marked *