Kapil
Kapil

Reputation: 832

Xml to text with formatting

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 field such as length of name is varchar2(50) and so on.

Upvotes: 1

Views: 1379

Answers (2)

Kapil
Kapil

Reputation: 832

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>

Upvotes: 1

marc_s
marc_s

Reputation: 754220

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

Upvotes: 0

Related Questions