XSLT 1.0根据兄弟记录参数

时间:2017-02-07 02:03:40

标签: xml xslt xslt-1.0 muenchian-grouping

我有一个传入的XML文件,其中包含我需要稍微转换的参与者和家属列表,以便将相关信息作为子记录附加到参与者信息和参与者记录 EN_PLAN_TYPE node根据参与者的家属数量及其关系获得一个值。

INCOMING XML

  <EligibilityRecords xmlns="http://Eligibility_LSDD">
    <Eligibility_Detail_Record xmlns="">
      <PT_PARTICIPANT_FILE_IMP_ID>553739837</PT_PARTICIPANT_FILE_IMP_ID>
      <PT_LST_NM>DOE</PT_LST_NM>
      <PT_FRST_NM>JANE</PT_FRST_NM>
      <EN_PLAN_NAME>Health Reimbursement Arrangement</EN_PLAN_NAME>
      <EN_MBRSHP_EFF_STRT_DT>01012017</EN_MBRSHP_EFF_STRT_DT>
      <EN_MBRSHP_EFF_END_DT></EN_MBRSHP_EFF_END_DT>
      <EN_PLAN_YEAR_STRT_DT>01012017</EN_PLAN_YEAR_STRT_DT>
    </Eligibility_Detail_Record>
    <Eligibility_Detail_Record xmlns="">
        <PT_PARTICIPANT_FILE_IMP_ID>561859010</PT_PARTICIPANT_FILE_IMP_ID>
        <PT_LST_NM>MCLEOD</PT_LST_NM>
        <PT_FRST_NM>CONOR</PT_FRST_NM>
        <EN_PLAN_NAME>Health Reimbursement Arrangement</EN_PLAN_NAME>
        <EN_MBRSHP_EFF_STRT_DT>01012016</EN_MBRSHP_EFF_STRT_DT>
        <EN_MBRSHP_EFF_END_DT></EN_MBRSHP_EFF_END_DT>
        <EN_PLAN_YEAR_STRT_DT>01012016</EN_PLAN_YEAR_STRT_DT>
    </Eligibility_Detail_Record>
    <Eligibility_Detail_Record xmlns="">
        <PT_PARTICIPANT_FILE_IMP_ID>561859010</PT_PARTICIPANT_FILE_IMP_ID>
        <PT_LST_NM>MCLEOD</PT_LST_NM>
        <PT_FRST_NM>CONOR</PT_FRST_NM>
        <EN_PLAN_NAME>Health Reimbursement Arrangement</EN_PLAN_NAME>
        <EN_MBRSHP_EFF_STRT_DT>01012016</EN_MBRSHP_EFF_STRT_DT>
        <EN_MBRSHP_EFF_END_DT></EN_MBRSHP_EFF_END_DT>
        <EN_PLAN_YEAR_STRT_DT>01012016</EN_PLAN_YEAR_STRT_DT>
        <DP_PARTICIPANT_FILE_IMP_ID>561859010</DP_PARTICIPANT_FILE_IMP_ID>
        <DP_DEPENDENT_FILE_IMP_ID>SC000018241906</DP_DEPENDENT_FILE_IMP_ID>
        <DP_RELATIONSHIP>Dependent</DP_RELATIONSHIP>
        <DP_LST_NM>MCLEOD</DP_LST_NM>
        <DP_FRST_NM>DUNCAN</DP_FRST_NM>
    </Eligibility_Detail_Record>
    <Eligibility_Detail_Record xmlns="">
      <PT_PARTICIPANT_FILE_IMP_ID>610051908</PT_PARTICIPANT_FILE_IMP_ID>
      <PT_LST_NM>JACKSON</PT_LST_NM>
      <PT_FRST_NM>MICHAEL</PT_FRST_NM>
      <EN_PLAN_NAME>Health Reimbursement Arrangement</EN_PLAN_NAME>
      <EN_MBRSHP_EFF_STRT_DT>01012017</EN_MBRSHP_EFF_STRT_DT>
      <EN_MBRSHP_EFF_END_DT></EN_MBRSHP_EFF_END_DT>
      <EN_PLAN_YEAR_STRT_DT>01012017</EN_PLAN_YEAR_STRT_DT>
    </Eligibility_Detail_Record>
    <Eligibility_Detail_Record xmlns="">
      <PT_PARTICIPANT_FILE_IMP_ID>610051908</PT_PARTICIPANT_FILE_IMP_ID>
      <PT_LST_NM>JACKSON</PT_LST_NM>
      <PT_FRST_NM>MICHAEL</PT_FRST_NM>
      <EN_PLAN_NAME>Health Reimbursement Arrangement</EN_PLAN_NAME>
      <EN_MBRSHP_EFF_STRT_DT>01012017</EN_MBRSHP_EFF_STRT_DT>
      <EN_MBRSHP_EFF_END_DT></EN_MBRSHP_EFF_END_DT>
      <EN_PLAN_YEAR_STRT_DT>01012017</EN_PLAN_YEAR_STRT_DT>
      <DP_PARTICIPANT_FILE_IMP_ID>610051908</DP_PARTICIPANT_FILE_IMP_ID>
      <DP_DEPENDENT_FILE_IMP_ID>NC110015202761</DP_DEPENDENT_FILE_IMP_ID>
      <DP_RELATIONSHIP>Spouse</DP_RELATIONSHIP>
      <DP_LST_NM>JACKSON</DP_LST_NM>
      <DP_FRST_NM>MELISSA</DP_FRST_NM>
    </Eligibility_Detail_Record>
    <Eligibility_Detail_Record xmlns="">
      <PT_PARTICIPANT_FILE_IMP_ID>553739837</PT_PARTICIPANT_FILE_IMP_ID>
      <PT_LST_NM>DOE</PT_LST_NM>
      <PT_FRST_NM>JANE</PT_FRST_NM>
      <EN_PLAN_NAME>Health Reimbursement Arrangement</EN_PLAN_NAME>
      <EN_MBRSHP_EFF_STRT_DT>01012016</EN_MBRSHP_EFF_STRT_DT>
      <EN_MBRSHP_EFF_END_DT></EN_MBRSHP_EFF_END_DT>
      <EN_PLAN_YEAR_STRT_DT>01012016</EN_PLAN_YEAR_STRT_DT>
      <DP_PARTICIPANT_FILE_IMP_ID>553739837</DP_PARTICIPANT_FILE_IMP_ID>
      <DP_DEPENDENT_FILE_IMP_ID>NC110012077673</DP_DEPENDENT_FILE_IMP_ID>
      <DP_RELATIONSHIP>Spouse</DP_RELATIONSHIP>
      <DP_LST_NM>DOE</DP_LST_NM>
      <DP_FRST_NM>JOHN</DP_FRST_NM>
    </Eligibility_Detail_Record>
    <Eligibility_Detail_Record xmlns="">
      <PT_PARTICIPANT_FILE_IMP_ID>553739837</PT_PARTICIPANT_FILE_IMP_ID>
      <PT_LST_NM>DOE</PT_LST_NM>
      <PT_FRST_NM>JANE</PT_FRST_NM>
      <EN_PLAN_NAME>Health Reimbursement Arrangement</EN_PLAN_NAME>
      <EN_MBRSHP_EFF_STRT_DT>01012016</EN_MBRSHP_EFF_STRT_DT>
      <EN_MBRSHP_EFF_END_DT></EN_MBRSHP_EFF_END_DT>
      <EN_PLAN_YEAR_STRT_DT>01012016</EN_PLAN_YEAR_STRT_DT>
      <DP_PARTICIPANT_FILE_IMP_ID>553739837</DP_PARTICIPANT_FILE_IMP_ID>
      <DP_DEPENDENT_FILE_IMP_ID>NC110012077680</DP_DEPENDENT_FILE_IMP_ID>
      <DP_RELATIONSHIP>Dependent</DP_RELATIONSHIP>
      <DP_LST_NM>DOE</DP_LST_NM>
      <DP_FRST_NM>JIMMY</DP_FRST_NM>
    </Eligibility_Detail_Record>
    <Eligibility_Detail_Record xmlns="">
      <PT_PARTICIPANT_FILE_IMP_ID>573794953</PT_PARTICIPANT_FILE_IMP_ID>
      <PT_LST_NM>GABRIEL</PT_LST_NM>
      <PT_FRST_NM>PETER</PT_FRST_NM>
      <EN_PLAN_NAME>Health Reimbursement Arrangement</EN_PLAN_NAME>
      <EN_MBRSHP_EFF_STRT_DT>01012016</EN_MBRSHP_EFF_STRT_DT>
      <EN_MBRSHP_EFF_END_DT></EN_MBRSHP_EFF_END_DT>
      <EN_PLAN_YEAR_STRT_DT>01012016</EN_PLAN_YEAR_STRT_DT>
    </Eligibility_Detail_Record>
  </EligibilityRecords>

