In this post, we will solve Querying XML Datastores with XPath – 3 HackerRank Solution. This problem (Querying XML Datastores with XPath – 3) 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 format of the movies with popularity<8, in the same order as which they occur in the given XML.
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
DVD
VHS
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 – 3 – 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 # XPath Selector for listing the format of the movies with popularity<8, in the same order as which they occur in the given XML. # Fill in the blanks to complete the required XPath selector query puts doc.elements.each("collection/movie[popularity/text() < 8]/format/text()")
Note: This problem (Querying XML Datastores with XPath – 3) is generated by HackerRank but the solution is provided by CodingBroz. This tutorial is only for Educational and Learning purpose.