Search This Blog

Saturday, 3 November 2012

XPATH bits and pieces

This is just a dump of some code I wrote as part of an interview process.  No notes here, just make of it what you will!

Code (yeah, yeah, I know - it's a VB module, yuk!):


Module Module1

    Sub Main()

        Try

            RenderTop()

            Dim xmlFilePath As String = System.AppDomain.CurrentDomain.BaseDirectory.ToString & "BookStore.xml"

            Dim xDoc As XPathDocument = New XPathDocument(xmlFilePath)
            Dim xPathNav As XPathNavigator = xDoc.CreateNavigator
            Dim namespaceManager As New XmlNamespaceManager(xPathNav.NameTable)
            namespaceManager.AddNamespace("x", "http://www.focus-solutions.co.uk/books")
            namespaceManager.AddNamespace("g", "http://www.contoso.com/genre")
            namespaceManager.AddNamespace("g2", "http://www.northwind.co.uk/genre")
            namespaceManager.AddNamespace("a", "http://www.focus-solutions.co.uk/author")

            Question1(xPathNav, namespaceManager)

            Question2(xPathNav, namespaceManager)

            Question3(xPathNav, namespaceManager)

            Question4(xPathNav, namespaceManager)

        Catch ex As Exception
            Console.ForegroundColor = ConsoleColor.Red
            Console.WriteLine(ex.Message)
        End Try

        Console.ReadKey()

    End Sub

    Private Sub Question1(ByVal xPathNav As XPathNavigator, ByVal namespaceMan As XmlNamespaceManager)
        RenderHeading("Question 1 - books under £10")

        Dim xNodeIterator As XPathNodeIterator

        xNodeIterator = xPathNav.Select("/x:bookstore/x:book[x:price<10]", namespaceMan)

        While xNodeIterator.MoveNext()
            Console.WriteLine(xNodeIterator.Current.Value)
        End While

    End Sub

    Private Sub Question2(ByVal xPathNav As XPathNavigator, ByVal namespaceMan As XmlNamespaceManager)
        RenderHeading("Question 2 - Northwind novels")
        
        Dim xNodeIterator As XPathNodeIterator

        xNodeIterator = xPathNav.Select("/x:bookstore/x:book[@g2:genre='novel']", namespaceMan)

        While xNodeIterator.MoveNext()
            Console.WriteLine(xNodeIterator.Current.Value)
        End While

    End Sub

    Private Sub Question3(ByVal xPathNav As XPathNavigator, ByVal namespaceMan As XmlNamespaceManager)
        RenderHeading("Question 3 - Author’s first name has an 'a' in it")

        Dim xNodeIterator As XPathNodeIterator

        xNodeIterator = xPathNav.Select("/x:bookstore/x:book[a:author/a:first-name[contains(.,'a')]]", namespaceMan)

        While xNodeIterator.MoveNext()
            Console.WriteLine(xNodeIterator.Current.Value)
        End While

    End Sub

    Private Sub Question4(ByVal xPathNav As XPathNavigator, ByVal namespaceMan As XmlNamespaceManager)
        RenderHeading("Question 4 - Average price of Contoso novels with price > £6")

        Dim exp As String = "/x:bookstore/x:book[@g:genre='novel' and ./x:price>6]/x:price"

        Console.WriteLine(xPathNav.Evaluate(String.Format("sum({0}) div count({0})", exp), namespaceMan))

    End Sub


    '======= Helpers ==============

    Private Sub RenderHeading(ByVal headingName As String)
        Console.WriteLine()
        Console.ForegroundColor = ConsoleColor.White
        Console.WriteLine(headingName)
        Console.ForegroundColor = ConsoleColor.Gray
        Console.WriteLine()
    End Sub

    Private Sub RenderTop()
        Console.ForegroundColor = ConsoleColor.Yellow
        Console.WriteLine("XML Test - Nik Rhodes")
        Console.WriteLine()
    End Sub

End Module

XML:


<bookstore xmlns="http://www.focus-solutions.co.uk/books" xmlns:g="http://www.contoso.com/genre" xmlns:g2="http://www.northwind.co.uk/genre">
<book g:genre="novel" g2:genre="thriller" publicationdate="2010-03-01" ISBN="1-123456-15-0">
<title>61 Hours</title>
<author xmlns="http://www.focus-solutions.co.uk/author">
<first-name>Lee</first-name>
<last-name>Child</last-name>
</author>
<price>6.99</price>
</book>
<book g:genre="novel" g2:genre="crime" publicationdate="2009-05-10" ISBN="0-123-4567-2">
<title>Nine Dragons</title>
<author xmlns="http://www.focus-solutions.co.uk/author">
<first-name>Michael</first-name>
<last-name>Connelly</last-name>
</author>
<price>11.99</price>
</book>
<book g2:genre="novel" g:genre="autobiography" publicationdate="2004-01-01" ISBN="0-127-4567-2">
<title>David Beckham: My Side</title>
<author xmlns="http://www.focus-solutions.co.uk/author">
      <first-name>David</first-name>
      <last-name>Beckham</last-name>
</author>
<price>9.99</price>
</book>
<book g2:genre="fiction" g:genre="novel" publicationdate="1975-04-20" ISBN="3-456-0141-8">
<title>A Collection of Short Stories</title>
<author xmlns="http://www.focus-solutions.co.uk/author">
<first-name>Dominic</first-name>
<last-name>Cake</last-name>
</author>
<price>5.99</price>
</book>
<book g:genre="autobiography" g2:genre="novel" publicationdate="1950-02-28" ISBN="3-563-3463-1">
<title>My Story</title>
<author xmlns="http://www.focus-solutions.co.uk/author">
<first-name>Donald</first-name>
<last-name>Duck</last-name>
</author>
<price>17.99</price>
</book>
<book g:genre="novel" g2:genre="novel" publicationdate="1986-05-20" ISBN="1-125-8973-X">
<title>Tall Trees</title>
<author xmlns="http://www.focus-solutions.co.uk/author">
<first-name>Derek</first-name>
<last-name>McDowall</last-name>
</author>
<price>5.99</price>
</book>
<book g:genre="novel"  g2:genre="fiction" publicationdate="1860-12-01" ISBN="5-245-6567-7">
<title>Great Expectations</title>
<author xmlns="http://www.focus-solutions.co.uk/author">
<first-name>Charles</first-name>
<last-name>Dickens</last-name>
</author>
    <otherbooks>
      <!-- The following books are not available in the book store -->
      <book g:genre="novel" g2:genre="fiction" publicationdate="1843-12-01" ISBN="5-245-6567-1">
        <title>A Christmas Carol</title>
        <price>14.99</price>
      </book>
      <book g:genre="novel" g2:genre="fiction" publicationdate="1839-12-01" ISBN="5-245-6567-2">
        <title>Oliver Twist</title>
        <price>9.99</price>
      </book>
      <book g:genre="novel" g2:genre="fiction" publicationdate="1859-12-01" ISBN="5-245-6567-3">
        <title>A Tale Of Two Cities</title>
        <price>5.99</price>
      </book>
      <book g:genre="novel" g2:genre="fiction" publicationdate="1853-12-01" ISBN="5-245-6567-4">
        <title>Bleak House</title>
        <price>3.99</price>
      </book>
    </otherbooks>
<price>7.99</price>
</book>
</bookstore>


No comments:

Post a Comment