How to get the items count in xml using marklogic - xquery

I am new to MarkLogic ..I need to get the total count of books from the following XML. Can anyone suggest me.
<bk:bookstore xmlns:bk="http://www.bookstore.org">
<bk:book category='Computer'>
<bk:author>Gambardella, Matthew</bk:author>
<bk:title>XML Developer's Guide</bk:title>
<bk:price>44.95</bk:price>
<bk:publish_year>1995</bk:publish_year>
<bk:description>An in-depth look at creating applications with XML.
</bk:description>
</bk:book>
<bk:book category='Fantasy'>
<bk:author>Ralls, Kim</bk:author>
<bk:title>Midnight Rain</bk:title>
<bk:price>5.95</bk:price>
<bk:publish_year>2000</bk:publish_year>
<bk:description>A former architect battles corporate zombies, an evil
sorceress, and her own childhood to become queen of the world.
</bk:description>
</bk:book>
<bk:book category='Comic'>
<bk:author>Robert M. Overstreet</bk:author>
<bk:title>The Overstreet Indian Arrowheads Identification </bk:title>
<bk:price>2000</bk:price>
<bk:publish_year>1991</bk:publish_year>
<bk:description>A leading expert and dedicated collector, Robert M.
Overstreet has been writing The Official Overstreet Identification and
Price
Guide to Indian Arrowheads for more than 21 years</bk:description>
</bk:book>
<bk:book category='Comic'>
<bk:author>Randall Fuller</bk:author>
<bk:title>The Book That Changed America</bk:title>
<bk:price>1000</bk:price>
<bk:publish_year>2017</bk:publish_year>
<bk:description>The New York Times Book Review Throughout its history
America has been torn in two by debates over ideals and beliefs.
</bk:description>
</bk:book>
</bk:bookstore>
Can anyone find the solution for this question as I am new to this.

