Iam坚持使用xslt递增值。我需要根据特定条件增加元素值。在实际xml中,每个策略/ PolicyConditions都满足,我需要增加1并创建policy / PolicyConditions / PolicyConditionCriteria / ItemData。请查看预期的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>
答案 0 :(得分:0)
您可以在此处使用密钥以building
location-number
个元素
<xsl:key name="buildings" match="building" use="building-header/location-number" />
现在,您可以使用position()
生成所需的数字,但为了实现此目的,您必须为一个覆盖范围内的所有building
选择insured-object
元素选择陈述。
<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>