xslt 条件增量 [英] xslt conditional increment

查看:37
本文介绍了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"
      • 将计数器设置为 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

        有什么想法吗?

        推荐答案

        这个转变:

        <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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