Question

The question is similar to my original issue [here].1

My original issue was that I needed to insert a hyphen every two digits into a seven digit number. Now I need to make a six digit number into a seven digit number by adding a leading '0' and then I need to hyphenate the number every two digits, just as before.

Here is my code, I think I'm close but just not quite there.

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

Should I even use the variable name I created above named "AddZero"?

Was it helpful?

Solution

I certainly don't see any need for a variable to hold the '0' string.... It looks like you're producing 012-34-56-7, which certainly isn't what you've said you want, especially since 7 will be missing. Try

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

Or, to use the formatting you'd already come up with, add the zero prefix BEFORE formatting:

                <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)
                    )"/>

OTHER TIPS

Your comment says:

for when the leading "0" is dropped from the PID making it six digits long

IMHO, if a leading "0" can be dropped, then a leading "00" can be dropped too - and so on. Therefore you should pad the string back to 7 digits dynamically:

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

and then use:

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

Had to do something similar a while ago, you can do the same thing using 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>

This will output:

01-23-45-6
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top