0

我想添加<LineNum>[number]</LineNum>输出,如果我们有不同的值,LineNum 必须再次从“1”开始<cell num="4">

源代码:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<table>
   <sheet name="Notification" num="1">
      <row num="1">
         <cell num="1">IBD</cell>
         <cell num="2">YES</cell>
         <cell num="3">6600027830(010000)</cell>
         <cell num="4">800015001</cell>
      </row>
      <row num="2">
         <cell num="1">IBD</cell>
         <cell num="2">YES</cell>
         <cell num="3">6600027830(010000)</cell>
         <cell num="4">800015001</cell>
      </row>
      <row num="3">
         <cell num="1">IBD</cell>
         <cell num="2">YES</cell>
         <cell num="3">6600027831(010000)</cell>
         <cell num="4">800015002</cell>
      </row>
      <row num="4">
         <cell num="1">IBD</cell>
         <cell num="2">YES</cell>
         <cell num="3">6600027831(010000)</cell>
         <cell num="4">800015002</cell>
      </row>
      <row num="5">
         <cell num="1">IBD</cell>
         <cell num="2">YES</cell>
         <cell num="3">6600027831(010000)</cell>
         <cell num="4">800015002</cell>
      </row>
   </sheet>
</table>

在这种情况下也是如此:

<cell num="4">800015001</cell>
<LineNum>1</LineNum>
<cell num="4">800015001</cell>
<LineNum>2</LineNum>

<!-- cell num="4" different from previous, start LineNum from 1 -->
<cell num="4">800015002</cell>
<LineNum>1</LineNum>
<cell num="4">800015002</cell>
<LineNum>2</LineNum>
<cell num="4">800015002</cell>
<LineNum>3</LineNum>

我可以添加全局变量,将单元格 num="4" 分配给它,然后检查当前值是否与全局变量相同,如果不是 - 再次从 1 开始 LineNum 并重新分配全局变量。遗憾的是 XSLT 不提供重新分配。

我能做些什么?

UPD: 试图以某种方式修改其他问题XSL: Counting Previous Unique Siblings中的示例,但没有运气..

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="text"/>

  <!-- don't copy whitespace -->
  <xsl:template match="text()"/>

  <xsl:template match="row/cell[@num=4]">
    <xsl:variable name="roles-so-far" select=". | preceding::row/cell[@num=4]"/>
    <xsl:value-of select="count($roles-so-far)"/>
    <xsl:value-of select="' '"/>
    <xsl:value-of select="."/>

    <!-- Only select the first instance of each ROLE name -->
    <xsl:variable name="roles-so-far-unique"
                  select="$roles-so-far[not(. = preceding-sibling::row/cell[@num=4])]"/>

    <xsl:apply-templates select="/"/>
    <xsl:text> </xsl:text>
    <xsl:value-of select="count($roles-so-far-unique)"/>
    <xsl:text>&#xA;</xsl:text> <!-- linefeed -->


  </xsl:template>

</xsl:stylesheet>
4

1 回答 1

1

将问题标记为 xslt-2.0 后,您可以轻松使用for-each-group group-adjacent

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  exclude-result-prefixes="xs"
  version="2.0">

<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="@* | node()">
  <xsl:copy>
    <xsl:apply-templates select="@* , node()"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="sheet">
  <xsl:copy>
    <xsl:for-each-group select="row" group-adjacent="cell[@num = 4]">
      <xsl:apply-templates select="current-group()"/>
    </xsl:for-each-group>
  </xsl:copy>
</xsl:template>

<xsl:template match="row">
  <xsl:copy>
    <xsl:apply-templates select="@* , node()"/>
    <LineNum><xsl:value-of select="position()"/></LineNum>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>

当我将上面的 Saxon 9.4 代码应用于您的输入示例时,我得到以下结果:

<table>
   <sheet>
      <row num="1">
         <cell num="1">IBD</cell>
         <cell num="2">YES</cell>
         <cell num="3">6600027830(010000)</cell>
         <cell num="4">800015001</cell>
         <LineNum>1</LineNum>
      </row>
      <row num="2">
         <cell num="1">IBD</cell>
         <cell num="2">YES</cell>
         <cell num="3">6600027830(010000)</cell>
         <cell num="4">800015001</cell>
         <LineNum>2</LineNum>
      </row>
      <row num="3">
         <cell num="1">IBD</cell>
         <cell num="2">YES</cell>
         <cell num="3">6600027831(010000)</cell>
         <cell num="4">800015002</cell>
         <LineNum>1</LineNum>
      </row>
      <row num="4">
         <cell num="1">IBD</cell>
         <cell num="2">YES</cell>
         <cell num="3">6600027831(010000)</cell>
         <cell num="4">800015002</cell>
         <LineNum>2</LineNum>
      </row>
      <row num="5">
         <cell num="1">IBD</cell>
         <cell num="2">YES</cell>
         <cell num="3">6600027831(010000)</cell>
         <cell num="4">800015002</cell>
         <LineNum>3</LineNum>
      </row>
   </sheet>
</table>
于 2012-12-24T09:55:44.360 回答