带连字符的XSL concat +格式编号

时间:2014-03-03 02:35:00

标签: xslt concat

问题类似于我原来的问题[这里]。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”的变量名?

3 个答案:

答案 0 :(得分: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)
                    )"/>

答案 1 :(得分:0)

您的评论说:

  

当从PID中删除前导“0”时使其为6位数   长

恕我直言,如果可以删除前导“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)
)"/>

答案 2 :(得分:0)

不久前做过类似的事情,你可以用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
相关问题