Pragya,
It seems, you want a Cartesian cross-product among any number of elements
inside <result>. Try with below code and this should work for you.
xquery version "1.0-ml";
declare variable $qt :="";
declare function local:transform-into-result($result as node()){
let $elements := fn:distinct-values($result/*/name(.))
let $q := for $e at $i in $elements
let $q := xdmp:set($qt, concat($qt, fn:concat('$token', $e, ",")))
return fn:concat("for ", '$token',$e, " in $result/*[name(.) eq
","'",$e,"'","]")
let $f-q :=($q, "return ", <result>{fn:concat('{(',fn:replace($qt, ',$',
''),')}')}</result>)
return xdmp:value(xdmp:quote($f-q))
};
let $input := <results>
<result>
<G>England and Wales</G>
<G>Latvia</G>
<A>2011-12-21</A>
<C>0000027</C>
<AType>ISDA-MAG-MC02</AType>
<ADate/>
<N>A AUTHORITY</N>
<LEI/>
<AName/>
<PStatus>Active</PStatus>
</result>
<result>
<G>Texas</G>
<A>2014-04-01</A>
<C/>
<AType>ISDA-MAG</AType>
<ADate/>
<N>Erinome, P.L.C.</N>
<N>Perinome, P.L.C.</N>
<N>Terinome, P.L.C.</N>
<N>Gerinome, P.L.C.</N>
<LEI/>
<AName/>
<PStatus>Active</PStatus>
<PStatus>Active</PStatus>
<PStatus>Active</PStatus>
<PStatus>Active</PStatus>
</result>
</results>
let $results :=<results>
{
for $result in $input/result
let $_ := xdmp:set($qt, "")
return local:transform-into-result($result)
}
</results>
return $results
Regards,
Indy
On Fri, May 15, 2015 at 4:28 PM, Arvind Kumar <[email protected]> wrote:
> Hi Pragya,
>
> Below xslt script may help you.
>
>
> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
> xmlns:xs="http://www.w3.org/2001/XMLSchema"
> exclude-result-prefixes="xs" version="2.0">
>
> <xsl:output method="xml"/>
>
>
> <xsl:template match="results">
> <results>
> <xsl:apply-templates/>
> </results>
> </xsl:template>
>
>
> <xsl:template match="result">
>
> <xsl:apply-templates select="Name"/>
>
> </xsl:template>
>
> <xsl:template match="Name">
> <xsl:variable name="law" select=
> "following-sibling::Law"/>
> <xsl:variable name="name" select="."/>
>
> <xsl:for-each select="following-sibling::Law">
> <result>
> <name>
> <xsl:value-of
> select="$name"/>
> </name>
> <Law>
> <xsl:value-of
> select="."/>
> </Law>
> </result>
> </xsl:for-each>
>
> </xsl:template>
>
> </xsl:stylesheet>
>
>
>
> Regards,
> Arvind Kr.
>
>
>
>
>
> On Fri, May 15, 2015 at 4:13 PM, Kapoor, Pragya <[email protected]>
> wrote:
>
>> Hi Raja,
>>
>>
>> The name structure is not mandatory. none of the element is mandatory.
>>
>> <results>
>>
>> <result>
>>
>> <a></a>
>>
>> <a></a> ------ <Name>
>>
>> <b></b> ------ <Law>
>>
>> <c></c> ------- <LEI>
>>
>> <result>
>>
>> </results>
>>
>> or
>>
>>
>> <results>
>>
>> <result>
>>
>> <a></a>
>>
>> <a></a> ------ <City>
>>
>> <b></b> ------ <Law>
>>
>> <c></c> ------- <LEI>
>>
>> <result>
>>
>> </results>
>>
>> or
>>
>>
>> <results>
>>
>> <result>
>>
>> <a></a> ------ <City>
>>
>> <a></a> ------ <City>
>>
>> <b></b> ------ <Law>
>>
>> <b></b> ------ <Law>
>>
>> <c></c> ------- <LEI>
>>
>> <c></c>-----<LEI>
>>
>>
>>
>> <result>
>>
>> </results>
>>
>> an so on...
>>
>>
>> ------------------------------
>> *From:* [email protected] <
>> [email protected]> on behalf of
>> [email protected] <[email protected]>
>> *Sent:* Friday, May 15, 2015 3:58 PM
>>
>> *To:* [email protected]
>> *Subject:* Re: [MarkLogic Dev General] query on xml
>>
>>
>> Hi Pragya,
>>
>>
>>
>> The ‘n’ number of elements in result means – like below structure
>>
>>
>>
>> <results>
>>
>> <result>
>>
>> <a></a>
>>
>> <a></a> ------ <Name>
>>
>> <b></b> ------ <Law>
>>
>> <c></c> ------- <LEI>
>>
>> <result>
>>
>> </result>
>>
>>
>>
>> Am not able to understand , because in below xml also you have only two
>> combinations . IS <name> element is mandatory?
>>
>>
>>
>> Thanks and regards
>>
>> Raja >>>
>>
>>
>>
>> *From:* [email protected] [mailto:
>> [email protected]] *On Behalf Of *Kapoor, Pragya
>> *Sent:* Friday, May 15, 2015 9:35 AM
>> *To:* MarkLogic Developer Discussion
>> *Subject:* Re: [MarkLogic Dev General] query on xml
>>
>>
>>
>> Thanks Indy.
>>
>> But, The input <result> could have 'n' number of dynamic elements.
>> ------------------------------
>>
>> *From:* [email protected] <
>> [email protected]> on behalf of Indrajeet Verma <
>> [email protected]>
>> *Sent:* Thursday, May 14, 2015 6:16 PM
>> *To:* MarkLogic Developer Discussion
>> *Subject:* Re: [MarkLogic Dev General] query on xml
>>
>>
>>
>> Pragya,
>>
>>
>>
>> See if below code works for you.
>>
>>
>>
>> If your input <result> can contain only two elements combination, Try
>> below code changes in Raja's code,
>>
>>
>>
>> declare function local:transform($result as node()){
>>
>>
>>
>> let $elements := fn:distinct-values($result/child::*/name(.))
>>
>> for $token_one in $result/*[name(.) eq ($elements)[1]], $token_two in
>> $result/*[name(.) eq ($elements)[2]]
>>
>> return <result>{($token_one,$token_two)}</result>
>>
>> };
>>
>>
>>
>>
>>
>> Regards,
>>
>> Indy
>>
>>
>>
>> On Thu, May 14, 2015 at 4:42 PM, Kapoor, Pragya <[email protected]>
>> wrote:
>>
>> Yes Raja, I mean two or more elements other than name and law.
>>
>>
>>
>> So my input xml could be:
>>
>>
>>
>> <results>
>>
>> <result>
>>
>> <Name>thg</Name>
>>
>> <LEI>rty</LEI>
>>
>> </result>
>>
>> <result>
>>
>> <Name>gux</Name>
>>
>> <LEI>rty</LEI>
>>
>> </result>
>>
>> <result>
>>
>> <Name>abc</Name>
>>
>> <Name>xyz</Name>
>>
>> <Name>cde</Name>
>>
>> <LEI>fgd</LEI>
>>
>> </result>
>>
>> </results>
>>
>>
>> ------------------------------
>>
>> *From:* [email protected] <
>> [email protected]> on behalf of
>> [email protected] <[email protected]>
>> *Sent:* Thursday, May 14, 2015 3:05 PM
>>
>>
>> *To:* [email protected]
>> *Subject:* Re: [MarkLogic Dev General] query on xml
>>
>>
>>
>> Hi Pragya,
>>
>>
>>
>> You meant, you will get some other element than “ name” and “Law” ? If
>> that is not a case and will get these two elements with different count
>> then will use the same code.
>>
>>
>>
>> Thanks and regards
>>
>> Raja >>>
>>
>>
>>
>> *From:* [email protected] [mailto:
>> [email protected]] *On Behalf Of *Kapoor, Pragya
>> *Sent:* Thursday, May 14, 2015 2:56 PM
>> *To:* [email protected]
>> *Subject:* Re: [MarkLogic Dev General] query on xml
>>
>>
>>
>> Thanks for the solution, but as mentioned in my previous mail,
>>
>> the elements in input xml are dynamic, so I cant use
>>
>>
>>
>> for $name in $result/Name, $law in $result/Law
>>
>> return <result>{($name,$law)}</result>
>>
>>
>>
>> Thanks
>>
>> Pragya
>>
>>
>> ------------------------------
>>
>> *From:* [email protected] <
>> [email protected]> on behalf of
>> [email protected] <[email protected]>
>> *Sent:* Thursday, May 14, 2015 1:39 PM
>> *To:* [email protected]
>> *Subject:* Re: [MarkLogic Dev General] query on xml
>>
>>
>>
>> Hi Pragya,
>>
>>
>>
>> Following code should give the expected result ,
>>
>> xquery version "1.0-ml";
>>
>> declare namespace html = "http://www.w3.org/1999/xhtml";
>>
>>
>>
>> declare function local:transform($result as node()){
>>
>>
>>
>> for $name in $result/Name, $law in $result/Law
>>
>> return <result>{($name,$law)}</result>
>>
>>
>>
>> };
>>
>> let $data := <results>
>>
>> <result>
>>
>> <Name>thg</Name>
>>
>> <Name>rty</Name>
>>
>> <Law>England and Wales</Law>
>>
>> <Law>Latvia</Law>
>>
>> </result>
>>
>> <result>
>>
>> <Name>gux</Name>
>>
>> <Law>India</Law>
>>
>> </result>
>>
>> <result>
>>
>> <Name>abc</Name>
>>
>> <Name>xyz</Name>
>>
>> <Name>cde</Name>
>>
>> <Law>England</Law>
>>
>> </result>
>>
>> </results>
>>
>>
>>
>> let $result :=<results>{ for $result in $data/result
>>
>> return local:transform($result)
>>
>> }</results>
>>
>> return $result
>>
>>
>>
>> Thanks and regards
>>
>> Raja >>>
>>
>>
>>
>>
>>
>> *From:* [email protected] [
>> mailto:[email protected]
>> <[email protected]>] *On Behalf Of *Kapoor, Pragya
>> *Sent:* Thursday, May 14, 2015 1:05 PM
>> *To:* MarkLogic Developer Discussion
>> *Subject:* [MarkLogic Dev General] query on xml
>>
>>
>>
>> Hi,
>>
>>
>>
>> I need to convert the below input to the desired output.
>>
>> The elements in the input xml is dynamic(it could be one
>> element,two,three n so on, not fixed).
>>
>>
>>
>> Input:
>>
>>
>>
>>
>>
>> <results>
>>
>> <result>
>>
>> <Name>thg</Name>
>>
>> <Name>rty</Name>
>>
>> <Law>England and Wales</Law>
>>
>> <Law>Latvia</Law>
>>
>> </result>
>>
>> <result>
>>
>> <Name>gux</Name>
>>
>> <Law>India</Law>
>>
>> </result>
>>
>> <result>
>>
>> <Name>abc</Name>
>>
>> <Name>xyz</Name>
>>
>> <Name>cde</Name>
>>
>> <Law>England</Law>
>>
>> </result>
>>
>> </results>
>>
>>
>>
>> desired output:
>>
>>
>>
>> <results>
>>
>> <result>
>>
>> <Name>thg</Name>
>>
>> <Law>England and Wales</Law></result>
>>
>> <result>
>>
>> <Name>thg</Name>
>>
>> <Law>Latvia</Law></result>
>>
>> <result>
>>
>> <Name>rty</Name>
>>
>> <Law>England and Wales</Law></result>
>>
>> <result>
>>
>> <Name>rty</Name>
>>
>> <Law>Latvia</Law></result>
>>
>> <result>
>>
>> <Name>gux</Name>
>>
>> <Law>India</Law>
>>
>> </result>
>>
>> <result>
>>
>> <Name>abc</Name>
>>
>> <Law>England</Law>
>>
>> </result>
>>
>> <result>
>>
>> <Name>xyz</Name>
>>
>> <Law>England</Law>
>>
>> </result>
>>
>> <result>
>>
>> <Name>cde</Name>
>>
>> <Law>England</Law>
>>
>> </result>
>>
>> </results>
>>
>>
>>
>> Thanks
>>
>> Pragya
>>
>>
>>
>> "This e-mail and any attachments transmitted with it are for the sole use
>> of the intended recipient(s) and may contain confidential , proprietary or
>> privileged information. If you are not the intended recipient, please
>> contact the sender by reply e-mail and destroy all copies of the original
>> message. Any unauthorized review, use, disclosure, dissemination,
>> forwarding, printing or copying of this e-mail or any action taken in
>> reliance on this e-mail is strictly prohibited and may be unlawful."
>>
>> This e-mail and any files transmitted with it are for the sole use of the
>> intended recipient(s) and may contain confidential and privileged
>> information. If you are not the intended recipient(s), please reply to the
>> sender and destroy all copies of the original message. Any unauthorized
>> review, use, disclosure, dissemination, forwarding, printing or copying of
>> this email, and/or any action taken in reliance on the contents of this
>> e-mail is strictly prohibited and may be unlawful. Where permitted by
>> applicable law, this e-mail and other e-mail communications sent to and
>> from Cognizant e-mail addresses may be monitored.
>>
>> "This e-mail and any attachments transmitted with it are for the sole use
>> of the intended recipient(s) and may contain confidential , proprietary or
>> privileged information. If you are not the intended recipient, please
>> contact the sender by reply e-mail and destroy all copies of the original
>> message. Any unauthorized review, use, disclosure, dissemination,
>> forwarding, printing or copying of this e-mail or any action taken in
>> reliance on this e-mail is strictly prohibited and may be unlawful."
>>
>> This e-mail and any files transmitted with it are for the sole use of the
>> intended recipient(s) and may contain confidential and privileged
>> information. If you are not the intended recipient(s), please reply to the
>> sender and destroy all copies of the original message. Any unauthorized
>> review, use, disclosure, dissemination, forwarding, printing or copying of
>> this email, and/or any action taken in reliance on the contents of this
>> e-mail is strictly prohibited and may be unlawful. Where permitted by
>> applicable law, this e-mail and other e-mail communications sent to and
>> from Cognizant e-mail addresses may be monitored.
>>
>> "This e-mail and any attachments transmitted with it are for the sole use
>> of the intended recipient(s) and may contain confidential , proprietary or
>> privileged information. If you are not the intended recipient, please
>> contact the sender by reply e-mail and destroy all copies of the original
>> message. Any unauthorized review, use, disclosure, dissemination,
>> forwarding, printing or copying of this e-mail or any action taken in
>> reliance on this e-mail is strictly prohibited and may be unlawful."
>>
>>
>> _______________________________________________
>> General mailing list
>> [email protected]
>> Manage your subscription at:
>> http://developer.marklogic.com/mailman/listinfo/general
>>
>>
>>
>> "This e-mail and any attachments transmitted with it are for the sole use
>> of the intended recipient(s) and may contain confidential , proprietary or
>> privileged information. If you are not the intended recipient, please
>> contact the sender by reply e-mail and destroy all copies of the original
>> message. Any unauthorized review, use, disclosure, dissemination,
>> forwarding, printing or copying of this e-mail or any action taken in
>> reliance on this e-mail is strictly prohibited and may be unlawful."
>> This e-mail and any files transmitted with it are for the sole use of
>> the intended recipient(s) and may contain confidential and privileged
>> information. If you are not the intended recipient(s), please reply to the
>> sender and destroy all copies of the original message. Any unauthorized
>> review, use, disclosure, dissemination, forwarding, printing or copying of
>> this email, and/or any action taken in reliance on the contents of this
>> e-mail is strictly prohibited and may be unlawful. Where permitted by
>> applicable law, this e-mail and other e-mail communications sent to and
>> from Cognizant e-mail addresses may be monitored.
>> "This e-mail and any attachments transmitted with it are for the sole
>> use of the intended recipient(s) and may contain confidential , proprietary
>> or privileged information. If you are not the intended recipient, please
>> contact the sender by reply e-mail and destroy all copies of the original
>> message. Any unauthorized review, use, disclosure, dissemination,
>> forwarding, printing or copying of this e-mail or any action taken in
>> reliance on this e-mail is strictly prohibited and may be unlawful."
>>
>> _______________________________________________
>> General mailing list
>> [email protected]
>> Manage your subscription at:
>> http://developer.marklogic.com/mailman/listinfo/general
>>
>>
>
> _______________________________________________
> General mailing list
> [email protected]
> Manage your subscription at:
> http://developer.marklogic.com/mailman/listinfo/general
>
>
_______________________________________________
General mailing list
[email protected]
Manage your subscription at:
http://developer.marklogic.com/mailman/listinfo/general