[XML-DEV Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Creating a "complete" specification is hard

  • From: Roger L Costello <costello@mitre.org>
  • To: "xml-dev@lists.xml.org" <xml-dev@lists.xml.org>
  • Date: Sat, 28 May 2022 12:06:47 +0000

Creating a "complete" specification is hard

Hi Folks,

 

Yesterday I posted a message asking:

 

With XML documents like this:

 

<Books>

    <Book>

        <Title>The Science of Programming</Title>

        <Author>David Gries</Author>

    </Book>

    <Book>

        <Title>Compiler Construction for Digital Computers</Title>

        <Author>David Gries</Author>

    </Book>

    <Book>

        <Title>The Emperor's New Mind</Title>

    </Book>

    <Book>

        <Title>Algorithms</Title>

    </Book>

    <Book>

        <Title>The Path to Power</Title>

        <Author>Robert A. Caro</Author>

    </Book>

</Books>

 

How can we answer this question:

 

              How many distinct authors are there in the book list,

              where the books with an UNKNOWN author are

              counted as 1?

 

It was pointed out to me that my specification is ambiguous:

 

  • Should a Book element with an empty Author element and a Book element with an absent Author element be treated as equivalent or as distinct?
  • Should Book elements with no Author be counted together as a single value or separately as distinct values (because undefined)?

 

Wow!

 

Lesson Learned: Creating a specification that considers all cases is hard.

 

Let’s extend the sample XML document to include a Book element with an empty Author element:

 

<Books>

    <Book>

        <Title>The Science of Programming</Title>

        <Author>David Gries</Author>

    </Book>

    <Book>

        <Title>Compiler Construction for Digital Computers</Title>

        <Author>David Gries</Author>

    </Book>

    <Book>

        <Title>The Emperor's New Mind</Title>

    </Book>

    <Book>

        <Title>Algorithms</Title>

    </Book>

    <Book>

        <Title>The Path to Power</Title>

        <Author>Robert A. Caro</Author>

    </Book>

    <Book>

        <Title>Constraint Programming Languages</Title>

        <Author></Author>

    </Book>

</Books>

 

Here are XPath expressions to handle each case:

 

Treat a Book element with an empty Author element and a Book element with an absent Author element as equivalent:

count(distinct-values(/Books/Book/string(Author)))

 

Evaluating that XPath expression on the XML document yields: 3

 

Treat a Book element with an empty Author element and a Book element with an absent Author element as distinct:

let $book := /Books/Book

  return

    count(distinct-values($book/Author)) + xs:integer(exists($book[not(Author)]))

 

Evaluating that XPath expression on the XML document yields: 4

 

Treat each Book element with an absent Author element as distinct:

 

let $book := /Books/Book

  return

    count(distinct-values($book/Author)) + count($book[not(Author)])

 

Evaluating that XPath expression on the XML document yields: 5

 

Acknowledgements

 

Thank you to the following people for their tremendous help and insights:

 

  • Michael Kay
  • Dimitre Novatchev
  • Wendell Piez

 

/Roger

 



[Date Prev] | [Thread Prev] | [Thread Next] | [Date Next] -- [Date Index] | [Thread Index]


PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
 

Stylus Studio has published XML-DEV in RSS and ATOM formats, enabling users to easily subcribe to the list from their preferred news reader application.


Stylus Studio Sponsored Links are added links designed to provide related and additional information to the visitors of this website. they were not included by the author in the initial post. To view the content without the Sponsor Links please click here.

Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.