<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="Test1.xsl"?>
<root>
<RemittanceInformation>
<EntityAssignedNumber>25</EntityAssignedNumber>
<IndividualRemittance>
<IndividualName>
<LastName>Johnson</LastName>
<FirstName>Steve</FirstName>
<ExchangeAssignedSubscriberID>6650442525</ExchangeAssignedSubscriberID>
</IndividualName>
<ExchangeAssignedQHPID>38408SC221000101</ExchangeAssignedQHPID>
<ExchangeAssignedPolicyID>26141334</ExchangeAssignedPolicyID>
<IssuerAssignedPolicyID>39147964</IssuerAssignedPolicyID>
<IssuerAssignedSubscriberID>101009913000</IssuerAssignedSubscriberID>
</IndividualRemittance>
<RemittanceDetail>
<ExchangePaymentCode>APTC</ExchangePaymentCode>
<PaymentAmount>214.00</PaymentAmount>
<PaymentCoverageStartDate>2016-10-01</PaymentCoverageStartDate>
<PaymentCoverageEndDate>2016-10-31</PaymentCoverageEndDate>
</RemittanceDetail>
<RemittanceDetail>
<ExchangePaymentCode>UF</ExchangePaymentCode>
<PaymentAmount>-43.04</PaymentAmount>
<PaymentCoverageStartDate>2016-10-01</PaymentCoverageStartDate>
<PaymentCoverageEndDate>2016-10-31</PaymentCoverageEndDate>
</RemittanceDetail>
</RemittanceInformation>
<RemittanceInformation>
<EntityAssignedNumber>26</EntityAssignedNumber>
<IndividualRemittance>
<IndividualName>
<LastName>Johnson</LastName>
<FirstName>Steve</FirstName>
<ExchangeAssignedSubscriberID>0000442525</ExchangeAssignedSubscriberID>
</IndividualName>
<ExchangeAssignedQHPID>38408SC001000101</ExchangeAssignedQHPID>
<ExchangeAssignedPolicyID>26141334</ExchangeAssignedPolicyID>
<IssuerAssignedPolicyID>39147964</IssuerAssignedPolicyID>
<IssuerAssignedSubscriberID>101009913000</IssuerAssignedSubscriberID>
</IndividualRemittance>
<RemittanceDetail>
<ExchangePaymentCode>APTC</ExchangePaymentCode>
<PaymentAmount>556.00</PaymentAmount>
<PaymentCoverageStartDate>2016-10-01</PaymentCoverageStartDate>
<PaymentCoverageEndDate>2016-10-31</PaymentCoverageEndDate>
</RemittanceDetail>
<RemittanceDetail>
<ExchangePaymentCode>UF</ExchangePaymentCode>
<PaymentAmount>-30.50</PaymentAmount>
<PaymentCoverageStartDate>2016-10-01</PaymentCoverageStartDate>
<PaymentCoverageEndDate>2016-10-31</PaymentCoverageEndDate>
</RemittanceDetail>
</RemittanceInformation>
<RemittanceInformation>
<EntityAssignedNumber>27</EntityAssignedNumber>
<IndividualRemittance>
<IndividualName>
<LastName>Masterson</LastName>
<FirstName>Gene</FirstName>
<MiddleName>E</MiddleName>
<ExchangeAssignedSubscriberID>0032171620</ExchangeAssignedSubscriberID>
</IndividualName>
<ExchangeAssignedQHPID>384111C001000101</ExchangeAssignedQHPID>
<ExchangeAssignedPolicyID>26523035</ExchangeAssignedPolicyID>
<IssuerAssignedPolicyID>38976623</IssuerAssignedPolicyID>
<IssuerAssignedSubscriberID>101009869500</IssuerAssignedSubscriberID>
</IndividualRemittance>
<RemittanceDetail>
<ExchangePaymentCode>APTC</ExchangePaymentCode>
<PaymentAmount>448.00</PaymentAmount>
<PaymentCoverageStartDate>2016-10-01</PaymentCoverageStartDate>
<PaymentCoverageEndDate>2016-10-31</PaymentCoverageEndDate>
</RemittanceDetail>
<RemittanceDetail>
<ExchangePaymentCode>UF</ExchangePaymentCode>
<PaymentAmount>-30.50</PaymentAmount>
<PaymentCoverageStartDate>2016-10-01</PaymentCoverageStartDate>
<PaymentCoverageEndDate>2016-10-31</PaymentCoverageEndDate>
</RemittanceDetail>
</RemittanceInformation>
</root>
&#13;
我在上述格式的XML文件中有大量数据。一个人由一个&#34;汇款信息&#34;代表,但有一些重复。我想合并这些重复项并添加&#34; RemittanceDetail&#34;从所有重复项到该人的第一个实例的实例。一个人由&#34; ExchangeAssignedPolicyID&#34;唯一标识。号。
在上面的示例中,26号的两个RemittanceDetail节点应该移动到25号的RemittanceInformation,因为它们都是具有相同ExchangeAssignedPolicyID的同一个人。所有后续的RemittanceInformation节点都应该递增以弥补缺失的数字。
我在S / O上看过类似的代码,但是经过很长时间和很多咖啡后,我无法解决这个问题。任何帮助将不胜感激。
答案 0 :(得分:1)
如果您在XSLT 2.0中使用for-each-group
(请参阅https://www.w3.org/TR/xslt20/#grouping-examples中的示例),那么您只需要
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="root">
<xsl:copy>
<xsl:for-each-group select="RemittanceInformation" group-by="IndividualRemittance/ExchangeAssignedPolicyID">
<xsl:copy>
<xsl:apply-templates select="EntityAssignedNumber, IndividualRemittance, current-group()/RemittanceDetail"/>
</xsl:copy>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
</xsl:transform>
在http://xsltransform.net/ejivdHd在线。
至于EntityAssignedNumber
元素的适应性,这里有一些改变:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="root">
<xsl:copy>
<xsl:for-each-group select="RemittanceInformation" group-by="IndividualRemittance/ExchangeAssignedPolicyID">
<xsl:copy>
<xsl:variable name="pos" select="position()"/>
<xsl:apply-templates select="EntityAssignedNumber, IndividualRemittance, current-group()/RemittanceDetail">
<xsl:with-param name="pos" select="$pos"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
<xsl:template match="EntityAssignedNumber">
<xsl:param name="pos"/>
<xsl:copy>
<xsl:value-of select="/root/RemittanceInformation[1]/EntityAssignedNumber + $pos - 1"/>
</xsl:copy>
</xsl:template>
</xsl:transform>