增量属性值xslt

时间:2015-07-28 14:39:15

标签: xslt

我一直在尝试增加以下xml的属性值。在获得理想结果时,请帮助我错误。

输入xml:

<?xml version="1.0" encoding="UTF-8" ?>
<GetProratedPriceInput xmlns="http://www.BRMProration.org">
   <ProductsInfo>
      <START_T>2015-07-28T18:07:07.561</START_T>
      <END_T>2015-07-28T18:07:07.561</END_T>
   </ProductsInfo>
   <ProductsInfo>
      <START_T>2015-07-28T18:07:07.561</START_T>
      <END_T>2015-07-28T18:07:07.561</END_T>
   </ProductsInfo>
   <ProductsInfo>
      <START_T>2015-07-28T18:07:07.562</START_T>
      <END_T>2015-07-28T18:07:07.562</END_T>
   </ProductsInfo>
</GetProratedPriceInput>

我正在使用的XSLT。

<xsl:template match="/">
    <xsl:variable name="counter">
      <xsl:value-of select="0"/>
    </xsl:variable>
    <abc:inputFlist>
      <xsl:for-each select="/ns2:GetProratedPriceInput/ns2:ProductsInfo">
        <abc:RESULTS elem="{$counter}">
          <abc:END_T>
            <xsl:value-of select="ns2:END_T"/>
          </abc:END_T>
          <abc:START_T>
            <xsl:value-of select="ns2:START_T"/>
          </abc:START_T>
          <xsl:variable name="counter">
            <xsl:value-of select="$counter + 1"/>
          </xsl:variable>
        </abc:RESULTS>
      </xsl:for-each>
    </abc:inputFlist>
  </xsl:template>
</xsl:stylesheet>

来自此xslt的输出:

<abc:inputFlist xmlns:abc="http://xmlns.oracle.com/abc/schemas/BusinessOpcodes">
      <abc:RESULTS elem="0">
      <abc:END_T>2015-07-28T18:07:07.561</abc:END_T>
      <abc:START_T>2015-07-28T18:07:07.561</abc:START_T>
   </abc:RESULTS>
   <abc:RESULTS elem="0">
      <abc:END_T>2015-07-28T18:07:07.561</abc:END_T>
      <abc:START_T>2015-07-28T18:07:07.561</abc:START_T>
   </abc:RESULTS>
   <abc:RESULTS elem="0">
      <abc:END_T>2015-07-28T18:07:07.562</abc:END_T>
      <abc:START_T>2015-07-28T18:07:07.562</abc:START_T>
   </abc:RESULTS>
</abc:inputFlist>

但所需的输出如下:

<abc:inputFlist xmlns:abc="http://xmlns.oracle.com/abc/schemas/BusinessOpcodes">
      <abc:RESULTS elem="0">
      <abc:END_T>2015-07-28T18:07:07.561</abc:END_T>
      <abc:START_T>2015-07-28T18:07:07.561</abc:START_T>
   </abc:RESULTS>
   <abc:RESULTS elem="1">
      <abc:END_T>2015-07-28T18:07:07.561</abc:END_T>
      <abc:START_T>2015-07-28T18:07:07.561</abc:START_T>
   </abc:RESULTS>
   <abc:RESULTS elem="2">
      <abc:END_T>2015-07-28T18:07:07.562</abc:END_T>
      <abc:START_T>2015-07-28T18:07:07.562</abc:START_T>
   </abc:RESULTS>
</abc:inputFlist>

请帮助增量计数器变量出错。

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

XSLT中的变量是不可变的,无法更改。在你的代码中,你只是定义一个新的变量“阴影”前一个变量,但它实际上会在每个xsl:for-each块的末尾立即超出范围

这里根本不需要使用变量。您可以使用position()函数来跟踪计数。只需将行<abc:RESULTS elem="{$counter}">更改为:

 <abc:RESULTS elem="{position() - 1}">