Search code examples
xmlxsltxquery

Count number of elements with same tag


Building upon books.xml transform to CSV: repeat title on each row:

For the document below, how can I count

  • the number of authors per book
  • the number of unique authors per book?

In this case, they would both be the same:

+--------------------+---------------+
|                    |               |
| Title              | Author_count  |
+--------------------+---------------+
|                    |               |
| Everyday Italian   | 1             |
+--------------------+---------------+
|                    |               |
| Harry Potter       | 1             |
+--------------------+---------------+
|                    |               |
| XQuery Kick Start  | 5             |
+--------------------+---------------+
|                    |               |
| Learning XML       | 1             |
+--------------------+---------------+

xslt or xquery approaches ar both OK. I would prefer tabular output.

<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
    <book category="COOKING">
        <title lang="en">Everyday Italian</title>
        <author>Giada De Laurentiis</author>
    </book>
    <book category="CHILDREN">
        <title lang="en">Harry Potter</title>
        <author>J K. Rowling</author>
    </book>
    <book category="WEB">
        <title lang="en">XQuery Kick Start</title>
        <author>James McGovern</author>
        <author>Per Bothner</author>
        <author>Kurt Cagle</author>
        <author>James Linn</author>
        <author>Vaidyanathan Nagarajan</author>
    </book>
    <book category="WEB">
        <title lang="en">Learning XML</title>
        <author>Erik T. Ray</author>
    </book>
</bookstore>

Solution

  • I am using BaseX v.9.5.2

    XQuery

    xquery version "3.1";
    
    declare context item := document {
    <bookstore>
        <book category="COOKING">
            <title lang="en">Everyday Italian</title>
            <author>Giada De Laurentiis</author>
        </book>
        <book category="CHILDREN">
            <title lang="en">Harry Potter</title>
            <author>J K. Rowling</author>
        </book>
        <book category="WEB">
            <title lang="en">XQuery Kick Start</title>
            <author>James McGovern</author>
            <author>Per Bothner</author>
            <author>Kurt Cagle</author>
            <author>James Linn</author>
            <author>Vaidyanathan Nagarajan</author>
        </book>
        <book category="WEB">
            <title lang="en">Learning XML</title>
            <author>Erik T. Ray</author>
        </book>
    </bookstore>
    };
    
    <root>
    {
      for $x in ./bookstore/book
      return <r>
        <title>{data($x/title)}</title>
        <author_count>{count($x/author)}</author_count>
      </r>
    }
    </root>
    

    Output

    <root>
      <r>
        <title>Everyday Italian</title>
        <author_count>1</author_count>
      </r>
      <r>
        <title>Harry Potter</title>
        <author_count>1</author_count>
      </r>
      <r>
        <title>XQuery Kick Start</title>
        <author_count>5</author_count>
      </r>
      <r>
        <title>Learning XML</title>
        <author_count>1</author_count>
      </r>
    </root>