XSLT 1.0 根据兄弟记录参数更改记录输出

XSLT 1.0 Change output of record based on sibling record parameter

我有一个传入的 XML 文件,其中包含我需要稍微转换的参与者和家属列表,以便依赖信息作为​​子记录附加到参与者信息和参与者记录的 EN_PLAN_TYPE节点根据参与者有多少家属以及他们的关系获得一个值。

传入 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. 创建一个新的Dependent_Record,它是每个Eligibility_Detail_Record的子记录]DP_PARTICIPANT_FILE_IMP_ID 匹配 PT_PARTICIPANT_FILE_IMP_ID
  2. 为每个 EN_PLAN_NAME 为 "Health Reimbursement Arrangement" 的记录添加一个 EN_PLAN_TYPE 字段上面写着:
    • "Ind" 如果没有找到家属
    • "IndSpouse" 如果只有一个受抚养人有 DP_RELATIONSHIP 的配偶
    • "IndChild" 如果只有一个受抚养人有 DP_RELATIONSHIP of Dependent
    • "Family" 如果不止一位受抚养人

预期输出

<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_RELATIONSHIP 的第一次出现进行测试,以测试存在依赖项的记录(DP_DEPENDENT_FILE_IMP_ID 被发现)并且 participantCount 是 1(我可能应该将变量重命名为 dependentCount 但我稍后会担心)

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

首先模板化顶级记录,然后使用键 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>