I am trying to transform from one xml to another
input
<Surcharge>
<CommercialVehicle desc="Commercial Vehicle">395.00</CommercialVehicle>
<EnhancedElectrical desc="Enhanced Electrical">395.00</EnhancedElectrical>
<AirConditioningHeating desc="Air Conditioning/Heating">395.00</AirConditioningHeating>
</Surcharge>
desired output
<Options>
<Option>
<OptionId>0</OptionId>
<OptionDesc>Commercial Vehicle</OptionDesc>
<OptionName>Commercial Vehicle</OptionName>
<RetailRate>395.00</RetailRate>
<NetRate>395.00</NetRate>
<IsSurcharge>false</IsSurcharge>
</Option>
<Option>
<OptionId>0</OptionId>
<OptionDesc>Enhanced Electrical</OptionDesc>
<OptionName>Enhanced Electrical</OptionName>
<RetailRate>395.00</RetailRate>
<NetRate>395.00</NetRate>
<IsSurcharge>false</IsSurcharge>
</Option>
....
</Options>
I am using following XSL
<Options>
<xsl:for-each select="//Rate[(CvCvty = $vProg) and (MonthTerm=$vMonthTerm) and (MileageTerm=$vMileageTerm) and (Deductible=$vDeductible)]/Surcharge">
<xsl:choose>
<xsl:when test="string-length(.//@desc)>0">
<Option>
<OptionId>0</OptionId>
<OptionDesc>
<xsl:value-of select=".//@desc"/>
</OptionDesc>
<OptionName>
<xsl:value-of select=".//@desc"/>
</OptionName>
<RetailRate>
<xsl:value-of select="."/>
</RetailRate>
<NetRate>
<xsl:value-of select="."/>
</NetRate>
<IsSurcharge>false</IsSurcharge>
</Option>
</xsl:when>
</xsl:choose>
</xsl:for-each>
</Options>
But somehow i am getting this output, can someone help me figure out what has to be changed
<Options>
<Option>
<OptionId>0</OptionId>
<OptionDesc>Commercial Vehicle</OptionDesc>
<OptionName>Commercial Vehicle</OptionName>
<RetailRate>395.00395.00395.00</RetailRate>
<NetRate>395.00395.00395.00</NetRate> 开发者_如何学Go
<IsSurcharge>false</IsSurcharge>
</Option>
</Options>
Thanks
Your foreach is only selecting the single surcharge element, you need to change it to select the child nodes by putting /* at the end.
This will put the context on the element you're interested in, therefore you should use @desc
instead of the .//@desc
syntax.
精彩评论