xslt条件增量 [英] xslt conditional increment
本文介绍了xslt条件增量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在某些情况下,我无法增加计数器的数量.
I'm having problems incrementing a counter under certain conditions.
输入:
<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>
想要的输出:
<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:
- 按用户名排序输入 每个用户
-
- 先前的用户名等于当前的用户名时
- 增量计数器和
- 将用户名设置为"$ username $ counter"
- sort input by username
- for each user
- when previous username is equals current username
- increment counter and
- set username to '$username$counter'
- 将计数器设置为1
所以我试图将其转换为XSLT:
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:
- 用户名不排序
- 计数器不增加
- 当条件匹配时,计数器将是当前节点位置.
- 在我们的示例中,id = 3的节点的用户名= bob03
- 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
有什么想法吗?
推荐答案
此转换:
<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>
应用于提供的XML文档时
<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>
产生想要的正确结果:
<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>
这篇关于xslt条件增量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- when previous username is equals current username
- 先前的用户名等于当前的用户名时
查看全文