Home > Mobile >  how to write xml query that returns specific value?
how to write xml query that returns specific value?

Time:07-03

i have this xml code that i wrote ( i am new at xml) , i have a couple of books where each book is identified by its name , and for each book there could be more than one edition , and an edition of a book is identified by the year it was published , and the number of pages of each book is according to the edition , and for each edition there could be more than faculty that has it , where each faculty can has more than one copy from the same edition , where each copy is identified by a book id , and each copy has a maximum time to borrow . i wrote this xml code according to the above , ( i was giving a table with the data)

<books>
    <book Book_Name="Database Systems">
        <edition Year="1998">
            <pages>348</pages>
            <Faculty faculty_name="CS">
                <book_copy Book_ID="1111">
                    <Max_Time>7</Max_Time>
                </book_copy>
                <book_copy Book_ID="1112">
                    <Max_Time>14</Max_Time>
                </book_copy>
            </Faculty>
        </edition>
        <edition Year="2001">
            <pages>424</pages>
            <Faculty faculty_name="CS">
                <book_copy Book_ID="1113">
                    <Max_Time>7</Max_Time>
                </book_copy>
            </Faculty>
        </edition>
    </book>
    <book Book_Name="Database And Knowledge">
        <edition year="1998">
            <pages>390</pages>
            <Faculty faculty_name="CS">
                <book_copy Book_ID="2222">
                    <Max_Time>1</Max_Time>
                </book_copy>
            </Faculty>
            <Faculty faculty_name="EE">
                <book_copy Book_ID="2222">
                    <Max_Time>7</Max_Time>
                </book_copy>
            </Faculty>
        </edition>
    </book>
</books>
    
    

( i am not sure with the code)

my problem is that i want to build a query that returns the name of the books that has a faculty that has more than one copy in the same edition for the book

i have no idea how to write the query , and plz if anyone can assure me that the code is correct !!

CodePudding user response:

You need to traverse the xml, this is a very basic example that will log out the XML of any duplicate Book_ID's with in a Faculty

let xml = `<books>
    <book Book_Name="Database Systems">
        <edition Year="1998">
            <pages>348</pages>
            <Faculty faculty_name="CS">
                <book_copy Book_ID="1111">
                    <Max_Time>7</Max_Time>
                </book_copy>
                <book_copy Book_ID="1112">
                    <Max_Time>14</Max_Time>
                </book_copy>
            </Faculty>
        </edition>
        <edition Year="2001">
            <pages>424</pages>
            <Faculty faculty_name="CS">
                <book_copy Book_ID="1113">
                    <Max_Time>8</Max_Time>
                </book_copy>
            </Faculty>
        </edition>
    </book>
    <book Book_Name="Database And Knowledge">
        <edition year="1998">
            <pages>390</pages>
            <Faculty faculty_name="CS">
                <book_copy Book_ID="2222">
                    <Max_Time>1</Max_Time>
                </book_copy>
            </Faculty>
            <Faculty faculty_name="EE">
                <book_copy Book_ID="2222">
                    <Max_Time>9</Max_Time>
                </book_copy>
            </Faculty>
        </edition>
    </book>
</books>`
let bookIDs = []
let booksWithSameIDs = []
xmlDoc = $.parseXML(xml),
  $xml = $(xmlDoc),
  $xml.find('book').each(function() {
    $(this).find('Faculty').each(function() {
      $(this).find("book_copy").each(function() {
        if (bookIDs.indexOf($(this).attr("Book_ID")) == -1) {
          bookIDs.push($(this).attr("Book_ID"))
        } else {
          booksWithSameIDs.push($(this).parent().parent().html())
        }
      })
    })
  })
console.log(booksWithSameIDs)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

it should give you a starting point, to have a go at

CodePudding user response:

You don't really need jquery, vanilla js can do the trick:

xml = `[your xml above]
`
domdoc = new DOMParser().parseFromString(xml, "text/xml")

books = domdoc.evaluate('//book', domdoc, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
for (let i = 0; i < books.snapshotLength; i  ) {
  let book = books.snapshotItem(i);
  title = domdoc.evaluate('.//edition[count(.//Faculty)>1]/../@Book_Name', book, null, XPathResult.STRING_TYPE, null);
  
}  
console.log(title.stringValue)  

Output: This gives you the name of the book with an edition which is owned by more than one faculty, i.e.,

"Database And Knowledge"
  • Related