要求

  1. 为与 PT_PARTICIPANT_FILE_IMP_ID <匹配的 DP_PARTICIPANT_FILE_IMP_ID 创建一个新的 Dependent_Record ,这是 Eligibility_Detail_Record 的子记录/ LI>
  2. 为每个记录添加 EN_PLAN_TYPE 字段,该记录的 EN_PLAN_NAME 为&#34;健康报销安排&#34;这是:
    • &#34;工业&#34;如果没有找到家属
    • &#34; IndSpouse&#34;如果一个且只有一个拥有 DP_RELATIONSHIP 的配偶
    • &#34; IndChild&#34;如果只有一个依赖项具有 DP_RELATIONSHIP 的依赖
    • &#34;家庭&#34;如果不止一个依赖
  3. 预期输出

    <EligibilityRecords xmlns="http://Eligibility_LSDD">
      <Eligibility_Detail_Record xmlns="">
        <PT_PARTICIPANT_FILE_IMP_ID>553739837</PT_PARTICIPANT_FILE_IMP_ID>
        <PT_LST_NM>DOE</PT_LST_NM>
        <PT_FRST_NM>JANE</PT_FRST_NM>
        <EN_PLAN_NAME>Health Reimbursement Arrangement</EN_PLAN_NAME>
        <EN_PLAN_TYPE>Family</EN_PLAN_TYPE>
        <EN_MBRSHP_EFF_STRT_DT>01012017</EN_MBRSHP_EFF_STRT_DT>
        <EN_MBRSHP_EFF_END_DT></EN_MBRSHP_EFF_END_DT>
        <EN_PLAN_YEAR_STRT_DT>01012017</EN_PLAN_YEAR_STRT_DT>
        <Dependent_Record>
          <DP_PARTICIPANT_FILE_IMP_ID>553739837</DP_PARTICIPANT_FILE_IMP_ID>
          <DP_DEPENDENT_FILE_IMP_ID>NC110012077673</DP_DEPENDENT_FILE_IMP_ID>
          <DP_RELATIONSHIP>Spouse</DP_RELATIONSHIP>
          <DP_LST_NM>DOE</DP_LST_NM>
          <DP_FRST_NM>JOHN</DP_FRST_NM>
        </Dependent_Record>
        <Dependent_Record>
          <DP_PARTICIPANT_FILE_IMP_ID>553739837</DP_PARTICIPANT_FILE_IMP_ID>
          <DP_DEPENDENT_FILE_IMP_ID>NC110012077680</DP_DEPENDENT_FILE_IMP_ID>
          <DP_RELATIONSHIP>Dependent</DP_RELATIONSHIP>
          <DP_LST_NM>DOE</DP_LST_NM>
          <DP_FRST_NM>JIMMY</DP_FRST_NM>
        </Dependent_Record>
      </Eligibility_Detail_Record>
      <Eligibility_Detail_Record xmlns="">
        <PT_PARTICIPANT_FILE_IMP_ID>610051908</PT_PARTICIPANT_FILE_IMP_ID>
        <PT_LST_NM>JACKSON</PT_LST_NM>
        <PT_FRST_NM>MICHAEL</PT_FRST_NM>
        <EN_PLAN_NAME>Health Reimbursement Arrangement</EN_PLAN_NAME>
        <EN_PLAN_TYPE>IndSpouse</EN_PLAN_TYPE>
        <EN_MBRSHP_EFF_STRT_DT>01012017</EN_MBRSHP_EFF_STRT_DT>
        <EN_MBRSHP_EFF_END_DT></EN_MBRSHP_EFF_END_DT>
        <EN_PLAN_YEAR_STRT_DT>01012017</EN_PLAN_YEAR_STRT_DT>
        <Dependent_Record>
          <DP_PARTICIPANT_FILE_IMP_ID>610051908</DP_PARTICIPANT_FILE_IMP_ID>
          <DP_DEPENDENT_FILE_IMP_ID>NC110015202761</DP_DEPENDENT_FILE_IMP_ID>
          <DP_RELATIONSHIP>Spouse</DP_RELATIONSHIP>
          <DP_LST_NM>JACKSON</DP_LST_NM>
          <DP_FRST_NM>MELISSA</DP_FRST_NM>
        </Dependent_Record>
      </Eligibility_Detail_Record>
      <Eligibility_Detail_Record xmlns="">
        <PT_PARTICIPANT_FILE_IMP_ID>561859010</PT_PARTICIPANT_FILE_IMP_ID>
        <PT_LST_NM>MCLEOD</PT_LST_NM>
        <PT_FRST_NM>CONOR</PT_FRST_NM>
        <EN_PLAN_NAME>Health Reimbursement Arrangement</EN_PLAN_NAME>
        <EN_PLAN_TYPE>IndChild</EN_PLAN_TYPE>
        <EN_MBRSHP_EFF_STRT_DT>01012016</EN_MBRSHP_EFF_STRT_DT>
        <EN_MBRSHP_EFF_END_DT></EN_MBRSHP_EFF_END_DT>
        <EN_PLAN_YEAR_STRT_DT>01012016</EN_PLAN_YEAR_STRT_DT>
        <Dependent_Record>
          <DP_PARTICIPANT_FILE_IMP_ID>561859010</DP_PARTICIPANT_FILE_IMP_ID>
          <DP_DEPENDENT_FILE_IMP_ID>SC000018241906</DP_DEPENDENT_FILE_IMP_ID>
          <DP_RELATIONSHIP>Dependent</DP_RELATIONSHIP>
          <DP_LST_NM>MCLEOD</DP_LST_NM>
          <DP_FRST_NM>DUNCAN</DP_FRST_NM>
        </Dependent_Record>
      </Eligibility_Detail_Record>
      <Eligibility_Detail_Record xmlns="">
        <PT_PARTICIPANT_FILE_IMP_ID>573794953</PT_PARTICIPANT_FILE_IMP_ID>
        <PT_LST_NM>GABRIEL</PT_LST_NM>
        <PT_FRST_NM>PETER</PT_FRST_NM>
        <EN_PLAN_NAME>Health Reimbursement Arrangement</EN_PLAN_NAME>
        <EN_MBRSHP_EFF_STRT_DT>01012016</EN_MBRSHP_EFF_STRT_DT>
        <EN_MBRSHP_EFF_END_DT></EN_MBRSHP_EFF_END_DT>
        <EN_PLAN_YEAR_STRT_DT>01012016</EN_PLAN_YEAR_STRT_DT>
        <EN_PLAN_TYPE>Ind</EN_PLAN_TYPE>
      </Eligibility_Detail_Record>
    </EligibilityRecords>
    

    XSLT 1.0

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output indent="yes" omit-xml-declaration="yes"/>
      <xsl:strip-space elements="*"/>
    
      <xsl:key name="participants" match="Eligibility_Detail_Record" use="PT_PARTICIPANT_FILE_IMP_ID"/>
    
      <xsl:template match="/*">
        <xsl:copy>
          <xsl:for-each select="Eligibility_Detail_Record[string(PT_PARTICIPANT_FILE_IMP_ID)]
            [count(.|key('participants',PT_PARTICIPANT_FILE_IMP_ID)[1])=1]">
            <xsl:variable name="participantCount" select="count(key('participants',PT_PARTICIPANT_FILE_IMP_ID)[string(DP_PARTICIPANT_FILE_IMP_ID)])"/>
            <Eligibility_Detail_Record>
              <xsl:copy-of select="*[starts-with(name(),'PT_')]|EN_PLAN_NAME"/>
              <xsl:choose>
                <!--Add an EN_PLAN_TYPE field for each Record that has an EN_PLAN_NAME of "Health Reimbursement Arrangement" that reads:-->
                <xsl:when test="EN_PLAN_NAME='Health Reimbursement Arrangement'">
                  <xsl:call-template name="HRA">
                    <xsl:with-param name="participantCount" select="$participantCount"/>
                  </xsl:call-template>
                </xsl:when>
              </xsl:choose>
              <xsl:copy-of select="*[not(self::EN_PLAN_NAME) and starts-with(name(),'EN_')]"/>
              <!--Create a new Dependent_Record that is a child record of Eligibility_Detail_Record for each DP_PARTICIPANT_FILE_IMP_ID that matches a PT_PARTICIPANT_FILE_IMP_ID-->
              <xsl:for-each select="key('participants',PT_PARTICIPANT_FILE_IMP_ID)[string(DP_PARTICIPANT_FILE_IMP_ID)]">
                <Dependent_Record>
                  <xsl:copy-of select="*[starts-with(name(),'DP_')]"/>
                </Dependent_Record>
              </xsl:for-each>
            </Eligibility_Detail_Record>
          </xsl:for-each>
        </xsl:copy>
      </xsl:template>
    
      <xsl:template name="HRA">
        <xsl:param name="participantCount"/>
        <EN_PLAN_TYPE>
          <xsl:choose>
            <!--"Family" if more than one dependent-->
            <xsl:when test="$participantCount > 1">Family</xsl:when>
            <!--"IndChild" if one and only one dependent that has DP_RELATIONSHIP of Dependent-->
            <xsl:when test="$participantCount = 1 and DP_RELATIONSHIP = 'Dependent'">IndChild</xsl:when>
            <!--"IndSpouse" if one and only one dependent that has DP_RELATIONSHIP of Spouse-->
            <xsl:when test="$participantCount = 1 and DP_RELATIONSHIP = 'Spouse'">IndSpouse</xsl:when>
            <!--"Ind" if no dependents are found-->
            <xsl:when test="$participantCount = 0">Ind</xsl:when>
          </xsl:choose>
        </EN_PLAN_TYPE>
      </xsl:template>
    
    </xsl:stylesheet>
    

    问题是 DP_RELATIONSHIP 正在针对特定参与者的每个 ELIGIBILITY_DETAIL_RECORD 的第一次出现进行测试,该参与者始终为空白。我还不确定如何轻松调整它以针对存在依赖项( DP_DEPENDENT_FILE_IMP_ID )和的记录首次出现 DP_RELATIONSHIP 进行测试participantCount 是1(我应该将变量重命名为dependentCount,但我稍后会担心)

