David K
David K

Reputation: 213

Finding unique local-names of nodes (as well as unique node structure)

I'm trying to dynamically sense the structure of an xml file to determine unique nodes present

i.e. if I had

<?xml version="1.0" encoding="UTF-8"?>
<nsX:bookstore xmlns:nsX="http://namespace1" xmlns:nsY="http://namespace2">
  <nsX:bookList>
    <nsX:book category="COOKING">
      <nsX:title lang="en">Everyday Italian</nsX:title>
      <nsY:stock>1</nsY:stock>
    </nsX:book>
    <nsX:book category="CHILDREN">
      <nsX:title lang="en">Harry Potter</nsX:title>
      <nsX:edition lang="en">2</nsX:edition>
      <nsY:stock>0</nsY:stock>
    </nsX:book>
    <nsX:cd category="WEB">
      <nsX:albumTitle lang="en">XQuery Kick Start</nsX:albumTitle >
      <nsY:stock>1</nsY:stock>
    </nsX:cd>
    <nsX:tape category="WEB">
      <nsX:title lang="en">Learning XML</nsX:title>
      <nsY:year>1992</nsY:year>
    </nsX:tape>
  </nsX:bookList>
</nsX:bookstore>

and wanted to generate a list of my unique types of objects

book
cd
tape

I know how to find distinct-values of nodes, but what I need is to find distinct-local-names of nodes (which I am not aware of a command for).

I can use a loop to return the local name of each item, but that returns each local name separately making it impossible for me to use the distinct-values function.

I can use programmatic logic to handle the results of my xQuery, but I would much prefer to just return the correct information. Any advice would be greatly appreciated.

Ultimately, I would like to also be able to determine unique structure of the child nodes so that I could see that I have 2 different types of books (one with a title and one with an addition ) as well as a cd and a tape. It is easy enough to check to see if these things exist, but if I didn't know what I was looking for and I wanted to identify the unique types of objects based on their node structure, I would need to know how to find that.

Upvotes: 0

Views: 272

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167696

It is as easy as distinct-values(/*/*/*/local-name()).

Upvotes: 3

Related Questions