XSLT序列号生成

时间:2015-06-21 14:00:23

标签: java xslt xslt-2.0 xslt-grouping

我正在使用XSLT 2.0从XMl准备一个平面文件。我的输入XML是

<city>
  <family>
    <parent>A</parent>
    <child>A1</child>
  </family>
  <family>
    <parent>A</parent>
    <child>A2</child>
  </family>
  <family>
    <parent>B</parent>
    <child>B1</child>
  </family>
  <family>
    <parent>B</parent>
    <child>B2</child>
  </family>
  <family>
    <parent>B</parent>
    <child>B3</child>
  </family>
  <family>
    <parent>C</parent>
    <child>C1</child>
  </family>


 </city>

Expected Output
-----------------
01 A 
02 B
03 C

我必须对父组进行分组,并且每个父组在平面文件中都有一个条目,它应该具有正确的序列号。我无法正确生成序列。

1 个答案:

答案 0 :(得分:2)

假设您使用for-each-group,那么输出和格式化position()就足够了:

  <xsl:template match="/">
      <xsl:for-each-group select="city/family" group-by="parent">
          <xsl:value-of select="concat(format-number(position(), '00'), ' ', current-grouping-key(), '&#10;')"/>
      </xsl:for-each-group>
  </xsl:template>