1 个答案:

答案 0 :(得分:0)

我建议只收集家属,而不是用钥匙收集所有参与者。

模板顶级使用密钥首先记录EN_PLAN_TYPEDependent_Record元素:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes" omit-xml-declaration="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:key name="dependents" match="Eligibility_Detail_Record" use="DP_PARTICIPANT_FILE_IMP_ID"/>

    <xsl:template match="/*">
        <xsl:copy>
            <xsl:apply-templates select="Eligibility_Detail_Record[not(DP_PARTICIPANT_FILE_IMP_ID)]"/>
        </xsl:copy>
    </xsl:template>

    <!-- Template top level records -->
    <xsl:template match="Eligibility_Detail_Record[not(DP_PARTICIPANT_FILE_IMP_ID)]">
        <xsl:copy>
            <xsl:copy-of select="*[starts-with(name(),'PT_')]|EN_PLAN_NAME"/>
            <!--Add an EN_PLAN_TYPE field for each Record that has an EN_PLAN_NAME of "Health Reimbursement Arrangement" -->
            <xsl:if test="EN_PLAN_NAME='Health Reimbursement Arrangement'">
                <xsl:call-template name="HRA"/>
            </xsl:if>
            <xsl:copy-of select="*[not(self::EN_PLAN_NAME) and starts-with(name(),'EN_')]"/>
            <xsl:apply-templates select="key('dependents', PT_PARTICIPANT_FILE_IMP_ID)"/>
        </xsl:copy>
    </xsl:template>

    <!-- Template dependent records -->
    <xsl:template match="Eligibility_Detail_Record[DP_PARTICIPANT_FILE_IMP_ID]">
        <Dependent_Record>
            <xsl:copy-of select="*[starts-with(name(), 'DP_')]"/>
        </Dependent_Record>
    </xsl:template>

    <!-- Plan type -->
    <xsl:template name="HRA">
        <xsl:variable name="dependents" select="key('dependents', PT_PARTICIPANT_FILE_IMP_ID)"/>
        <xsl:variable name="participantCount" select="count($dependents)"/>
        <EN_PLAN_TYPE>
            <xsl:choose>
                <!--"Family" if more than one dependent-->
                <xsl:when test="$participantCount > 1">Family</xsl:when>
                <!--"IndChild" if one and only one dependent that has DP_RELATIONSHIP of Dependent-->
                <xsl:when test="$participantCount = 1 and $dependents/DP_RELATIONSHIP = 'Dependent'">IndChild</xsl:when>
                <!--"IndSpouse" if one and only one dependent that has DP_RELATIONSHIP of Spouse-->
                <xsl:when test="$participantCount = 1 and $dependents/DP_RELATIONSHIP = 'Spouse'">IndSpouse</xsl:when>
                <!--"Ind" if no dependents are found-->
                <xsl:when test="$participantCount = 0">Ind</xsl:when>
            </xsl:choose>
        </EN_PLAN_TYPE>
    </xsl:template>

</xsl:stylesheet>