Id suggest using a cts:count-aggregate in combination with cts:element-reference. This requires you to have a element range index on book.
cts:count-aggregate(cts:element-reference(fn:QName("http://www.bookstore.org", "book")))
If performance isn't too critical and your document count isn't too large, you could also count with fn:count.
declare namespace bk="http://www.bookstore.org";
fn:count(//bk:book)

Try this-
declare namespace bk="http://www.bookstore.org";
let $book_xml :=
<bk:bookstore xmlns:bk="http://www.bookstore.org">
</bk:book>
........
........
</bk:book>
</bk:bookstore>
return fn:count($book_xml//bk:book)
Hope That Helps !

Related

Converting a dataframe which contains list into a csv with r

I am new to R and I am facing difficulties to convert my dataframe (named dffinal) which contains list into a csv.
I tried the following code which gave a csv that is not usable:
dput(dffinal, file="out.txt")
new <- source("out.txt")
write.csv2(dffinal,"C:/Users\\final.csv", row.names = FALSE)
I tried all the option but I found nothing! Here is a sample of my dataframe:
dput(head(dffinal[1:2]))
structure(list(V1 = list("I heard about your products and I would like to give it a try but I'm not sure which product is better for my dry skin, Almond products or Shea Butter products? Thank you",
"Hi,\n\nCan you please tell me the difference between the shea shower oil limited edition and the other shower gels? I got a sample of one in a kit that had a purple label on it. (Please see attached photo.) I love it!\nBut, what makes it limited edition, the smell or what? It is out of stock and I was wondering if it is going to be restocked or not?\n\nAlso, what makes it different from the almond one?\n\nThank you for your help.",
"Hello, Have you discontinued Eau de toilette", "I both an eGift card for my sister and she hasn't received anything via her email\n\nPlease advise \n\nThank you \n\n cann",
"I do not get Coco Pillow Mist. yet. When are you going to deliver it? I need it before January 3rd.",
"Hello,\nI wish to follow up on an email I just received from Lol, notifying\nme that I've \"successfully canceled my subscription of bun Complete.\"\nHowever, I didn't request a cancelation and was expecting my next scheduled\nfulfillment later this month. Could you please advise and help? I'd\nappreciate it if you could reinstate my subscription.\n"),
V2 = list("How long can I keep a product before opening it? shea butter original hand cream large size 5oz, i like to buy a lot during sales promotions, is this alright or should i only buy what i'll use immediately, are these natural organic products that will still have a long stable shelf life? thank you",
"Hi,\nI recently checked to see if my order had been delivered, and I only received my gift box and free sample. Can you please send the advent calendar? Does not seem to have been included in the shipping. Thank you",
"Is the gade fragrance still available?", "I previously contacted you because I purchased your raspberry lip scrub. When I opened the scrub, 25% of the product was missing. Your customer service department agreed to send me a replacement, but I never received the replacement rasberry lip scrub. Could you please tell me when I will receive the replacement product? Thanks, me",
"To whom it may concern:\n\nI have 3 items in my order: 1 Shea Butter Intensive Hand Balm and 2 S‚r‚nit‚ Relaxing Pillow Mist. I have just received the hand balm this morning. I was wondering when I would receive the two bottles of pillow mist.\n\nThanks and regards,\n\nMe",
"I have not received 2X Body Scalp Essence or any shipment information regarding these items. Please let me know if and when you will be shipping these items, otherwise please credit my card. Thanks")), row.names = c(NA,
6L), class = "data.frame")
We can do this in tidyverse
library(dplyr)
library(readr)
dffinal %>%
mutate(across(everything(), unlist)) %>%
write_csv('result.csv')
If you have list of only length 1 for all the rows as shared in the example using unlist will work -
dffinal[] <- lapply(dffinal, unlist)
If the length of list is greater than 1 use -
dffinal[] <- lapply(dffinal, sapply, toString)
Write the data with write.csv -
write.csv(dffinal, 'result.csv', row.names = FALSE)

Extracting full article text via the newsanchor package [in R]

I am using the newsanchor package in R to try to extract entire article content via NewsAPI. For now I have done the following :
require(newsanchor)
results <- get_everything(query = "Trump +Trade", language = "en")
test <- results$results_df
This give me a dataframe full of info of (maximum) a 100 articles. These however do not containt the entire actual article text. Rather they containt something like the following:
[1] "Tensions between China and the U.S. ratcheted up several notches over the weekend as Washington sent a warship into the disputed waters of the South China Sea. Meanwhile, Google dealt Huaweis smartphone business a crippling blow and an escalating trade war co… [+5173 chars]"
Is there a way to extract the remaining 5173 chars. I have tried to read the documentation but I am not really sure.
I don't think that is possible at least with free plan. If you go through the documentation at https://newsapi.org/docs/endpoints/everything in the Response object section it says :
content - string
The unformatted content of the article, where available. This is truncated to 260 chars for Developer plan users.
So all the content is restricted to only 260 characters. However, test$url has the link of the source article which you can use to scrape the entire content but since it is being aggregated from various sources I don't think there is one automated way to do this.

lnternational/Domestic flight indicator in Sabre reservation

When using Sabre APIs, is there any reliable indicator available in a Sabre TravelItineraryReadRS (or GetReservation) or other API that indicates whether a flight is international or domestic?
I want to avoid adding complexity and having to maintain a separate list of airport codes and countries if possible, and instead just use an indicator from a response.
I've checked <FlightSegment> in <PTC_FareBreakdown> but nothing seems to indicate internationality:
<tir39:FlightSegment ConnectionInd="O" DepartureDateTime="02-24T13:00" FlightNumber="123" ResBookDesigCode="E" SegmentNumber="1" Status="SS">
<tir39:BaggageAllowance Number="01P"/>
<tir39:FareBasis Code="AFB112"/>
<tir39:MarketingAirline Code="VA" FlightNumber="123"/>
<tir39:OriginLocation LocationCode="BNE"/>
<tir39:ValidityDates>
<tir39:NotValidAfter>2019-02-24</tir39:NotValidAfter>
<tir39:NotValidBefore>2019-02-24</tir39:NotValidBefore>
</tir39:ValidityDates>
</tir39:FlightSegment>
and also checked in <ReservationItems><Item>, e.g.:
<tir39:Item RPH="1">
<tir39:FlightSegment AirMilesFlown="0466" ArrivalDateTime="05-18T14:40" DayOfWeekInd="6" DepartureDateTime="2019-05-18T13:05" SegmentBookedDate="2018-12-21T11:20:00" ElapsedTime="01.35" eTicket="true" FlightNumber="0529" NumberInParty="01" ResBookDesigCode="E" SegmentNumber="0001" SmokingAllowed="false" SpecialMeal="false" Status="HK" StopQuantity="00" IsPast="false" CodeShare="false" Id="123">
<tir39:DestinationLocation LocationCode="SYD" Terminal="TERMINAL 3 DOMESTIC" TerminalCode="3"/>
<tir39:Equipment AirEquipType="21B"/>
<tir39:MarketingAirline Code="QF" FlightNumber="0529">
<tir39:Banner>MARKETED BY QANTAS AIRWAYS</tir39:Banner>
</tir39:MarketingAirline>
<tir39:Meal Code="L"/>
<tir39:OperatingAirline Code="QF" FlightNumber="0529" ResBookDesigCode="E">
<tir39:Banner>OPERATED BY QANTAS AIRWAYS</tir39:Banner>
</tir39:OperatingAirline>
<tir39:OperatingAirlinePricing Code="QF"/>
<tir39:DisclosureCarrier Code="QF" DOT="false">
<tir39:Banner>QANTAS AIRWAYS</tir39:Banner>
</tir39:DisclosureCarrier>
<tir39:OriginLocation LocationCode="BNE" Terminal="DOMESTIC" TerminalCode="D"/>
<tir39:UpdatedArrivalTime>05-18T14:40</tir39:UpdatedArrivalTime>
<tir39:UpdatedDepartureTime>05-18T13:05</tir39:UpdatedDepartureTime>
</tir39:FlightSegment>
</tir39:Item>
and although these have origin/destination airports, neither indicate whether the flight is international or not, and the terminal name is not reliable as an indicator.
<PriceQuotePlus> has a DomesticIntlInd attribute that initially looked useful:
<tir39:PriceQuotePlus DomesticIntlInd="I" PricingStatus="S" VerifyFareCalc="false" ItineraryChanged="false" ...>
but PriceQuotePlus and therefore DomesticIntlInd does not seem to be present in all circumstances. e.g. I have TravelItineraryReadRs responses where there is no PriceQuotePlus element, but still contains ReservationItem/Item/FlightSegment elements that I need to be able to identify as International or Domestic.
Not only this, but as an example, I have a reservation where "DomesticIntlInd" is set to "I" in a reservation that does not have an International flight (it has only one flight, and that flight is domestic (BNE-SYD)).
Any other thoughts on where I might find a reliable international flight indicator or is this functionality simply not available?
Sabre does expose a City Pairs API that includes country codes for each airport, which you could use to infer whether a flight started and ended in the same country.
They also expose this as a list that you could build into your own data table, but the API would probably be more futureproof.
The current file can be found here, but I don't know if that link will work forever.

What does Qt stand for?

I am a fan of Qt and the more i learn about it, the more i love it. I don't know why it is named Qt. I searched it but all of the results where irrelevant. Most were acronyms like "Quality Time" and some were "cutie" or "quiet".
I thought may be it is a funny acronym like "GNU" which is GNU's not UNIX. Either way it is such a cool and cute name. Anybody know about it?
Quote from the first edition of C++ gui programming with Qt 4:
The letter ‘Q’ was chosen as the class prefix because the letter looked beautiful
in Haavard’s Emacs font. The ‘t’ was added to stand for “toolkit”, inspired by
Xt, the X Toolkit.
*Haavard Nord and Eirik Chambe-Eng began the development of Qt
The same piece of history is on wikipedia
"The letter ‘Q’ was chosen as the class prefix because the letter looked beautiful in Haavard’s Emacs font. The ‘t’ was added to stand for “toolkit”, inspired by Xt, the X Toolkit. The company was incorporated on March 4, 1994, originally as Quasar Technologies, then as Troll Tech, and today as Trolltech."
So it should be Quasar Toolkit.
References: 1.https://answers.yahoo.com/question/index?qid=20130907190045AAZZ9uM
2.http://formfull.in/qt

Where does '.' and '..' come from?

What's the story behind our massive repitition of ./foo and cd ... Where do these two . and .. come from? Where could they be seen as a way of navigating a file system tree for the first time?
Excerpt from an interview with Ken Thompson (9-6-89):
Every time we made a directory, by
convention we put it in another
directory called directory -
directory, which was dd. Its name was
dd and that all the users directories
and in fact most other directories,
users maintain their own directory
systems, had pointers back to dd, and
dd got shortened into ‘dot-dot,’ and
dd was for directory-directory.
I'm sorry I can't answer properly, but I can recommend these links:
The Evolution of the Unix Time-sharing System and
A Brief history of the BSD Fast File System
for some interesting historical notes on early file systems and their use.
Other than that, the earliest reference I've found is the 1974 paper "The UNIX Time-Sharing System" by Dennis Ritchie and Ken Thompson. The precursor of UNIX, Multics, also had hierarchical file systems and the notion of relative paths, but according to "Introduction to MULTICS" it used another notation for that.
I believe it was invented by Dennis Ritchie at Bell Labs in the early years of Unix.

Resources