问题
I'm having problems incrementing a counter under certain conditions.
Input:
<Users>
<User>
<id>1</id>
<username>jack</username>
</User>
<User>
<id>2</id>
<username>bob</username>
</User>
<User>
<id>3</id>
<username>bob</username>
</User>
<User>
<id>4</id>
<username>jack</username>
</User>
</Users>
Wanted Output:
<Users>
<User>
<id>1</id>
<username>jack01</username>
</User>
<User>
<id>2</id>
<username>bob01</username>
</User>
<User>
<id>3</id>
<username>bob02</username>
</User>
<User>
<id>4</id>
<username>jack02</username>
</User>
</Users>
To accomplish this following algorithm can be used:
- sort input by username
- for each user
- when previous username is equals current username
- increment counter and
- set username to '$username$counter'
- otherwise
- set counter to 1
- when previous username is equals current username
- (sort by id again - not really necessary)
So i tried to transform this into XSLT:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<xsl:template match="Users">
<Users>
<xsl:apply-templates select="create_user">
<xsl:sort select="User/username"/>
</xsl:apply-templates>
</Users>
</xsl:template>
<xsl:template match="create_user">
<id><xsl:value-of select="id"/></id>
<xsl:choose>
<xsl:when test="username=(preceding-sibling::User[1]//username)">
<xsl:variable name="count">
<xsl:number format="01"/>
</xsl:variable>
<username><xsl:value-of select="concat(username, $count)"/></username>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="count">
<xsl:number value="1" format="01"/>
</xsl:variable>
<username><xsl:value-of select="concat(username, $count)"/></username>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
However, by executing this i get following errors:
- usernames do not sort
- counter does not increment
- instead when condition matches counter will be the current node-position.
- for our example the node with id = 3 would have the username = bob03
- the tag is missing
Any thoughts?
回答1:
This transformation:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="username/text()">
<xsl:value-of select="."/>
<xsl:value-of select=
"format-number(count(../../preceding-sibling::*[username=current()])+1,
'00')
"/>
</xsl:template>
</xsl:stylesheet>
when applied on the provided XML document:
<Users>
<User>
<id>1</id>
<username>jack</username>
</User>
<User>
<id>2</id>
<username>bob</username>
</User>
<User>
<id>3</id>
<username>bob</username>
</User>
<User>
<id>4</id>
<username>jack</username>
</User>
</Users>
produces the wanted, correct result:
<Users>
<User>
<id>1</id>
<username>jack01</username>
</User>
<User>
<id>2</id>
<username>bob01</username>
</User>
<User>
<id>3</id>
<username>bob02</username>
</User>
<User>
<id>4</id>
<username>jack02</username>
</User>
</Users>
回答2:
"Incrementing a counter" isn't what you do in a functional programming language. You need to describe the output you want as a function of the input (as Dimitre has done), rather than describing a process or procedure for computing the output.
来源:https://stackoverflow.com/questions/13714647/xslt-conditional-increment