我想添加<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>
</xsl:text> <!-- linefeed -->
</xsl:template>
</xsl:stylesheet>