Xquery搜索一对堂兄弟

时间:2013-11-17 16:18:22

标签: xml xslt xpath xquery

我有一个XSLT文档可以完成这项工作,但我想翻译成xQuery! 我想寻找第一对堂兄和第二对堂兄。我想我只需要在xquery中转换xsl函数,但我完全是新手,有人可以帮我解决 任何帮助都会非常感激!

<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文件:

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

1 个答案:

答案 0 :(得分:0)

如果您只想将关键功能转换为XQuery功能,请转到Xquery function for Xslt key function并准备好建议解决方案。