How to convert XML into formated text file whith XSlt

I have XML file which is looks like:

<Report>
<Total>
    <RecordValues>
        <Record>
        <FieldValue fieldName="index"       fieldValue="1" />
        <FieldValue fieldName="version"     fieldValue="100" />
        <FieldValue fieldName="user"        fieldValue="tester" />
        <FieldValue fieldName="date_modified"   fieldValue="2010-10-18 12:18:12" />
        <FieldValue fieldName="object_name"     fieldValue="Menu" />
        <FieldValue fieldName="permission"  fieldValue="Permission X" />
        </Record>
        <Record>
        <FieldValue fieldName="index"       fieldValue="2" />
        <FieldValue fieldName="version"     fieldValue="100" />
        <FieldValue fieldName="user"        fieldValue="user1" />
        <FieldValue fieldName="date_modified"   fieldValue="2010-12-15 12:18:12" />
        <FieldValue fieldName="object_name"     fieldValue="Control" />
        <FieldValue fieldName="permission"  fieldValue="Permission E" />
        </Record>
        <Record>
        <FieldValue fieldName="index"       fieldValue="3" />
        <FieldValue fieldName="version"     fieldValue="15" />
        <FieldValue fieldName="user"        fieldValue="user2" />
        <FieldValue fieldName="date_modified"   fieldValue="2010-10-02 12:18:12" />
        <FieldValue fieldName="object_name"     fieldValue="Run" />
        <FieldValue fieldName="permission"  fieldValue="Permission R" />
        </Record>
    </RecordValues>
</Total>

I have to convert it with XSLT into formated text file:

1 100  101810
tester  Menu       Permission X
2 100  121510
user1   Control    Permission E
3 15   100210
user2   Run        Permission R 

The only fieldValue attribute must be used and all fields in the text file have fixed length. Please help me. Thanks.

Answers


This stylesheet:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:m="map">
    <xsl:output method="text"/>
    <xsl:strip-space elements="*"/>
    <xsl:variable name="vPadding"
         select="'             &#xA;                               '"/>
    <m:m n="index" p="1"/>
    <m:m n="version" p="3"/>
    <m:m n="user" p="15"/>
    <m:m n="date_modified" p="8"/>
    <m:m n="object_name" p="23"/>
    <m:m n="permission" p="34"/>
    <xsl:template match="Record">
        <xsl:apply-templates select="*[1]"/>
    </xsl:template>
    <xsl:template match="FieldValue">
        <xsl:param name="pOutput" select="$vPadding"/>
        <xsl:variable name="vValue">
            <xsl:apply-templates select="@fieldValue"/>
        </xsl:variable>
        <xsl:variable name="vPos"
                      select="document('')/*/m:m
                                 [@n=current()/@fieldName]/@p"/>
        <xsl:variable name="vOutput"
             select="concat(substring($pOutput,1,$vPos -1),
                            $vValue,
                            substring($pOutput,$vPos+string-length($vValue)))"/>
        <xsl:variable name="vNext" select="following-sibling::*[1]"/>
        <xsl:apply-templates select="$vNext">
            <xsl:with-param name="pOutput" select="$vOutput"/>
        </xsl:apply-templates>
        <xsl:if test="not($vNext)">
            <xsl:value-of select="concat($vOutput,'&#xA;')"/>
        </xsl:if>
    </xsl:template>
    <xsl:template match="@*[../@fieldName='date_modified']">
        <xsl:value-of select="concat(substring(translate(.,'-',''),5,4),
                                     substring(.,3,2))"/>
    </xsl:template>
</xsl:stylesheet>

Output:

1 100  101810
tester  Menu       Permission X
2 100  121510
user1   Control    Permission E
3 15   100210
user2   Run        Permission R

Note: Fine grained traversal, a padding mask, inline map with value starting position.

EDIT: Strip space just in case...


Need Your Help

This batch script returns incorrect value when invoked from a different directory

windows for-loop batch-file cmd folder

This batch script returns incorrect value when invoked from a different directory. I am hoping someone can help me fix this so that it always determines the correct APP_HOME directory.

iOS Partial Keyboard Display

ios objective-c xcode

Is it possible to display only the part of the keyboard in iOS 7? For example, I would like to display the keyboard, but I do not want the space bar visible. How would I add the keyboard to displ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.