2

私はxqueryを初めて使用し、この問題の迅速な解決策を探しています。

xmlドキュメントがあり、ドキュメント内の最初のいとこのすべてのペアと2番目のいとこのすべてのペアを検索したいと思います。

以下のxmlをご覧になり、続行する方法、または高度に適用されるコードスニペットを提供できるかどうかをガイドしてください。

<?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>

*出力例:* 最初のいとこのペアの出力例はIN006とIN009です。

よろしく運賃

4

1 に答える 1

1

I.このXSLT2.0変換(XSLT 1.0に簡単に書き直すことができます)は、すべてのいとこを生成します

<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>

この変換が提供されたXMLドキュメントに適用される場合:

<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>

必要な正しい結果が生成されます:

[IN006,IN009] 

II。2つのChild要素が従兄弟であるかどうかをテストするには、上記にこれChildを追加します(そして、それらが従兄弟であるかどうかをテストするために必要なコードを追加します。

 <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。完全なソリューション

<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>

この変換が同じ提供されたXMLドキュメント(上記)に適用されると、正しい結果が生成されます。

  First Cousins:
  [IN006,IN009] 

  Second Cousins:
于 2012-12-09T00:27:54.307 に答える