tags:

views:

122

answers:

3

I have XML in the following format:

<Order>
  <Customer>
     <Name>kapil</name>
     <AddressLine1>ABC</AddressLine1>
     <PostCode>12345</postCode>
  </Customer>
  <Customer>
     <Name>Soniya</name>
     <AddressLine1>XYZPER</AddressLine1>
     <PostCode>54321</postCode>
  </Customer>
  <Customer>
     <Name>kapil</name>
     <AddressLine1>ABC</AddressLine1>
     <PostCode>12345</postCode>
  </Customer>
</Order>

And I want the text file in a particular format as

Soniya    XYZPER   54321
Kapil     ABC      12345

I want to do it via XSLT.

A: 
<?xml version='1.0' encoding='UTF-8'?>
<xsl:stylesheet 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0"
>

<xsl:template match='/Order'>
  <xsl:for-each select='Customer'>
    <xsl:value-of select='Name' />
    <xsl:value-of select='AddressLine1' />
    <xsl:value-of select='PostCode' />
  </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

This doesn't address the ordering (which you haven't specified), and getting everything to line up in columns, which might be tricky...

Ned Batchelder
Thank's for your help Ned but...That's exactly i want to do...all columns should be properly lined up....i.e. the text must be formatted...every new address and postcode should get start from the other one
Vivek
+1  A: 

In order to pad a string with spaces in XSLT 1.0 you can use a named template like this:

<xsl:template name="ppad">
    <xsl:param name="str" />
    <xsl:param name="chr" select="' '" />
    <xsl:param name="len" select="0" />
    <xsl:choose>
        <xsl:when test="string-length($str) &lt; $len">
            <xsl:call-template name="ppad">
                <xsl:with-param name="str" select="concat($str, $chr)" />
                <xsl:with-param name="len" select="$len" />
                <xsl:with-param name="chr" select="$chr" />
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$str" />
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

And then call it with the string and length as parameters:

<xsl:call-template name="rpad">
    <xsl:with-param name="str" select="Name" />
    <xsl:with-param name="len" select="16" />
</xsl:call-template>
Jörn Horstmann
thanks a ton Jorn...but can u please implement it on my example above and post..i will be highly appreciated....Actually i am new to XSLT :)
Vivek
+1  A: 

you could use XSL-FO and a XSL-FO processor, which could help you format your output (a table disposition for your results in your case) and output it to various formats (plain text, PDF ...) For starters, you should check w3schools, and use Apache FOP - an open source solution - to process your XML documents. As far as I am concerned, I've used XSL-FO to generate PDF from XML files.

peroumal1
I can't use XSL-FO ,because my requirement says i only have to use XSLT
Vivek