Shalini
Shalini

Reputation: 348

How to wrap several nodes within a XML document to a new node using XQuery?

I want to Wrap multiple nodes (specific nodes) into a new single node within my xml document and then want to insert it.

Example XML Document-

<root>
  <value1>somevalue</value1>
  <value2>somevalue</value2>
  <value3>somevalue</value3>
  <value4>somevalue</value4>
  <value5>Australia</value5>
  <value6>India</value6>
  <value7>USA</value7>
  <value8>somevalue</value8>
  <value9>somevalue</value9>
  <value10>somevalue</value10>
</root>

Since my value5 to value7 are name of the countries, i want to put them at the same father node. The output need to look like this:

Output-

<root>
  <value1>somevalue</value1>
  <value2>somevalue</value2>
  <value3>somevalue</value3>
  <value4>somevalue</value4>
  <Country>
    <value5>Australia</value5>
    <value6>India</value6>
    <value7>USA</value7>
  </Country>
  <value8>somevalue</value8>
  <value9>somevalue</value9>
  <value10>somevalue</value10>
</root>

Similarly if my other values belong to some other fields/properties then i want to WRAP them in a new single node.

Any Suggestions?

Upvotes: 1

Views: 660

Answers (2)

Martin Honnen
Martin Honnen

Reputation: 167716

For adjacent elements you could a tumbling window clause https://www.w3.org/TR/xquery-31/#id-tumbling-windows:

declare variable $countries as xs:string* := ('Australia', 'India', 'USA');

<root>
{
    for tumbling window $w in root/*
    start $s when true()
    end next $n when ($s = $countries) and not($n = $countries) or (not($s = $countries) and $n = $countries)
    return 
        if ($w[1] = $countries)
        then <Country>
              {$w}
            </Country>
        else $w
}
</root>

https://xqueryfiddle.liberty-development.net/gWcDMeh/2

If you want to wrap based on element names then with a window clause you could use

declare variable $countries as xs:QName* := (QName('', 'value5'), QName('', 'value6'), QName('', 'value7'));

<root>
{
    for tumbling window $w in root/*
    start $s when true()
    end next $n 
       when ($s/node-name() = $countries) and not($n/node-name() = $countries)
            or (not($s/node-name() = $countries) and $n/node-name() = $countries)
    return 
        if ($s/node-name() = $countries)
        then <Country>
              {$w}
            </Country>
        else $w
}
</root>

https://xqueryfiddle.liberty-development.net/gWcDMeh/6

I have now also tried to avoid the use of the window clause and instead implement the wrapping with a recursive function:

declare variable $countries as xs:string* := ('Australia', 'India', 'USA');

declare function local:wrap($seq as item()*, $wrapper as element()) as item()*
{
  let $first-item := head($seq)
  return
    if (not($first-item))
    then (if (empty($wrapper/node())) then () else $wrapper)
    else if (not($first-item[. = $countries]))
    then 
      (if (empty($wrapper/node())) then () else $wrapper, 
       $first-item, 
       local:wrap(tail($seq), $wrapper!element {node-name()} {})
      )
    else local:wrap(tail($seq), $wrapper!element {node-name()} { node(), $first-item})
};

<root>
{
    local:wrap(root/*, <countries/>)
}
</root>

Seems to do the job as well at https://xqueryfiddle.liberty-development.net/gWcDMeh/4, I have no idea whether it makes sense with Marklogic. If you want to wrap based on the elements names and not values then you can adapt the code to https://xqueryfiddle.liberty-development.net/gWcDMeh/5 which declares

declare variable $countries as xs:QName* := (QName('', 'value5'), QName('', 'value6'), QName('', 'value7'));

and then compares else if (not($first-item/node-name() = $countries)).

If you only need to wrap all value5, value6, value7 elements then I think you can simply use

/root/<root>
{
    let $values := (value5, value6, value7)
    return ( 
        * except $values, 
        if ($values) then <countries>{ $values }</countries> else ()
    )

}
</root>

https://xqueryfiddle.liberty-development.net/gWcDMeh/7

Upvotes: 3

Mads Hansen
Mads Hansen

Reputation: 66781

You could achieve what you are trying to do in XSLT, using xsl:for-each-group.

If you want to group them when the value is not equal to "somevalue", then you could use group-adjacent to test whether the element value is equal to "somevalue" or not, and then wrap those that are not in the <country> element.

You can execute an XSLT within your XQuery module in MarkLogic, like this:

xquery version "1.0-ml";
declare variable $doc := document {
<root>
  <value1>somevalue</value1>
  <value2>somevalue</value2>
  <value3>somevalue</value3>
  <value4>somevalue</value4>
  <value5>Australia</value5>
  <value6>India</value6>
  <value7>USA</value7>
  <value8>somevalue</value8>
  <value9>somevalue</value9>
  <value10>somevalue</value10>
</root>
};

declare variable $grouping-xslt :=
  <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes" />

    <xsl:template match="root">
        <xsl:copy>
            <xsl:for-each-group select="*" group-adjacent=". = 'somevalue'">
                <xsl:choose>
                    <xsl:when test="current-grouping-key()">
                        <xsl:copy-of select="current-group()"/>
                    </xsl:when>
                    <xsl:otherwise>
                        <country>
                            <xsl:copy-of select="current-group()"/>
                        </country>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:for-each-group>        
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>;

xdmp:xslt-eval($grouping-xslt, $doc)

If you have a known sequence of country names that you would want to group by, then you could do that with group-by and test whether the value matches any of the country names:

xquery version "1.0-ml";
declare variable $doc := document {
<root>
  <value1>somevalue</value1>
  <value2>somevalue</value2>
  <value3>somevalue</value3>
  <value4>somevalue</value4>
  <value5>Australia</value5>
  <value6>India</value6>
  <value7>USA</value7>
  <value8>somevalue</value8>
  <value9>somevalue</value9>
  <value10>somevalue</value10>
</root>
};

declare variable $grouping-xslt :=
  <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes" />
    <xsl:param name="countries" />
    <xsl:template match="root">
        <xsl:copy>
            <xsl:for-each-group select="*" group-by=". = $countries">
                <xsl:choose>
                    <xsl:when test="current-grouping-key()">
                        <country>
                            <xsl:copy-of select="current-group()"/>
                        </country>
                    </xsl:when>
                    <xsl:otherwise>
                         <xsl:copy-of select="current-group()"/>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:for-each-group>        
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>;

declare variable $params := map:new(map:entry("countries", ("Australia", "India", "USA")));

xdmp:xslt-eval($grouping-xslt, $doc, $params)

Upvotes: 1

Related Questions