XSLT翻译

时间:2011-03-01 21:23:38

标签: xslt translation

我正在尝试从一个xml转换为另一个

输入

<Surcharge>
  <CommercialVehicle desc="Commercial Vehicle">395.00</CommercialVehicle> 
  <EnhancedElectrical desc="Enhanced Electrical">395.00</EnhancedElectrical> 
  <AirConditioningHeating desc="Air Conditioning/Heating">395.00</AirConditioningHeating> 
</Surcharge>

所需的输出

<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>

我正在使用以下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>

但不知怎的,我得到了这个输出,有人可以帮我找出必须改变的内容

<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> 
  <IsSurcharge>false</IsSurcharge> 
  </Option>
</Options>

由于

1 个答案:

答案 0 :(得分:1)

您的foreach只选择单个附加费元素,您需要通过将/ *放在最后来更改它以选择子节点。

这会将上下文放在您感兴趣的元素上,因此您应该使用@desc而不是.//@desc语法。