Unanswered XSL Fixed text output

  • Tuesday, November 20, 2012 7:53 AM
     
     

    Hi,

    I have a scenario where from the XML, I need to get some fixed text output. Say I have the following XML, the text output should be like

    Name1     00000 3456700000

    The scenario here is - Say the first element should start at position 1 and end at position 10, if the text is only say 5 characters, the rest of the place should be blank, I never know what will be the size of the text(in this case say Name1) then the next 5 character positions should be zeros '00000' then say another field will come which is numeric, and can occupy 10 characters, but if the value is positive and it is only 3 digits, I will append it with zeros for the remaining 7 positions; but if it is negative, I need to add the zeros prior to the text i.e., the first 7 digits will be zeros.

    These are the basic rules, if you could advice me on how to take it in the XSL, it will be great, thanks.

    <Employees>
    <EmployeeCompany>
    <ID>1</ID>
    <Name>Name1</Name>
    <Name2>Name2</Name2>
    </Employee>
    <Employee>
    <ID>2</ID>
    <Name>Name1a</Name>
    <Name2>Name2b</Name2>
    </Employee>
    <Employee>
    <ID>3</ID>
    <Name>Name1c</Name>
    <Name2>Name3c</Name2>
    </EmployeeCompany>
    <Employees>


    Rpaul

All Replies

  • Wednesday, November 21, 2012 11:28 AM
     
     

    Not sure where you get the number 34567 from, but I have created a numeric element "Number" for each Employee:

    <?xml version="1.0" encoding="utf-16"?>
    <Employees>
      <EmployeeCompany>
        <Employee>
          <ID>1</ID>
          <Name>Name1</Name>
          <Name2>Name2</Name2>
          <Number>34567</Number>
        </Employee>
        <Employee>
          <ID>2</ID>
          <Name>Name1a</Name>
          <Name2>Name2b</Name2>
          <Number>-456</Number>
        </Employee>
        <Employee>
          <ID>3</ID>
          <Name>Name1c</Name>
          <Name2>Name3c</Name2>
          <Number>74567</Number>
        </Employee>
      </EmployeeCompany>
    </Employees>

    XSLT:

    <?xml version="1.0" encoding="utf-16"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output method="text" omit-xml-declaration="yes" indent="no" />
      <xsl:template match="/">
        <xsl:variable name="spaces">
          <xsl:value-of select="concat('          ','')" />
        </xsl:variable>
        <xsl:variable name="zeros">
          <xsl:value-of select="'0000000000'" />
        </xsl:variable>
        <xsl:for-each select="//Employee">
          <xsl:variable name="Name" select="concat(Name, substring($spaces,1,10-string-length(Name)))" />
          <xsl:variable name="Number">
            <xsl:choose>
              <xsl:when test="Number &gt; 0">
                <xsl:value-of select="concat(Number, substring($zeros,1,10-string-length(Number)))" />
              </xsl:when>
              <xsl:otherwise>
                <xsl:value-of select="concat(substring($zeros,1,11-string-length(Number)),substring(Number,2,string-length(Number)-1))" />
              </xsl:otherwise>
            </xsl:choose>
          </xsl:variable>
          <xsl:value-of select="concat($Name,substring($zeros,1,4),$Number,'&#xA;')" />
        </xsl:for-each>
      </xsl:template>
    </xsl:stylesheet>

    Morten la Cour