title: How to count occurences of a node in XML using XQuery?
body: <p>Input file:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; 
          &lt;books&gt;
              &lt;book id="6636551"&gt;
                  &lt;master_information&gt;
                      &lt;book_xref&gt;
                          &lt;xref type="Fiction" type_id="1"&gt;72771KAM3&lt;/xref&gt;
                          &lt;xref type="Non_Fiction" type_id="2"&gt;US72771KAM36&lt;/xref&gt;
                      &lt;/book_xref&gt;
                  &lt;/master_information&gt;
                  &lt;book_details&gt;
                      &lt;price&gt;24.95&lt;/price&gt;
                      &lt;publish_date&gt;2000-10-01&lt;/publish_date&gt;
                      &lt;description&gt;An in-depth look at creating applications with XML.&lt;/description&gt;
                  &lt;/book_details&gt;
              &lt;/book&gt;
              &lt;book id="119818569"&gt;
                  &lt;master_information&gt;
                      &lt;book_xref&gt;
                          &lt;xref type="Fiction" type_id="1"&gt;070185UL5&lt;/xref&gt;
                          &lt;xref type="Non_Fiction" type_id="2"&gt;US070185UL50&lt;/xref&gt;
                      &lt;/book_xref&gt;
                  &lt;/master_information&gt;
                  &lt;book_details&gt;
                      &lt;price&gt;19.25&lt;/price&gt;
                      &lt;publish_date&gt;2002-11-01&lt;/publish_date&gt;
                      &lt;description&gt;A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.&lt;/description&gt;
                  &lt;/book_details&gt;
              &lt;/book&gt;
              &lt;book id="119818568"&gt;
                  &lt;master_information&gt;
                      &lt;book_xref&gt;
                          &lt;xref type="Fiction" type_id="1"&gt;070185UK7&lt;/xref&gt;
                          &lt;xref type="Non_Fiction" type_id="2"&gt;US070185UK77&lt;/xref&gt;
                      &lt;/book_xref&gt;
                  &lt;/master_information&gt;
                  &lt;book_details&gt;
                      &lt;price&gt;5.95&lt;/price&gt;
                      &lt;publish_date&gt;2004-05-01&lt;/publish_date&gt;
                      &lt;description&gt;After the collapse of a nanotechnology society in England, the young survivors lay the foundation for a new society.&lt;/description&gt;
                  &lt;/book_details&gt;
              &lt;/book&gt;
              &lt;book id="119818567"&gt;
                  &lt;master_information&gt;
                      &lt;book_xref&gt;
                          &lt;xref type="Fiction" type_id="1"&gt;070185UJ0&lt;/xref&gt;
                          &lt;xref type="Non_Fiction" type_id="2"&gt;US070185UJ05&lt;/xref&gt;
                      &lt;/book_xref&gt;
                  &lt;/master_information&gt;
                  &lt;book_details&gt;
                      &lt;price&gt;4.95&lt;/price&gt;
                      &lt;publish_date&gt;2000-09-02&lt;/publish_date&gt;
                      &lt;description&gt;When Carla meets Paul at an ornithology conference, tempers fly as feathers get ruffled.&lt;/description&gt;
                  &lt;/book_details&gt;
              &lt;/book&gt;
          &lt;/books&gt;
  </code></pre>
  
  <p>I wrote XQuery to display the count of a specific field as follows:</p>
  
  <pre><code>for $x in //book_xref
  let $c := string-join(('name of element:', count($x)), '&amp;#10;')
  return $c
  </code></pre>
  
  <p>The intended output:</p>
  
  <pre><code>name of element: 4
  </code></pre>
  
  <p>but the output comes out as:</p>
  
  <pre><code>name of element:
  1
  name of element:
  1
  name of element:
  1
  name of element:
  1
  </code></pre>
  
  <p>After which I understood why it is doing that. I tried aggregating the count value but that didn't work out. Also, couldn't find any function to fetch the name of the element automatically so that it's automatically included in the string. </p>
  
  <p>Ideally, the target output is</p>
  
  <pre><code>book_xref:4
  </code></pre>
  
  <p>What do I need to achieve that? What am I missing?</p>
  
  <p>Thanks! I appreciate your response. </p>