iam使用XSLT将值卡在增量中。我需要根据某些条件来递增元素值。在满足每个策略/策略条件的实际XML中,我需要一个一个递增,并创建策略/策略/策略条件/策略条件/策略conditioncriteria/itemdata .please请找到预期的XML以获取详细信息。
>输入XML:
<buildings>
<building>
<building-header>
<location-number>LU1</location-number>
<building-number>LU1-B1</building-number>
</building-header>
</building>
<building>
<building-header>
<location-number>LU2</location-number>
<building-number>LU2-B1</building-number>
</building-header>
</building>
<building>
<building-header>
<location-number>LU2</location-number>
<building-number>LU2-B2</building-number>
</building-header>
</building>
<building>
<building-header>
<location-number>LU3</location-number>
<building-number>LU3-B1</building-number>
</building-header>
</building>
<building>
<building-header>
<location-number>LU3</location-number>
<building-number>LU3-B2</building-number>
</building-header>
</building>
<building>
<building-header>
<location-number>LU4</location-number>
<building-number>LU4-B1</building-number>
</building-header>
</building>
<policy-details>
<coverages>
<coverage>
<limit>
<limit-type code="Bla"/>
</limit>
<location-rule>
<insured-objects>
<insured-object>
<insured-object-nr>LU1</insured-object-nr>
</insured-object>
<insured-object>
<insured-object-nr>LU2</insured-object-nr>
</insured-object>
</insured-objects>
</location-rule>
</coverage>
<coverage>
<limit>
<limit-type code="Bla"/>
</limit>
<location-rule>
<insured-objects>
<insured-object>
<insured-object-nr>LU3</insured-object-nr>
</insured-object>
</insured-objects>
</location-rule>
</coverage>
<coverage>
<limit>
<limit-type code="Sub"/>
</limit>
<location-rule>
<insured-objects>
<insured-object>
<insured-object-nr>LU2</insured-object-nr>
</insured-object>
<insured-object>
<insured-object-nr>LU3</insured-object-nr>
</insured-object>
</insured-objects>
</location-rule>
</coverage>
</coverages>
</policy-details>
</buildings>
XSLT:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="/">
<policy>
<xsl:for-each select="//coverage">
<xsl:if test="limit/limit-type/@code='Bla'">
<PolicyConditions>
<Name>EQ Blanket</Name>
<xsl:for-each select="location-rule/insured-objects/insured-object">
<xsl:variable name="var_Ins_Obj">
<xsl:value-of select="insured-object-nr"/>
</xsl:variable>
<xsl:for-each select="//building">
<xsl:variable name="var_loc_Num">
<xsl:value-of select="building-header/location-number"/>
</xsl:variable>
<xsl:if test="$var_loc_Num=$var_Ins_Obj">
<xsl:variable name="locBuild">
<xsl:value-of select="building-header/building-number"/>
</xsl:variable>
<PolicyConditionCriteria>
<Field>LOCNAME</Field>
<Value>
<xsl:value-of select="$locBuild"/>
</Value>
</PolicyConditionCriteria>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
</PolicyConditions>
</xsl:if>
<xsl:if test="limit/limit-type/@code='Sub'">
<PolicyConditions>
<Name>EQ Sublimit</Name>
<xsl:for-each select="location-rule/insured-objects/insured-object">
<xsl:variable name="var_Ins_Obj">
<xsl:value-of select="insured-object-nr"/>
</xsl:variable>
<xsl:for-each select="//building">
<xsl:variable name="var_loc_Num">
<xsl:value-of select="building-header/location-number"/>
</xsl:variable>
<xsl:if test="$var_loc_Num=$var_Ins_Obj">
<xsl:variable name="locBuild">
<xsl:value-of select="building-header/building-number"/>
</xsl:variable>
<PolicyConditionCriteria>
<Field>LOCNAME</Field>
<Value>
<xsl:value-of select="$locBuild"/>
</Value>
</PolicyConditionCriteria>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
</PolicyConditions>
</xsl:if>
</xsl:for-each>
</policy>
</xsl:template>
</xsl:stylesheet>
实际XML:
<policy>
<PolicyConditions>
<Name>EQ Blanket</Name>
<PolicyConditionCriteria>
<Field>LOCNAME</Field>
<Value>LU1-B1</Value>
</PolicyConditionCriteria>
<PolicyConditionCriteria>
<Field>LOCNAME</Field>
<Value>LU2-B1</Value>
</PolicyConditionCriteria>
<PolicyConditionCriteria>
<Field>LOCNAME</Field>
<Value>LU2-B2</Value>
</PolicyConditionCriteria>
</PolicyConditions>
<PolicyConditions>
<Name>EQ Blanket</Name>
<PolicyConditionCriteria>
<Field>LOCNAME</Field>
<Value>LU3-B1</Value>
</PolicyConditionCriteria>
<PolicyConditionCriteria>
<Field>LOCNAME</Field>
<Value>LU3-B2</Value>
</PolicyConditionCriteria>
</PolicyConditions>
<PolicyConditions>
<Name>EQ Sublimit</Name>
<PolicyConditionCriteria>
<Field>LOCNAME</Field>
<Value>LU2-B1</Value>
</PolicyConditionCriteria>
<PolicyConditionCriteria>
<Field>LOCNAME</Field>
<Value>LU2-B2</Value>
</PolicyConditionCriteria>
<PolicyConditionCriteria>
<Field>LOCNAME</Field>
<Value>LU3-B1</Value>
</PolicyConditionCriteria>
<PolicyConditionCriteria>
<Field>LOCNAME</Field>
<Value>LU3-B2</Value>
</PolicyConditionCriteria>
</PolicyConditions>
</policy>
预期XML:
<policy>
<PolicyConditions>
<Name>EQ Blanket1</Name>
<Item>1</Item>
<PolicyConditionCriteria>
<Field>LOCNAME</Field>
<Value>LU1-B1</Value>
<ItemData>1</ItemData>
</PolicyConditionCriteria>
<PolicyConditionCriteria>
<Field>LOCNAME</Field>
<Value>LU2-B1</Value>
<ItemData>2</ItemData>
</PolicyConditionCriteria>
<PolicyConditionCriteria>
<Field>LOCNAME</Field>
<Value>LU2-B2</Value>
<ItemData>3</ItemData>
</PolicyConditionCriteria>
</PolicyConditions>
<PolicyConditions>
<Name>EQ Blanket2</Name>
<Item>2</Item>
<PolicyConditionCriteria>
<Field>LOCNAME</Field>
<Value>LU3-B1</Value>
<ItemData>1</ItemData>
</PolicyConditionCriteria>
<PolicyConditionCriteria>
<Field>LOCNAME</Field>
<Value>LU3-B2</Value>
<ItemData>2</ItemData>
</PolicyConditionCriteria>
</PolicyConditions>
<PolicyConditions>
<Name>EQ Sublimit1</Name>
<Item>2</Item>
<PolicyConditionCriteria>
<Field>LOCNAME</Field>
<Value>LU2-B1</Value>
<ItemData>1</ItemData>
</PolicyConditionCriteria>
<PolicyConditionCriteria>
<Field>LOCNAME</Field>
<Value>LU2-B2</Value>
<ItemData>2</ItemData>
</PolicyConditionCriteria>
<PolicyConditionCriteria>
<Field>LOCNAME</Field>
<Value>LU3-B1</Value>
<ItemData>3</ItemData>
</PolicyConditionCriteria>
<PolicyConditionCriteria>
<Field>LOCNAME</Field>
<Value>LU3-B2</Value>
<ItemData>4</ItemData>
</PolicyConditionCriteria>
</PolicyConditions>
</policy>
您可能会从此处使用键从其building
元素查找location-number
<xsl:key name="buildings" match="building" use="building-header/location-number" />
现在,您可以使用position()
生成所需的编号,但是要有效,您必须在一个选择语句中的覆盖范围内选择所有insured-object
的building
元素。
<xsl:variable name="insured-objects" select="location-rule/insured-objects/insured-object/insured-object-nr" />
<xsl:for-each select="key('buildings', $insured-objects)">
我还注意到,当两个xsl:if
语句之间的唯一区别似乎正在生成Name
元素时,您的XSLT似乎有很多重复。
尝试此XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:key name="buildings" match="building" use="building-header/location-number" />
<xsl:template match="/">
<policy>
<xsl:for-each select="//coverage">
<PolicyConditions>
<xsl:variable name="code" select="limit/limit-type/@code" />
<xsl:variable name="count" select="count(preceding-sibling::coverage[limit/limit-type/@code = $code]) + 1" />
<Name>
<xsl:choose>
<xsl:when test="$code='Bla'">EQ Blanket</xsl:when>
<xsl:when test="$code='Sub'">EQ Sublimit</xsl:when>
</xsl:choose>
<xsl:value-of select="$count" />
</Name>
<Item>
<xsl:value-of select="$count" />
</Item>
<xsl:variable name="insured-objects" select="location-rule/insured-objects/insured-object/insured-object-nr" />
<xsl:for-each select="key('buildings', $insured-objects)">
<PolicyConditionCriteria>
<Field>LOCNAME</Field>
<Value>
<xsl:value-of select="building-header/building-number"/>
</Value>
<ItemData>
<xsl:value-of select="position()" />
</ItemData>
</PolicyConditionCriteria>
</xsl:for-each>
</PolicyConditions>
</xsl:for-each>
</policy>
</xsl:template>
</xsl:stylesheet>