Xslt XSL concat+;用连字符格式化数字

Xslt XSL concat+;用连字符格式化数字,xslt,concat,Xslt,Concat,这个问题类似于我的原版[这里] 我最初的问题是,我需要在一个七位数的数字中每隔两位数插入一个连字符。现在,我需要通过添加前导“0”将一个六位数字转换为七位数字,然后需要每两位数字连字符,就像前面一样 这是我的代码,我想我已经很接近了,但只是不太接近 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="xml"/>

这个问题类似于我的原版[这里]

我最初的问题是,我需要在一个七位数的数字中每隔两位数插入一个连字符。现在,我需要通过添加前导“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”的变量名吗?

我当然不认为需要一个变量来保存“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)
                    )"/>

您的评论是:

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

依我看,如果可以删除前导的“0”,那么也可以删除前导的“00”——依此类推。因此,您应该动态地将字符串填充回7位:


然后使用:

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

不久前,您必须执行类似的操作,您可以使用格式编号执行相同的操作:

<?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>

两种很好的方法,谢谢你的帮助。这正是我要找的!我尝试了“第二种”方法,它抱怨“$modPatientID”没有变量,但第一种方法按预期工作。非常感谢。输入错误——当变量使用“Id”时,我写了“Id”。已修复。您的建议仅适用于Saxon 9.5。其他处理器将返回
0-1-2-3-4-5-6
。这已经在另一个线程中讨论过了。对不起,我的坏消息是没有阅读另一个线程,我不知道这只适用于Saxon 9.5…所以要记住这是一件好事!
<?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