【发布时间】:2021-10-14 18:28:18
【问题描述】:
我遇到了一个挑战,即根据一个 xml 标签的数量动态添加 xml 标签。例如:我在下面 xml 的“CreditorPPContractParts”部分中有 2 条债权人记录,如测试数据所示。
<PPPrivPropertyLine>
<InsuredProperties>
<Entry>
<Buildings>
<Entry>
<AlarmClass>None_De</AlarmClass>
<InterestType>OwnerOccupied_De</InterestType>
<BuildingStandard_De>Normal</BuildingStandard_De>
</Entry>
</Buildings>
<ContractParts>
<Entry>
<CreditorPPContractParts>
<Entry>
<ReferenceNumber>SSG-SGLAKTZN gel. wg. EU-DSGVO</ReferenceNumber>
<InsuranceCoverage>0</InsuranceCoverage>
<IssueDate>2016-09-08T00:00:00+02:00</IssueDate>
<Creditor>
<Contact>
<AddressBookUID>D73GLX</AddressBookUID>
</Contact>
</Creditor>
</Entry>
<Entry>
<ReferenceNumber>SSG-SGLAKTZN gel. wg. EU-DSGVO</ReferenceNumber>
<InsuranceCoverage>0</InsuranceCoverage>
<IssueDate>1979-10-17T00:00:00+01:00</IssueDate>
<Creditor>
<Contact>
<AddressBookUID>OAS5OE</AddressBookUID>
</Contact>
</Creditor>
</Entry>
</CreditorPPContractParts>
</Entry>
</ContractParts>
</Entry>
</InsuredProperties>
<PolicyContactRoles></PolicyContactRoles>
</PPPrivPropertyLine>
现在我必须在“PolicyContactRoles”中以相同的 xml 格式创建 2 个条目,如下面的格式,因为我上面有 2 个债权人记录。我们可能有超过 2 条债权人记录,但我们需要根据债权人记录数进行添加。
<PolicyContactRoles>
<Entry>
<AccountContactRole>
<Subtype>Creditor_De</Subtype>
<AccountContact>
<Contact>
<AddressBookUID>D73GLX</AddressBookUID>
</Contact>
</AccountContact>
</AccountContactRole>
<Subtype>PolicyCreditor_De</Subtype>
</Entry>
<Entry>
<AccountContactRole>
<Subtype>Creditor_De</Subtype>
<AccountContact>
<Contact>
<AddressBookUID>OAS5OE</AddressBookUID>
</Contact>
</AccountContact>
</AccountContactRole>
<Subtype>PolicyCreditor_De</Subtype>
</Entry>
</PolicyContactRoles>
我已经为单张唱片做了。我不知道如何实现多个债权人记录。请帮帮我,谢谢!
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<!--copy all nodes and attributes-->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:param name="old2" select="PPPrivPropertyLine/InsuredProperties/Entry/ContractParts/Entry/CreditorPPContractParts/Entry/Creditor/Contact/AddressBookUID"/>
<xsl:template match="PolicyContactRoles">
<xsl:copy>
<xsl:if test="$old2 != ''">
<Entry>
<AccountContactRole>
<Subtype>Creditor_De</Subtype>
<AccountContact>
<Contact>
<AddressBookUID>
<xsl:value-of select="$old2"/>
</AddressBookUID>
</Contact>
</AccountContact>
</AccountContactRole>
<Subtype>PolicyCreditor_De</Subtype>
</Entry>
</xsl:if>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
另外,请使用这个 XSLT Fiddle:https://xsltfiddle.liberty-development.net/pNEj9dH/11
【问题讨论】:
-
<AddressBookUID>XN8DOW</AddressBookUID>的值从何而来? -
@michael.hor257k :对不起,迈克尔,我更新错了。现已更正。并感谢您的回复。问题已解决
标签: javascript xml xslt