[xquery-talk] How to invert the hierarchy of an xml?
Peng Yu
pengyu.ut at gmail.com
Wed Nov 2 20:59:32 PST 2011
On Wed, Nov 2, 2011 at 10:38 PM, Joe Wicentowski <joewiz at gmail.com> wrote:
> Hi Peng,
>
> Welcome to the XQuery talk mailing list.
>
>> Suppose that I have an xml where each Article has it is own Journals.
>> But since the Journals may be the same. I'd like to group the articles
>> by Journals. I'm still learning XQuery. Could anybody show me how to
>> do the conversion using XQuery?
>
> I suggest making use of XQuery 3.0's "group by" clause. A nice
> example of "group by" happens to be on the eXist-db blog, describing
> exist-db's support for this clause
> (http://atomic.exist-db.org/HowTo/XQuery3/GroupByClause) and providing
> an example that is quite similar to yours, sans the need to filter out
> the Journal child of the Article element in the results. Chances are
> folks will suggest more elegant approaches to that portion of the
> code, but this query does the trick. Below is the code for your case.
>
> Cheers,
> Joe
>
>
> xquery version "3.0";
Is the above line necessary? I use the following command, where
main.xq include all the XQuery code except the above line. Everything
works fine. (httpclient.sh is from eXist-db)
httpclient.pl -u admin -p FyDi3QfBON -q < main.xq
Now suppose, that I have the elements Title and TitleLong in Journal.
I want to group by both Title and TitleLong. There are a number of
things that are different from the simpler case. I'm not familiar with
the syntax enough to know what I should use. Would you please let me
know what XQuery I should use to do the transformation? Thanks!
<Library>
<Article>
<ArticleTitle>title1</ArticleTitle>
<Author>author1</Author>
<Journal>
<Title>journal1</Title>
<TitleLong>journal1long</TitleLong>
<Issue>i1</Issue>
</Journal>
</Article>
<Article>
<ArticleTitle>title2</ArticleTitle>
<Author>author2</Author>
<Journal>
<Title>journal1</Title>
<TitleLong>journal1long</TitleLong>
<Issue>i2</Issue>
</Journal>
</Article>
<Article>
<ArticleTitle>title3</ArticleTitle>
<Author>author3</Author>
<Journal>
<Title>journal2</Title>
<TitleLong>journal2long</TitleLong>
<Issue>i3</Issue>
</Journal>
</Article>
<Article>
<ArticleTitle>title4</ArticleTitle>
<Author>author4</Author>
<Journal>
<Title>journal2</Title>
<TitleLong>journal2long</TitleLong>
<Issue>i4</Issue>
</Journal>
</Article>
</Library>
=========I want it to be converted to this
<Library>
<Journal title="journal1" title-long="journal1long">
<Article>
<ArticleTitle>title1</ArticleTitle>
<Author>author1</Author>
<Issue>i1</Issue>
</Article>
<Article>
<ArticleTitle>title2</ArticleTitle>
<Author>author2</Author>
<Issue>i2</Issue>
</Article>
</Journal>
<Journal title="journal2" title-long="journal2long">
<Article>
<ArticleTitle>title3</ArticleTitle>
<Author>author3</Author>
<Issue>i3</Issue>
</Article>
<Article>
<ArticleTitle>title4</ArticleTitle>
<Author>author4</Author>
<Issue>i4</Issue>
</Article>
</Journal>
</Library>
--
Regards,
Peng
More information about the talk
mailing list