Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8976951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:15:49+00:00 2026-06-15T19:15:49+00:00

I am new to xquery and I am looking for a quick solution to

  • 0

I am new to xquery and I am looking for a quick solution to this problem.

I have an xml document, and I want to find all pairs of first cousins and all pairs of 2nd cousins in the documents.

Please have a look a the the xml below and guide me how to proceed or if you can provide any code snippet that will be highly appricieated.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE GEDCOM SYSTEM "file:/Users/indiwarafernando/Downloads/family.dtd">
<GEDCOM>
    <HeaderRec>
        <FileCreation Date=""></FileCreation>
        <Submitter>
            <Link Target="" Ref="FM001"/> 
        </Submitter>
    </HeaderRec>
    <FamilyRec Id="FM001">
        <HusbFath>
            <Link Target="IndividualRec" Ref="IN001"/>
        </HusbFath>
        <WifeMoth>
            <Link Target="IndividualRec" Ref="IN002"/>
        </WifeMoth>
        <Child>
            <Link Target="IndividualRec" Ref="IN004"/>
        </Child>

        <Child>
            <Link Target="IndividualRec" Ref="IN007"/>
        </Child>

    </FamilyRec>


    <FamilyRec Id="FM002">
        <HusbFath>
            <Link Target="IndividualRec" Ref="IN004"/>
        </HusbFath>
        <WifeMoth>
            <Link Target="IndividualRec" Ref="IN005"/>
        </WifeMoth>
        <Child>
            <Link Target="IndividualRec" Ref="IN006"/>
        </Child>

    </FamilyRec>  



    <FamilyRec Id="FM003">
        <HusbFath>
            <Link Target="IndividualRec" Ref="IN007"/>
        </HusbFath>
        <WifeMoth>
            <Link Target="IndividualRec" Ref="IN008"/>
        </WifeMoth>
        <Child>
            <Link Target="IndividualRec" Ref="IN009"/>
        </Child>

    </FamilyRec> 

    <IndividualRec Id="IN001">
        <IndivName>Fathers name</IndivName>                 <!-- This tag used for father of c1 or husband of w1 -->
        <Gender>Male</Gender>
        <AssocIndiv>
            <Link Target="" Ref="IN002"/>
            <Association>Wife</Association>
        </AssocIndiv>
    </IndividualRec>

    <IndividualRec Id="IN002">                                 <!-- This tag used for mother of c1 or wife of h1 -->
        <IndivName>Mother s name</IndivName>
        <Gender>Male</Gender>
        <AssocIndiv>
            <Link Target="IndividualRec" Ref="IN001"/>
            <Association>Husband</Association>
        </AssocIndiv>
    </IndividualRec>

    <IndividualRec Id="IN003">                                  <!-- This tag used for a child to h1 or w1 or grand son to gm1 or gf1 -->
        <IndivName>Child 1 name</IndivName>
        <Gender>Male</Gender>
        <AssocIndiv>
            <Link Target="IndividualRec" Ref="IN001"/>
            <Association>Father</Association>
        </AssocIndiv>
        <AssocIndiv>
            <Link Target="IndividualRec" Ref="IN002"/>
            <Association>Mother</Association>
        </AssocIndiv>
    </IndividualRec>




    <IndividualRec Id="IN005">                                 <!-- This tag used for grand mother -->
        <IndivName>Grand mother's name</IndivName>
        <Gender>Male</Gender>
        <AssocIndiv>
            <Link Target="IndividualRec" Ref="IN004"/>
            <Association>Husband</Association>
        </AssocIndiv>
    </IndividualRec>

    <IndividualRec Id="IN004">                                  <!-- This tag used for grand father -->
        <IndivName>Grand father's name</IndivName>
        <Gender>Male</Gender>
        <AssocIndiv>
            <Link Target="IndividualRec" Ref="IN005"/>
            <Association>Grand Father's Wife</Association>
        </AssocIndiv>
    </IndividualRec>

    <IndividualRec Id="IN006">                                  <!-- This tag used for a child to h1 or w1 or grand son to gm1 or gf1 -->
        <IndivName>Child 1 name</IndivName>
        <Gender>Male</Gender>
        <AssocIndiv>
            <Link Target="IndividualRec" Ref="IN004"/>
            <Association>Father</Association>
        </AssocIndiv>
        <AssocIndiv>
            <Link Target="IndividualRec" Ref="IN005"/>
            <Association>Mother</Association>
        </AssocIndiv>
    </IndividualRec>



    <IndividualRec Id="IN007">                                 <!-- This tag used for grand mother -->
        <IndivName>Grand mother's name</IndivName>
        <Gender>Female</Gender>
        <AssocIndiv>
            <Link Target="IndividualRec" Ref="IN008"/>
            <Association>Husband</Association>
        </AssocIndiv>
    </IndividualRec>

    <IndividualRec Id="IN008">                                  <!-- This tag used for grand father -->
        <IndivName>Grand father's name</IndivName>
        <Gender>Male</Gender>
        <AssocIndiv>
            <Link Target="IndividualRec" Ref="IN007"/>
            <Association>Grand Father's Wife</Association>
        </AssocIndiv>
    </IndividualRec>

    <IndividualRec Id="IN009">                                  <!-- This tag used for a child to h1 or w1 or grand son to gm1 or gf1 -->
        <IndivName>Child 1 name</IndivName>
        <Gender>Male</Gender>
        <AssocIndiv>
            <Link Target="IndividualRec" Ref="IN007"/>
            <Association>Father</Association>
        </AssocIndiv>
        <AssocIndiv>
            <Link Target="IndividualRec" Ref="IN008"/>
            <Association>Mother</Association>
        </AssocIndiv>
    </IndividualRec>



