XSL formatting number with 'hyphens' every two digits

元气小坏坏 提交于 2019-12-24 07:06:05

问题


I have searched for days and found one example [here][1]

[1]: XSLT formatting numbers, insert '-' after every 4 digits starting from the right most digit, but it doesn't seem to work out as expected.

Here is what I need to happen. I have a number that is 7 digits like '1122334' and I need to coerce it to '11-22-33-4'.

Here is my code so far:

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="xml"/>
    <xsl:template match="/dataset">
      <xsl:decimal-format name="dashes" grouping-separator='-'/>
      <dataset>
         <!-- Nullify (0040,A043) Concept Name Code Sequence -->
        <attr tag="0040A043" vr="SQ"/>
      </dataset>
      <dataset>
       <xsl:variable name="modPatientID" select="attr[@tag='00100020']"/>
       <xsl:variable name="AccNum" select="attr[@tag='00080050']"/>
         <xsl:if test="string-length($modPatientID)=7">
           <xsl:if test="contains(attr[@tag='00080050'],'_')">
             <!-- (0008,0050) Accession_Number -->
             <attr tag="00100020" vr="LO">
               <xsl:value-of select="format-number($modPatientID, '##-##-##-#','dashes')"/>
             </attr>
            </xsl:if>
           </xsl:if>
          </dataset>
         </xsl:template>
        </xsl:stylesheet>

The output I get back is '1-1-2-2-3-3-4' and I want it to look like '11-22-33-4'

Can anyone explain to me what I am doing wrong? If possible I would like to continue using the 'format-number' function.

thank you

-frustrated


回答1:


I have a number that is 7 digits like '1122334' and I need to coerce it to '11-22-33-4'.

Here's one way:

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

Here's another:
---Don't use this - see my edit #2 below---

<xsl:value-of select="concat(
   format-number(substring($modPatientID, 1, 6), '-##', 'dashes'), '-', 
   substring($modPatientID, 7)
)"/>

Edit:
Here's a third one, which is merely a correction of what was suggested by @helderdarocha:
---Don't use this - see my edit #2 below---

<xsl:value-of select="substring(format-number(concat($modPatientID, 0), '-##', 'dashes'), 1, 10)"/>

EDIT 2:

This should have occurred to me right from the start: your PatientID is not a number and you shouldn't try applying number formatting to it. Otherwise you will lose any leading zeros the "number" might have.




回答2:


You can use str:split template from http://www.exslt.org/str/index.html.

I just tested your sample input '1122334' and it works fine.

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:func="http://exslt.org/functions"
    xmlns:exsl="http://exslt.org/common"
    xmlns:str="http://exslt.org/strings"
    extension-element-prefixes="str func exsl">
    <xsl:import href="str.xsl" />

    <xsl:template match="/">
        <div>
            <xsl:variable name="num">1122334</xsl:variable>
            <xsl:for-each select="str:split($num, '')">
                <xsl:value-of select="."/>
                <xsl:if test="(position() mod 2) = 0 and (position() != last())">-</xsl:if>

            </xsl:for-each>
        </div>
    </xsl:template>
</xsl:stylesheet>



回答3:


Try running this stylesheet, with any source:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:decimal-format name="dashes" grouping-separator='-'/>
    <xsl:template match="/">
        <xsl:value-of select="format-number('1122334', '##-##-##-#','dashes')"/>
    </xsl:template>
</xsl:stylesheet>

Does it print 11-22-33-4? If not, it reveals something about the transformer you are using (probably Xalan or Saxon 6.5.5). In that case it won't make any difference if you use ##-##-##-# or simply -#, since the grouping separator is only read once, from the right.

You can try -## and you will have your number in groups of two.

Saxon 9 prints the number the way you expected (which is surprising to me). If you can't use it as your XSL processor, you can try the hack below:

<xsl:value-of select="substring-before(format-number(concat('1122334','0'), '-##','dashes'), '0')"/>

I added a 0, formatted the number in groups of 2, and then removed the 0.

Edit: That was a bad hack. @michael.hor257k rightly observed that it would fail as soon as a number with a zero was inserted. So instead of substring-before use substring as suggested in his solution:

<xsl:value-of select="substring(format-number(concat('1122334', 0), '-##', 'dashes'), 1, 10)"/>



回答4:


With your code, I get an error because the xsl:decimal-format is within the xsl:template; it should be outside. When I fix that it works for me (in Saxon 9.5).

But I agree, treating a patient ID as a number is not good.

I have no end of trouble with my spreadsheet software treating VAT numbers as numeric because they are all-digits, and thus losing any leading zeroes.



来源:https://stackoverflow.com/questions/22053343/xsl-formatting-number-with-hyphens-every-two-digits

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!