Solved

xquery - how to use a recurring value only once

Posted on 2008-10-26
3
366 Views
Last Modified: 2013-11-11
I have a XML like this

<root>
<records>
<action id="1" site="A" item="abs"/>
<action id="2" site="A" item="hgt"/>
<action id="3" site="A" item="98uuj"/>
</records>
</root>

I want to make it into this one, where the site attribute will only appear once.
must use XQuery ...

<actions>
<site>A</site>
<items>
<item>
<id>1</id>
<code>abs</code>
</item>
<item>
<id>2</id>
<code>hgt</code>
</item>
<item>
<id>3</id>
<code>98uuj</code>
</item>
</items>
</actions>

0
Comment
Question by:shalomc
  • 2
3 Comments
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
ID: 22807208
mmh, it sounds like this is more a task for XSLT instead of XQuery, but here you go
let $doc := doc("file:///d:/untitled1.xml")
return
<actions>
    {
        for $a in distinct-values($doc//action/@site)
        return 
            <site>{$a}</site>
    }
    {
        for $a in distinct-values($doc//action/@site)
        return 
            <items>
                {
                for $b in $doc//action
                where $b/@site = $a
                return
                    <item>
                        <id>{string($b/@id)}</id>
                        <code>{string($b/@item)}</code>
                    </item>
                }
               </items>
    }
</actions>

Open in new window

0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 22807221
in case you are interested, this is the XSLT you would need
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output indent="yes"/>
    <xsl:template match="records">
        <actions>
            <xsl:for-each-group select="action" group-by="@site">
                <site><xsl:value-of select="@site"/></site>
                <items>
                    <xsl:for-each select="current-group()">
                        <item>
                            <id><xsl:value-of select="@id"/></id>
                            <code><xsl:value-of select="@item"/></code>
                        </item>
                    </xsl:for-each>
                </items>
            </xsl:for-each-group>
        </actions>
    </xsl:template>
</xsl:stylesheet>

Open in new window

0
 
LVL 33

Author Comment

by:shalomc
ID: 22807500
the distinct-value had me going :)
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Trouble parsing soap xml result 3 49
Formatting issues in XSL FO 3 37
SQL XML ALL Nodes Compare in function 2 25
Xsl-fo show PDF when no results are returned 2 18
The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now