【问题标题】:XSL concat + formatting number with hyphensXSL concat + 带连字符的格式化数字
【发布时间】:2014-04-03 23:11:59
【问题描述】:

这个问题类似于我原来的问题 [这里].1

我最初的问题是我需要每两位数插入一个连字符到一个七位数字中。现在我需要通过添加一个前导“0”将一个六位数字变成一个七位数字,然后我需要像以前一样每两个数字连字符一次。

这是我的代码,我想我已经很接近了,但还不够。

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="xml"/>
    <xsl:decimal-format name="dashes" grouping-separator='-'/>
    <xsl:template match="/dataset">
        <dataset>
            <!-- Nullify (0040,A043) Concept Name Code Sequence -->
            <attr tag="0040A043" vr="SQ"/>
            <attr tag="00100021" vr="LO">HOSP</attr>
        </dataset>
        <dataset>
            <!-- for when the leading "0" is dropped from the PID making it six digits long -->
            <xsl:variable name="modPatientID" select="attr[@tag='00100020']"/>
            <xsl:variable name="AddZero" select="'0'"/>
            <xsl:variable name="Station" select="attr[@tag='00081010']"/>
            <!-- (0008,1010) Station_Name -->
            <xsl:if test="string-length($modPatientID)=6">
                <xsl:if test="contains($Station,'J')">
                    <attr tag="00100020" vr="LO">
                        <xsl:value-of select="concat(
                        $AddZero, substring($modPatientID, 1, 2), '-',
                            substring($modPatientID, 3, 2), '-',
                            substring($modPatientID, 5, 2), '-',
                            substring($modPatientID, 7)
                        )"/>
                    </attr>
                </xsl:if>
            </xsl:if>
        </dataset>
    </xsl:template>
</xsl:stylesheet>

我是否应该使用上面创建的名为“AddZero”的变量名?

【问题讨论】:

    标签: xslt concat


    【解决方案1】:

    我当然认为不需要变量来保存“0”字符串....看起来您正在生成 012-34-56-7,这肯定不是您所说的你想要,特别是因为 7 会丢失。试试

                    <xsl:value-of select="concat(
                            '0', substring($modPatientID, 1, 1), '-',
                            substring($modPatientID, 2, 2), '-',
                            substring($modPatientID, 4, 2), '-',
                            substring($modPatientID, 6)
                        )"/>
    

    或者,要使用您已经提出的格式,请在格式之前添加零前缀:

                    <xsl:variable name="paddedToSeven" select="concat('0',$modPatientID)"/>
                    <xsl:value-of select="concat(
                            substring($paddedToSeven, 1, 2), '-',
                            substring($paddedToSeven, 3, 2), '-',
                            substring($paddedToSeven, 5, 2), '-',
                            substring($paddedToSeven, 7)
                        )"/>
    

    【讨论】:

    • 两种很好的方法,感谢大家的帮助。这正是我想要的!
    • 我尝试了“第二个”方法,它抱怨“$modPatientID”没有变量,但第一个方法按预期工作。谢谢!
    • 错字——当你的变量使用“ID”时,我写了“Id”。固定。
    【解决方案2】:

    你的评论说:

    用于当从 PID 中删除前导“0”使其成为六位数字时 长

    恕我直言,如果可以删除前导“0”,则也可以删除前导“00” - 依此类推。因此,您应该将字符串填充回 7 位 动态

    <xsl:variable name="pID" select="substring(concat('0000000', $modPatientID), 1 + string-length($modPatientID))" />
    

    然后使用:

    <xsl:value-of select="concat(
        substring($pID, 1, 2), '-', 
        substring($pID, 3, 2), '-' , 
        substring($pID, 5, 2), '-' ,
        substring($pID, 7)
    )"/>
    

    【讨论】:

      【解决方案3】:

      不久前不得不做类似的事情,您可以使用 format-number 做同样的事情:

      <?xml version="1.0" encoding="UTF-8"?>
      <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
          xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="1.0">
      
          <xsl:decimal-format grouping-separator="-" name="hyphenFormatting"/>
      
          <xsl:template match="/">
              <xsl:value-of select="format-number(123456,'00-00-00-0','hyphenFormatting')"/>
          </xsl:template>
      
      </xsl:stylesheet>
      

      这将输出:

      01-23-45-6
      

      【讨论】:

      • 您的建议仅适用于 Saxon 9.5。其他处理器将返回0-1-2-3-4-5-6。这已经在另一个线程中讨论过了。
      • 抱歉,我没有阅读其他线程,我不知道这仅适用于 Saxon 9.5 ...所以记住这一点是一件好事!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多