views:

340

answers:

3

Hi friends...I am having a lot of problem. I need the solution very urgent..please help me So i am having a xml file as:

<Order>
  <EP>
  <Name>Generation Date</Name>
  <Value>2009-08-04+05:30</Value>
  </EP>
  <EP>
  <Name>NoOfRecords</Name>
  <Value>100</Value>
  </EP>
 <OrderLineItems>
   <OrderLineItem OrderDateTime="2007-01-01T17:09:04.593+05:30>
   <Customer>
      <FullName>Mrs S </FulName>
   <Address>
     <AddressLine1>ABC</AddressLine1>
     <AddressLine2>XYZ</AddressLine2>
   </Address>
   </Customer>
   <EP>
      <Name>DealerAccount</Name>
      <Value>00000000000</Value>
   </EP>
 </OrderLineItem>
 </OrderLineItems>
</Order>

Where the OrderLineItem tag is repeating.Now i want to convert this xml to a text file using xslt. The format of flat file is fixed and it's as follows:

00000000000010107     Mrs S       ABC  XYZ 
00000000000150709     Mr x        PQR  TWR

where the first column contains the Dealeraccount and orderDate(time removed) second field is name and third and fourth field are addressline 1 and addressline2 respectively. Please note that the formatting of text file is must and i am also having the length of each feild such as length of name is varchar2(50) and so on. Friends please help me!!

A: 

Something like this should work - for the exact formatting, you'll need to tweak the <xsl:text> portions - add more spaces or other delimiters:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">

  <xsl:output method="text" indent="no"/>

  <xsl:template match="Order">
    <xsl:apply-templates select="OrderLineItems/OrderLineItem" />
  </xsl:template>

  <xsl:template match="OrderLineItem">
    <xsl:value-of select="EP/Value"/>
    <xsl:text>              </xsl:text>
    <xsl:value-of select="Customer/FullName"/>
    <xsl:text>     </xsl:text>
    <xsl:value-of select="Customer/Address/AddressLine1"/>
    <xsl:text>     </xsl:text>
    <xsl:value-of select="Customer/Address/AddressLine2"/>
    <xsl:text>
    </xsl:text>
  </xsl:template>
</xsl:stylesheet>

You can't do much more in terms of formatting raw text output in XSLT - it's really quite limited that way, unfortunately.

Marc

marc_s
To solve the OP's problem, the values should be padded to a fixed length (different but constant for each column). Example function: http://www.xsltfunctions.com/xsl/functx_pad-string-to-length.html
Wim Coenen
Good tip, thanks - but again - it's an XSLT add-on, it's not available in straight-up XSLT itself.
marc_s
Oops, I hadn't looked to closely at that link. But googling "xslt pad string" seems to yield many examples in standard XSLT.
Wim Coenen
A: 