</GEDCOM>

*Example Output: *
An example output of a pair of first cousin is
IN006 and IN009

Regards
Fareed

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T19:15:50+00:00Added an answer on June 15, 2026 at 7:15 pm

    I. This XSLT 2.0 transformation (can easily be re-written into XSLT 1.0) produces all first cousins:

    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:xs="http://www.w3.org/2001/XMLSchema"
     xmlns:my="my:my" exclude-result-prefixes="my xs">
     <xsl:output method="text"/>
    
     <xsl:key name="kChildren" match="Child"
      use="../HusbFath/*/@Ref"/>
    
     <xsl:key name="kChildren" match="Child"
      use="../WifeMoth/*/@Ref"/>
    
     <xsl:variable name="vDoc" select="/"/>
    
     <xsl:template match="FamilyRec">
      <xsl:variable name="vGrandChildren" select=
       "my:GrandChildren((HusbFath|WifeMoth)
                                [string(*/@Ref)][1]
                                  /*/@Ref
                         )
       "/>
       <xsl:sequence select=
        "for $first in 1 to count($vGrandChildren),
             $second in $first+1 to count($vGrandChildren),
             $grand1 in $vGrandChildren[$first],
             $grand2 in $vGrandChildren[$second]
          return
             if(not($grand1/.. is $grand2/..))
               then (concat('[', $grand1/*/@Ref, ',', $grand2/*/@Ref, '] '))
               else ()
        "/>
     </xsl:template>
    
     <xsl:template match="text()"/>
    
     <xsl:function name="my:GrandChildren">
      <xsl:param name="pRef" as="xs:string?"/>
    
       <xsl:sequence select=
       "key('kChildren',
            key('kChildren', $pRef, $vDoc)/Link/@Ref,
            $vDoc)
       "/>
     </xsl:function>
    </xsl:stylesheet>
    

    When this transformation is applied to the provided XML document:

    <GEDCOM>
        <HeaderRec>
            <FileCreation Date=""></FileCreation>
            <Submitter>
                <Link Target="" Ref="FM001"/>
            </Submitter>
        </HeaderRec>
        <FamilyRec Id="FM001">
            <HusbFath>
                <Link Target="IndividualRec" Ref="IN001"/>
            </HusbFath>
            <WifeMoth>
                <Link Target="IndividualRec" Ref="IN002"/>
            </WifeMoth>
            <Child>
                <Link Target="IndividualRec" Ref="IN004"/>
            </Child>
            <Child>
                <Link Target="IndividualRec" Ref="IN007"/>
            </Child>
        </FamilyRec>
        <FamilyRec Id="FM002">
            <HusbFath>
                <Link Target="IndividualRec" Ref="IN004"/>
            </HusbFath>
            <WifeMoth>
                <Link Target="IndividualRec" Ref="IN005"/>
            </WifeMoth>
            <Child>
                <Link Target="IndividualRec" Ref="IN006"/>
            </Child>
        </FamilyRec>
        <FamilyRec Id="FM003">
            <HusbFath>
                <Link Target="IndividualRec" Ref="IN007"/>
            </HusbFath>
            <WifeMoth>
                <Link Target="IndividualRec" Ref="IN008"/>
            </WifeMoth>
            <Child>
                <Link Target="IndividualRec" Ref="IN009"/>
            </Child>
        </FamilyRec>
        <IndividualRec Id="IN001">
            <IndivName>Fathers name</IndivName>
            <!-- This tag used for father of c1 or husband of w1 -->
            <Gender>Male</Gender>
            <AssocIndiv>
                <Link Target="" Ref="IN002"/>
                <Association>Wife</Association>
            </AssocIndiv>
        </IndividualRec>
        <IndividualRec Id="IN002">
            <!-- This tag used for mother of c1 or wife of h1 -->
            <IndivName>Mother s name</IndivName>
            <Gender>Male</Gender>
            <AssocIndiv>
                <Link Target="IndividualRec" Ref="IN001"/>
                <Association>Husband</Association>
            </AssocIndiv>
        </IndividualRec>
        <IndividualRec Id="IN003">
            <!-- This tag used for a child to h1 or w1 or grand son to gm1 or gf1 -->
            <IndivName>Child 1 name</IndivName>
            <Gender>Male</Gender>
            <AssocIndiv>
                <Link Target="IndividualRec" Ref="IN001"/>
                <Association>Father</Association>
            </AssocIndiv>
            <AssocIndiv>
                <Link Target="IndividualRec" Ref="IN002"/>
                <Association>Mother</Association>
            </AssocIndiv>
        </IndividualRec>
        <IndividualRec Id="IN005">
            <!-- This tag used for grand mother -->
            <IndivName>Grand mother's name</IndivName>
            <Gender>Male</Gender>
            <AssocIndiv>
                <Link Target="IndividualRec" Ref="IN004"/>
                <Association>Husband</Association>
            </AssocIndiv>
        </IndividualRec>
        <IndividualRec Id="IN004">
            <!-- This tag used for grand father -->
            <IndivName>Grand father's name</IndivName>
            <Gender>Male</Gender>
            <AssocIndiv>
                <Link Target="IndividualRec" Ref="IN005"/>
                <Association>Grand Father's Wife</Association>
            </AssocIndiv>
        </IndividualRec>
        <IndividualRec Id="IN006">
            <!-- This tag used for a child to h1 or w1 or grand son to gm1 or gf1 -->
            <IndivName>Child 1 name</IndivName>
            <Gender>Male</Gender>
            <AssocIndiv>
                <Link Target="IndividualRec" Ref="IN004"/>
                <Association>Father</Association>
            </AssocIndiv>
            <AssocIndiv>
                <Link Target="IndividualRec" Ref="IN005"/>
                <Association>Mother</Association>
            </AssocIndiv>
        </IndividualRec>
        <IndividualRec Id="IN007">
            <!-- This tag used for grand mother -->
            <IndivName>Grand mother's name</IndivName>
            <Gender>Female</Gender>
            <AssocIndiv>
                <Link Target="IndividualRec" Ref="IN008"/>
                <Association>Husband</Association>
            </AssocIndiv>
        </IndividualRec>
        <IndividualRec Id="IN008">
            <!-- This tag used for grand father -->
            <IndivName>Grand father's name</IndivName>
            <Gender>Male</Gender>
            <AssocIndiv>
                <Link Target="IndividualRec" Ref="IN007"/>
                <Association>Grand Father's Wife</Association>
            </AssocIndiv>
        </IndividualRec>
        <IndividualRec Id="IN009">
            <!-- This tag used for a child to h1 or w1 or grand son to gm1 or gf1 -->
            <IndivName>Child 1 name</IndivName>
            <Gender>Male</Gender>
            <AssocIndiv>
                <Link Target="IndividualRec" Ref="IN007"/>
                <Association>Father</Association>
            </AssocIndiv>
            <AssocIndiv>
                <Link Target="IndividualRec" Ref="IN008"/>
                <Association>Mother</Association>
            </AssocIndiv>
        </IndividualRec>
    </GEDCOM>
    

    the wanted, correct result is produced:

    [IN006,IN009] 
    

    II. To test if two Child elements are second cousins, add to the above this (and add the necessary code to test for every pair of Child, whether they are second cousins:

     <xsl:key name="kGrandParent" match="HusbFath | WifeMoth"
      use="my:GrandChildren(Link/@Ref)/*/@Ref"/>
    
     <xsl:function name="my:areFirststCousins" as="xs:boolean">
      <xsl:param name="pPers1" as="element()"/>
      <xsl:param name="pPers2" as="element()"/>
    
      <xsl:sequence select=
      "key('kGrandParent', $pPers1/*/@Ref, $vDoc)[1]
      is
       key('kGrandParent', $pPers2/*/@Ref, $vDoc)[1]
      and
       not($pPers1/.. is $pPers2/..)
      "/>
     </xsl:function>
    
     <xsl:function name="my:areSecondCousins" as="xs:boolean">
      <xsl:param name="pPers1" as="element()"/>
      <xsl:param name="pPers2" as="element()"/>
    
      <xsl:sequence select=
      "my:areFirststCousins($pPers1/../(HusbFath|WifeMoth)[1],
                            $pPers2/../(HusbFath|WifeMoth)[1]
                           )
      "/>
     </xsl:function>
    

    III. The complete solution:

    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:xs="http://www.w3.org/2001/XMLSchema"
     xmlns:my="my:my" exclude-result-prefixes="my xs">
     <xsl:output method="text"/>
    
     <xsl:key name="kChildren" match="Child"
      use="../HusbFath/*/@Ref"/>
    
     <xsl:key name="kChildren" match="Child"
      use="../WifeMoth/*/@Ref"/>
    
     <xsl:key name="kGrandParent" match="HusbFath | WifeMoth"
      use="my:GrandChildren(Link/@Ref)/*/@Ref"/>
    
     <xsl:variable name="vDoc" select="/"/>
    
     <xsl:template match="/*">
      First Cousins:
      <xsl:sequence select=
      "for $first in 1 to count(/*/FamilyRec/Child),
           $second in $first+1 to count(/*/FamilyRec/Child),
           $pers1 in (/*/FamilyRec/Child)[$first],
           $pers2 in (/*/FamilyRec/Child)[$second]
         return
           if(my:areFirststCousins($pers1, $pers2))
             then concat('[', $pers1/*/@Ref, ',', $pers2/*/@Ref, '] ')
             else ()
      "/>
    
      Second Cousins:
      <xsl:sequence select=
      "for $first in 1 to count(/*/FamilyRec/Child),
           $second in $first+1 to count(/*/FamilyRec/Child),
           $pers1 in (/*/FamilyRec/Child)[$first],
           $pers2 in (/*/FamilyRec/Child)[$second]
         return
           if(my:areSecondCousins($pers1, $pers2))
             then concat('[', $pers1/*/@Ref, ',', $pers2/*/@Ref, '] ')
             else ()
      "/>
     </xsl:template>
    
     <xsl:function name="my:GrandChildren">
      <xsl:param name="pRef" as="xs:string?"/>
    
       <xsl:sequence select=
       "key('kChildren',
            key('kChildren', $pRef, $vDoc)/Link/@Ref,
            $vDoc)
       "/>
     </xsl:function>
    
    
     <xsl:function name="my:areFirststCousins" as="xs:boolean">
      <xsl:param name="pPers1" as="element()"/>
      <xsl:param name="pPers2" as="element()"/>
    
      <xsl:sequence select=
      "key('kGrandParent', $pPers1/*/@Ref, $vDoc)[1]
      is
       key('kGrandParent', $pPers2/*/@Ref, $vDoc)[1]
      and
       not($pPers1/.. is $pPers2/..)
      "/>
     </xsl:function>
    
     <xsl:function name="my:areSecondCousins" as="xs:boolean">
      <xsl:param name="pPers1" as="element()"/>
      <xsl:param name="pPers2" as="element()"/>
    
      <xsl:sequence select=
      "my:areFirststCousins($pPers1/../(HusbFath|WifeMoth)[1],
                            $pPers2/../(HusbFath|WifeMoth)[1]
                           )
      "/>
     </xsl:function>
    </xsl:stylesheet>
    

    When this transformation is applied on the same provided XML document (above), the correct result is produced:

      First Cousins:
      [IN006,IN009] 
    
      Second Cousins:
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am new to xquery. I have the following xml document : <?xml version=1.0
I need to insert a new element into an XML document using XQuery insert
If I have several Section elements in an XML document, what XQuery do I
New to PHP and MySQL, have heard amazing things about this website from Leo
New to Regex. I want to validate to this format: Any character allowed, except
I'm completely new to XML Schema, XML Stylesheets and XQuery. To me, XML is
I am fighting with @x.nodes('...') as I am new to XQuery. I do have
I am new to xquery in SQL Server. I have often come across xquery
i am totally new to xquery and I am having problem getting the right
I am quite new to Marklogic and xquery in general. We have a collection

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.