I have a xml structure like this:
<nc>
<mod>
<grp id="d0e9f2f84">
<des>GROUP_33</des>
<num>--33</num>
<seq>33</seq>
<des2>GROUP_33</des2>
<art>
<num>15-33-101-00678</num>
</art>
</grp>
<art>
<num>15-36-072-00000</num>
</art>
<art>
<num>13-42-099-30002</num>
</art>
<art>
<num>15-42-001-45331</num>
</art>
</mod>
<mod>
<grp id="d0e9f2f84">
<des>GROUP_33</des>
<num>--33</num>
<seq>33</seq>
<des2>GROUP_33</des2>
<art>
<num>35-33-101-40678</num>
</art>
</grp>
<art>
<num>11-42-045-41002</num>
</art>
<art>
<num>12-42-101-46331</num>
</art>
</mod>
</nc>
Normally the correct structure is /nc/mod/grp/art but sometimes i have articles (art) that are orphans such as /nc/mod/art.
I am trying to automatise the following. When i have orphan articles, insert a node group (grp) and depending on the second sequence of the articles number (num) for example ([0-9]{2}-42) insert a grp node.
My try with this code snipet:
let $input:=doc("file:/C:/Users/FOB/Documents/git_documentation/private/test_mod_art.xml")
return
for $ii in $input//mod/art[not(ancestor::grp)] return
if ($ii[matches(num, '^[0-5][0-9]-36')]) then
insert node <grp id="d0e1f2f21"><des>GROUP_36</des><num>--36</num><seq>36</seq><des2>GROUP_36</des2></grp> before $ii
else if ($ii[matches(num, '^[0-5][0-9]-42')]) then
insert node <grp id="d0e1f2f23"><des>GROUP_42</des><num>--42</num><seq>42</seq><des2>GROUP_42</des2></grp> before $ii
else
()
It works, but this code doesn’t take into account the fact, that sometimes we have more than one node who matches ‘^[0-5][0-9]-42’, for example.
So i need to detect those and group it before insertion of a group node. I have read some articles with the function distinct-values (at the moment my saxon processor doesn’t accept the group by clause), tried some code, but unfortunately I don’t have enough knowledge to be able to understand it.
How can i achieve this, could some friendly souls give a try specially so that I can understand?
Thank you!
Expected result:
<nc>
<mod>
<grp id="d0e9f2f84">
<des>GROUP_33</des>
<num>--33</num>
<seq>33</seq>
<des2>GROUP_33</des2>
<art>
<num>15-33-101-00678</num>
</art>
</grp>
<grp id="d0e9f2f85">
<des>GROUP_36</des>
<num>--36</num>
<seq>36</seq>
<des2>GROUP_36</des2>
</grp>
<art>
<num>15-36-072-00000</num>
</art>
<grp id="d0e9f2f33">
<des>GROUP_42</des>
<num>--42</num>
<seq>42</seq>
<des2>GROUP_42</des2>
</grp>
<art>
<num>13-42-099-30002</num>
</art>
<art>
<num>15-42-001-45331</num>
</art>
</mod>