Well i was trying this but it doesn't work: just consider that instead of EP tag m having ExtensionProperties tag...and plz discard general errors which could be because of xml differnce,but i need output as i mentioned...i.e. formatted text..

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns0="http://il.tesco.com/canonical/order/v1.0"&gt; 
<xsl:output method="text" encoding="UTF-8" indent="yes"/>
   <xsl:template match="/">   
      <xsl:param name="separator" select="' '"/>
      <xsl:param name="line-separator" select="'&#13;&#10;'"/>
      <xsl:variable name="GenerationDate" select="ns0:Order/ExtensionProperties/Name"/>
      <xsl:if test="$GenerationDate='Generation Date'">
        <xsl:variable name="formatedDate" select="ns0:Order/ExtensionProperties/Value" />
        <xsl:text>Tesco Direct</xsl:text>
        <xsl:value-of select="$separator" />
        <xsl:value-of select="concat(substring($formatedDate, 9, 2),substring($formatedDate, 6, 2),substring($formatedDate, 3, 2))"/>
        <xsl:value-of select="$line-separator" />
      </xsl:if>
      <xsl:for-each select="ns0:Order/OrderLineItems/OrderLineItem">
         <xsl:variable name="Dealer_Account" select="ExtensionProperties/Name" />

         <xsl:if test="$Dealer_Account='DealerAccount'">
           <xsl:value-of select="ExtensionProperties/Value" />
         </xsl:if>
         <xsl:variable name="DateOfSale" select='substring-before(@orderDateTime,"T")'/>
         <xsl:value-of select="concat(substring($DateOfSale, 9, 2),substring($DateOfSale, 6, 2),substring($DateOfSale, 3, 2))" />
         <xsl:comment>Insert Five Spaces </xsl:comment>
         <xsl:value-of select="$separator" />
         <xsl:value-of select="$separator" />
         <xsl:value-of select="$separator" />
         <xsl:value-of select="$separator" />
         <xsl:value-of select="$separator" />
         <xsl:value-of select="Customer/FullName" />
         <xsl:comment>Insert Five Spaces </xsl:comment>
         <xsl:value-of select="$separator" />
         <xsl:value-of select="$separator" />
         <xsl:value-of select="$separator" />
         <xsl:value-of select="$separator" />
         <xsl:value-of select="$separator" />
         <xsl:value-of select="Customer/Address/AddressLine1" />
         <xsl:comment>Insert Five Spaces </xsl:comment>
         <xsl:value-of select="$separator" />
         <xsl:value-of select="$separator" />
         <xsl:value-of select="$separator" />
         <xsl:value-of select="$separator" />
         <xsl:value-of select="$separator" />
         <xsl:value-of select="Customer/Address/AddressLine2" />
         <xsl:comment>Insert Five Spaces </xsl:comment>
         <xsl:value-of select="$separator" />
         <xsl:value-of select="$separator" />
         <xsl:value-of select="$separator" />
         <xsl:value-of select="$separator" />
         <xsl:value-of select="$separator" />
         <xsl:value-of select="Customer/Address/AddressLine3" />
         <xsl:comment>Insert Five Spaces </xsl:comment>
         <xsl:value-of select="$separator" />
         <xsl:value-of select="$separator" />
         <xsl:value-of select="$separator" />
         <xsl:value-of select="$separator" />
         <xsl:value-of select="$separator" />
         <xsl:value-of select="Customer/Address/AddressLine4" />
         <xsl:comment>Insert Five Spaces </xsl:comment>
         <xsl:value-of select="$separator" />
         <xsl:value-of select="$separator" />
         <xsl:value-of select="$separator" />
         <xsl:value-of select="$separator" />
         <xsl:value-of select="$separator" />
         <xsl:value-of select="Customer/Address/PostCode" />
         <xsl:value-of select="@itemCategoryID" />
         <xsl:value-of select="$line-separator" />
      </xsl:for-each>
    </xsl:template>
 </xsl:stylesheet>

Please help!!

Vivek
Why on earth don't you define a variable "FiveSpaces" and set it to ' ' and then just use one single `<xsl:value-of select="$FiveSpaces" /> instead of having (several times!) five individual xsl:value-of statements.....
marc_s
just because the client does not expects a variable containing 5 spaces in his code....it's imple enough..anyways i have figured it out
Vivek
A: 

Well finally i got it..here is the solution

<xsl:template name="ColumnSeparator">
     <xsl:param name="count" select="1"/>
     <xsl:param name="separator" select="' '"/>
     <xsl:if test="$count > 0">
      <xsl:value-of select="$separator"/>
      <xsl:call-template name="ColumnSeparator">
       <xsl:with-param name="count" select="$count - 1"/>
      </xsl:call-template>
     </xsl:if>
    </xsl:template>

And then simply call this template using:

<xsl:call-template name="ColumnSeparator">
       <xsl:with-param name="count" select="50-string-length(Customer/FullName)"/>
      </xsl:call-template>
Vivek